Subversion Repositories SmartDukaan

Rev

Rev 11851 | Rev 11980 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1981 varun.gupt 1
package in.shop2020.serving.controllers;
2
 
2752 varun.gupt 3
import in.shop2020.datalogger.EventType;
11823 amit.gupta 4
import in.shop2020.model.v1.user.Cart;
1981 varun.gupt 5
import in.shop2020.model.v1.user.PromotionException;
3224 vikas 6
import in.shop2020.model.v1.user.PromotionService;
1981 varun.gupt 7
import in.shop2020.model.v1.user.UserContextService;
3224 vikas 8
import in.shop2020.thrift.clients.PromotionClient;
3126 rajveer 9
import in.shop2020.thrift.clients.UserClient;
3224 vikas 10
import in.shop2020.utils.DataLogger;
1981 varun.gupt 11
 
3224 vikas 12
import org.apache.log4j.Logger;
4839 varun.gupt 13
import org.apache.struts2.convention.annotation.InterceptorRef;
14
import org.apache.struts2.convention.annotation.InterceptorRefs;
3224 vikas 15
import org.apache.struts2.convention.annotation.Result;
16
import org.apache.struts2.convention.annotation.Results;
17
 
2145 chandransh 18
@SuppressWarnings("serial")
4839 varun.gupt 19
 
20
@InterceptorRefs({
21
    @InterceptorRef("myDefault")
22
})
23
 
1981 varun.gupt 24
@Results({
25
    @Result(name = "redirect", type = "redirectAction", params = {"actionName" , "cart"})
26
})
27
public class PromotionController extends BaseController {
28
 
29
    public PromotionController() {
30
        super();
31
    }
32
    private static Logger log = Logger.getLogger(Class.class);
33
 
34
    public String create()  {
35
        String action = request.getParameter("action");
36
        log.info("Action is: " + action);
37
 
3126 rajveer 38
        UserClient userServiceClient = null;
39
        PromotionClient promotionServiceClient = null;
1981 varun.gupt 40
 
41
        try {
42
            long cartId = userinfo.getCartId();
43
 
44
            if(action == null || action.isEmpty())  {
45
                addActionError("Invalid Request Action");
46
                return "redirect";
47
            }
2752 varun.gupt 48
            String couponCode = request.getParameter("coupon_code");
49
 
4839 varun.gupt 50
            if (action.equalsIgnoreCase("applycoupon"))   {
1981 varun.gupt 51
                if (couponCode == null || couponCode.isEmpty()) {
52
                    addActionError("Coupon Code field cannot be left empty");
53
                    return "redirect";
54
                }
11823 amit.gupta 55
                promotionServiceClient = new PromotionClient();
56
                PromotionService.Client promotionClient = promotionServiceClient.getClient();
57
                String str = "Coupon applied";
58
                Cart cart = promotionClient.applyCoupon(couponCode, cartId);
11854 amit.gupta 59
                if(cart.getMessage() != null && !cart.getMessage().equals("")){
11823 amit.gupta 60
                	str = str +  ", " + cart.getMessage();
4839 varun.gupt 61
                }
11851 amit.gupta 62
                addActionMessage(str);
11823 amit.gupta 63
                DataLogger.logData(EventType.COUPON_APPLIED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
1981 varun.gupt 64
            }
65
            else if (action.equals("removecoupon"))    {
3126 rajveer 66
                userServiceClient = new UserClient();
1981 varun.gupt 67
                UserContextService.Client userClient = userServiceClient.getClient();
68
                userClient.removeCoupon(cartId);
3224 vikas 69
                DataLogger.logData(EventType.COUPON_REMOVED, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), couponCode);
1981 varun.gupt 70
            }
71
        } catch (PromotionException e) {
3171 rajveer 72
            log.info("Invalid coupon: " + e.getMessage());
1981 varun.gupt 73
            addActionError(e.getMessage());
74
        } catch (Exception e) {
2949 chandransh 75
            log.error("Unable to apply or remove coupon", e);
1981 varun.gupt 76
        }
77
        return "redirect";
78
    }
79
}