| 6497 |
amit.gupta |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.user.Coupon;
|
|
|
4 |
import in.shop2020.model.v1.user.PromotionException;
|
|
|
5 |
import in.shop2020.support.utils.ReportsUtils;
|
|
|
6 |
import in.shop2020.thrift.clients.PromotionClient;
|
|
|
7 |
|
|
|
8 |
import java.text.DateFormat;
|
|
|
9 |
import java.text.SimpleDateFormat;
|
|
|
10 |
import java.util.ArrayList;
|
|
|
11 |
import java.util.Calendar;
|
|
|
12 |
import java.util.Date;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
|
|
|
15 |
import javax.servlet.ServletContext;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
|
|
18 |
import javax.servlet.http.HttpSession;
|
|
|
19 |
|
| 6500 |
amit.gupta |
20 |
import org.apache.commons.lang.StringUtils;
|
| 6497 |
amit.gupta |
21 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
22 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
23 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
24 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
25 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
26 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
27 |
import org.apache.thrift.TException;
|
|
|
28 |
import org.json.JSONException;
|
|
|
29 |
import org.json.JSONObject;
|
|
|
30 |
import org.slf4j.Logger;
|
|
|
31 |
import org.slf4j.LoggerFactory;
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
@InterceptorRefs({
|
|
|
35 |
@InterceptorRef("defaultStack"),
|
|
|
36 |
@InterceptorRef("login")
|
|
|
37 |
})
|
|
|
38 |
|
|
|
39 |
@Results({
|
|
|
40 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"}),
|
|
|
41 |
@Result(name="redirect", type="redirectAction", location = "coupon-gvs", params={"statusCode", "301"})
|
|
|
42 |
})
|
|
|
43 |
public class CouponGvsController implements ServletRequestAware, ServletContextAware {
|
|
|
44 |
|
|
|
45 |
private static Logger logger = LoggerFactory.getLogger(CouponGvsController.class);
|
|
|
46 |
|
|
|
47 |
private HttpServletRequest request;
|
|
|
48 |
private HttpServletResponse response;
|
|
|
49 |
private HttpSession session;
|
|
|
50 |
private ServletContext context;
|
|
|
51 |
|
|
|
52 |
private PromotionClient psc;
|
|
|
53 |
private in.shop2020.model.v1.user.PromotionService.Client pClient;
|
|
|
54 |
|
|
|
55 |
private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
private String coupon;
|
|
|
61 |
private long type;
|
|
|
62 |
private Long amount;
|
|
|
63 |
private String userEmail;
|
|
|
64 |
|
|
|
65 |
public CouponGvsController(){
|
|
|
66 |
try {
|
|
|
67 |
psc = new PromotionClient();
|
|
|
68 |
pClient = psc.getClient();
|
|
|
69 |
} catch (Exception e) {
|
|
|
70 |
logger.error("Error connecting to promotion service", e);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
public String index() {
|
|
|
76 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
77 |
return "authfail";
|
|
|
78 |
}
|
|
|
79 |
return "authsuccess";
|
|
|
80 |
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public String create(){
|
|
|
84 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
85 |
return "authfail";
|
|
|
86 |
}
|
|
|
87 |
//Coupon would we valid till 2 months of cretaion.
|
|
|
88 |
Calendar cal = Calendar.getInstance();
|
|
|
89 |
cal.add(Calendar.DATE, 60);
|
|
|
90 |
Date endOn = cal.getTime();
|
| 6500 |
amit.gupta |
91 |
if(!(StringUtils.isEmpty(userEmail) || amount == null) ) {
|
|
|
92 |
try {
|
|
|
93 |
coupon = pClient.createCoupon(type, endOn.getTime(), userEmail, amount, false, 1);
|
|
|
94 |
} catch (Exception e){
|
|
|
95 |
return "authsuccess";
|
|
|
96 |
}
|
| 6497 |
amit.gupta |
97 |
}
|
|
|
98 |
return "authsuccess";
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
@Override
|
|
|
103 |
public void setServletContext(ServletContext context) {
|
|
|
104 |
this.context= context;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
@Override
|
|
|
109 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
110 |
this.request = req;
|
|
|
111 |
this.session = req.getSession();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
//Crea
|
|
|
115 |
public String getCoupon() {
|
|
|
116 |
return coupon;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
public void setCoupon(String coupon) {
|
|
|
121 |
this.coupon = coupon;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
public long getType() {
|
|
|
126 |
return type;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
public void setType(long type) {
|
|
|
131 |
this.type = type;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
public Long getAmount() {
|
|
|
136 |
return amount;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
public void setAmount(Long amount) {
|
|
|
141 |
this.amount = amount;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
public String getUserEmail() {
|
|
|
146 |
return userEmail;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public void setUserEmail(String userEmail) {
|
|
|
150 |
this.userEmail = userEmail;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public List<Coupon> getCoupons() {
|
|
|
154 |
try {
|
|
|
155 |
return pClient.getActiveCodes(27l);
|
|
|
156 |
} catch (PromotionException e) {
|
|
|
157 |
// TODO Auto-generated catch block
|
|
|
158 |
e.printStackTrace();
|
|
|
159 |
} catch (TException e) {
|
|
|
160 |
// TODO Auto-generated catch block
|
|
|
161 |
e.printStackTrace();
|
|
|
162 |
}
|
|
|
163 |
return null;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public List<String> getArguments(Coupon coupon){
|
|
|
167 |
List<String> result = new ArrayList<String>();
|
|
|
168 |
try {
|
|
|
169 |
JSONObject obj = new JSONObject(coupon.getArguments());
|
|
|
170 |
result.add(obj.getJSONArray("emails").getString(0));
|
|
|
171 |
result.add(obj.getString("discount"));
|
|
|
172 |
} catch (JSONException e) {
|
|
|
173 |
// TODO Auto-generated catch block
|
|
|
174 |
e.printStackTrace();
|
|
|
175 |
}
|
|
|
176 |
return result;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
public void setId(String id) {
|
|
|
180 |
this.coupon = id;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
public String destroy(){
|
|
|
184 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath().split("/")[1])) {
|
|
|
185 |
return "athfail";
|
|
|
186 |
}
|
|
|
187 |
try {
|
|
|
188 |
pClient.deleteCoupon(coupon);
|
|
|
189 |
} catch (PromotionException e) {
|
|
|
190 |
// TODO Auto-generated catch block
|
|
|
191 |
e.printStackTrace();
|
|
|
192 |
} catch (TException e) {
|
|
|
193 |
// TODO Auto-generated catch block
|
|
|
194 |
e.printStackTrace();
|
|
|
195 |
}
|
|
|
196 |
return "redirect";
|
|
|
197 |
}
|
|
|
198 |
public String show(){
|
|
|
199 |
return destroy();
|
|
|
200 |
}
|
|
|
201 |
}
|