Subversion Repositories SmartDukaan

Rev

Rev 3389 | Rev 5148 | Go to most recent revision | Details | Compare with Previous | 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;
3936 chandransh 18
import org.apache.struts2.convention.annotation.Result;
19
import org.apache.struts2.convention.annotation.Results;
3389 varun.gupt 20
import org.apache.struts2.interceptor.ServletRequestAware;
21
import org.json.JSONObject;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24
 
25
@InterceptorRefs({
26
    @InterceptorRef("defaultStack"),
27
    @InterceptorRef("login")
28
})
3936 chandransh 29
@Results({
30
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
31
})
3389 varun.gupt 32
public class PromotionsController implements ServletRequestAware {
33
 
34
	private static Logger log = LoggerFactory.getLogger(PromotionsController.class);
35
 
36
	private HttpServletRequest request;
37
	private HttpSession session;
38
	private Client promotionClient;
39
	private UserContextService.Client userClient;
40
 
41
	private List<CouponInfo> coupons;
42
 
43
	@Override
44
	public void setServletRequest(HttpServletRequest req) {
45
		this.request = req;
46
		this.session = req.getSession();
47
	}
48
 
49
	public String index() {
50
        if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))	{
3936 chandransh 51
            return "authfail";
3389 varun.gupt 52
        }
53
		try {
54
			PromotionClient promotionServiceClient = new PromotionClient();
55
			promotionClient = promotionServiceClient.getClient();
56
 
57
			UserClient userServiceClient = new UserClient();
58
			userClient = userServiceClient.getClient();
59
 
60
			coupons = new ArrayList<CouponInfo>();
61
			CouponInfo couponInfo;
62
 
63
			for (Coupon coupon: promotionClient.getActiveCoupons())	{
64
				couponInfo = new CouponInfo(coupon.getCouponCode());
65
				couponInfo.setPaymentsCount(promotionClient.getSuccessfulPaymentCountForCoupon(coupon.getCouponCode()));
66
				couponInfo.setCartsCount(userClient.getCartsWithCouponCount(coupon.getCouponCode()));
67
 
68
				String args = coupon.getArguments();
69
				String description;
70
 
71
				if(args == null)	{
72
					description = promotionClient.getRuleDocString(coupon.getPromotion().getRuleExecutionSrc());
73
					couponInfo.setDescription("<pre>" + description + "</pre>");
74
 
75
				} else	{
76
					JSONObject argsJSON = new JSONObject(args);
77
 
78
					description = "Rs." + argsJSON.getString("discount");
79
					description += " off on " + argsJSON.getString("handset_display_name");
80
					description += " (Max " + argsJSON.getString("usage_limit") + " uses)";
81
 
82
					couponInfo.setDescription("<i>" + description + "</i>");
83
				}
84
				coupons.add(couponInfo);
85
			}
86
		} catch (Exception e) {
87
			log.error(e.getStackTrace().toString());
88
		}
89
		return "index";
90
	}
91
 
92
	public List<CouponInfo> getActiveCoupons()	{
93
		return coupons;
94
	}
95
 
96
	public static class CouponInfo {
97
		private String code;
98
		private long paymentsCount;
99
		private long cartsCount;
100
		private String description;
101
 
102
		public CouponInfo(String code)	{
103
			this.code = code;
104
			paymentsCount = -1;
105
			cartsCount = -1;
106
			description = null;
107
		}
108
 
109
		public void setPaymentsCount(long paymentsCount)	{
110
			this.paymentsCount = paymentsCount;
111
		}
112
 
113
		public void setCartsCount(long cartsCount)	{
114
			this.cartsCount = cartsCount;
115
		}
116
 
117
		public void setDescription(String desc)	{
118
			description = desc;
119
		}
120
 
121
	 	public String getCode()	{
122
			return this.code;
123
		}
124
 
125
		public long getPaymentsCount()	{
126
			return this.paymentsCount;
127
		}
128
 
129
		public long getCartsCount()	{
130
			return this.cartsCount;
131
		}
132
 
133
		public String getDescription()	{
134
			return description;
135
		}
136
 
137
		public String toString()	{
138
			return code + " " + paymentsCount + " " + cartsCount + " " + description;
139
		}
140
	}
141
}