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