Subversion Repositories SmartDukaan

Rev

Rev 2752 | Rev 3171 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import in.shop2020.utils.DataLogger;
import in.shop2020.datalogger.EventType;
import in.shop2020.model.v1.user.PromotionException;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.model.v1.user.PromotionService;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.thrift.clients.PromotionServiceClient;

@SuppressWarnings("serial")
@Results({
    @Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
})
public class PromotionController extends BaseController {

    public PromotionController() {
        super();
    }
    private static Logger log = Logger.getLogger(Class.class);
    
    public String create()  {
        String action = request.getParameter("action");
        log.info("Action is: " + action);
        
        UserContextServiceClient userServiceClient = null;
        PromotionServiceClient promotionServiceClient = null;
        
        try {
            long cartId = userinfo.getCartId();

            if(action == null || action.isEmpty())  {
                addActionError("Invalid Request Action");
                return "redirect";
            }
            String couponCode = request.getParameter("coupon_code");
            
            if (action.equals("applycoupon"))   {
                if (couponCode == null || couponCode.isEmpty()) {
                    addActionError("Coupon Code field cannot be left empty");
                    return "redirect";
                }
                promotionServiceClient = new PromotionServiceClient();
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
                promotionClient.applyCoupon(couponCode, cartId);
                DataLogger.logData(EventType.COUPON_APPLIED, session.getId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
            }
            else if (action.equals("removecoupon"))    {
                userServiceClient = new UserContextServiceClient();
                UserContextService.Client userClient = userServiceClient.getClient();
                userClient.removeCoupon(cartId);
                DataLogger.logData(EventType.COUPON_REMOVED, session.getId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
            }
        } catch (PromotionException e) {
            log.error("Unable to apply or remove coupon", e);
            addActionError(e.getMessage());
        } catch (Exception e) {
            log.error("Unable to apply or remove coupon", e);
        }
        return "redirect";
    }
}