Subversion Repositories SmartDukaan

Rev

Rev 4839 | Rev 11854 | Go to most recent revision | 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.thrift.clients.PromotionClient;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.DataLogger;

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";
                }
                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();
                }
                addActionError(str);
                DataLogger.logData(EventType.COUPON_APPLIED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
            }
            else if (action.equals("removecoupon"))    {
                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";
    }
}