Subversion Repositories SmartDukaan

Rev

Rev 12019 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.datalogger.EventType;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.PromotionException;
import in.shop2020.model.v1.user.PromotionService;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.serving.interceptors.UserInterceptor;
import in.shop2020.thrift.clients.PromotionClient;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.DataLogger;

import javax.servlet.http.Cookie;

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

@SuppressWarnings("serial")

@InterceptorRefs({
    @InterceptorRef("myDefault")
})

@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);
        
        UserClient userServiceClient = null;
        PromotionClient 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.equalsIgnoreCase("applycoupon"))   {
                if (couponCode == null || couponCode.isEmpty()) {
                    addActionError("Coupon Code field cannot be left empty");
                    return "redirect";
                } else if (couponCode.equals("saholicdeals")) {
                        Cookie co = cookiesMap.get(UserInterceptor.DEAL_COUPON_REMOVED);
                        if (co != null) {
                                co.setMaxAge(0);
                                co.setPath("/");
                                co.setDomain(domainName);
                        }
                }
                promotionServiceClient = new PromotionClient();
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
                String str = "Coupon applied";
                Cart cart = promotionClient.applyCoupon(couponCode, cartId);
                if(cart.getMessage() != null && !cart.getMessage().equals("")){
                        str = str +  ", " + cart.getMessage();
                }
                addActionMessage(str);
//                DataLogger.logData(EventType.COUPON_APPLIED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
            }
            else if (action.equals("removecoupon"))    {
                if(userinfo.isPrivateDealUser()) {
                        if (cookiesMap.get(UserInterceptor.DEAL_COUPON_REMOVED)==null) {
                                        Cookie co = new Cookie(UserInterceptor.DEAL_COUPON_REMOVED, "true");
                                        co.setDomain(this.domainName);
                                        co.setMaxAge(-1);
                                        co.setPath("/");
                                        cookiesMap.put(UserInterceptor.DEAL_COUPON_REMOVED, co);
                        }
                }
                userServiceClient = new UserClient();
                UserContextService.Client userClient = userServiceClient.getClient();
                userClient.removeCoupon(cartId);
//                DataLogger.logData(EventType.COUPON_REMOVED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
            }
        } catch (PromotionException e) {
            log.info("Invalid coupon: " + e.getMessage());
            addActionError(e.getMessage());
        } catch (Exception e) {
            log.error("Unable to apply or remove coupon", e);
        }
        return "redirect";
    }
}