Subversion Repositories SmartDukaan

Rev

Rev 3936 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3389 varun.gupt 1
package in.shop2020.support.controllers;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import in.shop2020.model.v1.user.UserContextService;
7
import in.shop2020.model.v1.user.PromotionService.Client;
8
import in.shop2020.model.v1.user.Coupon;
9
import in.shop2020.support.utils.ReportsUtils;
10
import in.shop2020.thrift.clients.PromotionClient;
11
import in.shop2020.thrift.clients.UserClient;
12
 
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpSession;
15
 
16
import org.apache.struts2.convention.annotation.InterceptorRef;
17
import org.apache.struts2.convention.annotation.InterceptorRefs;
18
import org.apache.struts2.interceptor.ServletRequestAware;
19
import org.json.JSONObject;
20
import org.slf4j.Logger;
21
import org.slf4j.LoggerFactory;
22
 
23
@InterceptorRefs({
24
    @InterceptorRef("defaultStack"),
25
    @InterceptorRef("login")
26
})
27
public class PromotionsController implements ServletRequestAware {
28
 
29
	private static Logger log = LoggerFactory.getLogger(PromotionsController.class);
30
 
31
	private HttpServletRequest request;
32
	private HttpSession session;
33
	private Client promotionClient;
34
	private UserContextService.Client userClient;
35
 
36
	private List<CouponInfo> coupons;
37
 
38
	@Override
39
	public void setServletRequest(HttpServletRequest req) {
40
		this.request = req;
41
		this.session = req.getSession();
42
	}
43
 
44
	public String index() {
45
        if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))	{
46
            return "exception";
47
        }
48
		try {
49
			PromotionClient promotionServiceClient = new PromotionClient();
50
			promotionClient = promotionServiceClient.getClient();
51
 
52
			UserClient userServiceClient = new UserClient();
53
			userClient = userServiceClient.getClient();
54
 
55
			coupons = new ArrayList<CouponInfo>();
56
			CouponInfo couponInfo;
57
 
58
			for (Coupon coupon: promotionClient.getActiveCoupons())	{
59
				couponInfo = new CouponInfo(coupon.getCouponCode());
60
				couponInfo.setPaymentsCount(promotionClient.getSuccessfulPaymentCountForCoupon(coupon.getCouponCode()));
61
				couponInfo.setCartsCount(userClient.getCartsWithCouponCount(coupon.getCouponCode()));
62
 
63
				String args = coupon.getArguments();
64
				String description;
65
 
66
				if(args == null)	{
67
					description = promotionClient.getRuleDocString(coupon.getPromotion().getRuleExecutionSrc());
68
					couponInfo.setDescription("<pre>" + description + "</pre>");
69
 
70
				} else	{
71
					JSONObject argsJSON = new JSONObject(args);
72
 
73
					description = "Rs." + argsJSON.getString("discount");
74
					description += " off on " + argsJSON.getString("handset_display_name");
75
					description += " (Max " + argsJSON.getString("usage_limit") + " uses)";
76
 
77
					couponInfo.setDescription("<i>" + description + "</i>");
78
				}
79
				coupons.add(couponInfo);
80
			}
81
		} catch (Exception e) {
82
			log.error(e.getStackTrace().toString());
83
		}
84
		return "index";
85
	}
86
 
87
	public List<CouponInfo> getActiveCoupons()	{
88
		return coupons;
89
	}
90
 
91
	public static class CouponInfo {
92
		private String code;
93
		private long paymentsCount;
94
		private long cartsCount;
95
		private String description;
96
 
97
		public CouponInfo(String code)	{
98
			this.code = code;
99
			paymentsCount = -1;
100
			cartsCount = -1;
101
			description = null;
102
		}
103
 
104
		public void setPaymentsCount(long paymentsCount)	{
105
			this.paymentsCount = paymentsCount;
106
		}
107
 
108
		public void setCartsCount(long cartsCount)	{
109
			this.cartsCount = cartsCount;
110
		}
111
 
112
		public void setDescription(String desc)	{
113
			description = desc;
114
		}
115
 
116
	 	public String getCode()	{
117
			return this.code;
118
		}
119
 
120
		public long getPaymentsCount()	{
121
			return this.paymentsCount;
122
		}
123
 
124
		public long getCartsCount()	{
125
			return this.cartsCount;
126
		}
127
 
128
		public String getDescription()	{
129
			return description;
130
		}
131
 
132
		public String toString()	{
133
			return code + " " + paymentsCount + " " + cartsCount + " " + description;
134
		}
135
	}
136
}