Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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;
6679 anupam.sin 13
import java.util.HashMap;
6497 amit.gupta 14
import java.util.List;
6679 anupam.sin 15
import java.util.Map;
6497 amit.gupta 16
 
17
import javax.servlet.ServletContext;
18
import javax.servlet.http.HttpServletRequest;
19
import javax.servlet.http.HttpServletResponse;
20
import javax.servlet.http.HttpSession;
21
 
6500 amit.gupta 22
import org.apache.commons.lang.StringUtils;
6497 amit.gupta 23
import org.apache.struts2.convention.annotation.InterceptorRef;
24
import org.apache.struts2.convention.annotation.InterceptorRefs;
25
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.interceptor.ServletRequestAware;
28
import org.apache.struts2.util.ServletContextAware;
29
import org.apache.thrift.TException;
30
import org.json.JSONException;
31
import org.json.JSONObject;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
 
35
 
36
@InterceptorRefs({
37
    @InterceptorRef("defaultStack"),
38
    @InterceptorRef("login")
39
})
40
 
41
@Results({
42
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"}),
43
	@Result(name="redirect", type="redirectAction", location = "coupon-gvs",  params={"statusCode", "301"})
44
})
45
public class CouponGvsController implements ServletRequestAware, ServletContextAware {
46
 
47
    private static Logger logger = LoggerFactory.getLogger(CouponGvsController.class);
48
 
49
    private HttpServletRequest request;
50
    private HttpServletResponse response;
51
    private HttpSession session;
52
    private ServletContext context;
53
 
54
    private PromotionClient psc;
55
    private in.shop2020.model.v1.user.PromotionService.Client pClient;
56
 
57
    private final DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
58
 
59
 
60
 
6679 anupam.sin 61
    private String discountType;
62
    private Long discount;
63
    private String couponType;
6497 amit.gupta 64
	private String coupon;
65
    private long type;
66
    private String userEmail;
6679 anupam.sin 67
 
68
    private Long userLimit;
69
    private Long globalLimit;
70
    private Long maxDiscount;
71
    private Long minDiscountableVal;
72
    private Long startHour;
73
    private Long startMinute;
74
    private Long endHour;
75
    private Long endMinute;
6497 amit.gupta 76
 
77
    public CouponGvsController(){
78
	    try {
79
            psc = new PromotionClient();
80
            pClient = psc.getClient();
81
        } catch (Exception e) {
82
            logger.error("Error connecting to promotion service", e);
83
        }
84
	}
85
 
86
 
87
	public String index() {
88
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
89
            return "authfail";
90
        }
91
        return "authsuccess";
92
 
93
	}
94
 
95
	public String create(){
96
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
97
			return "authfail";
98
		}
6679 anupam.sin 99
		//Coupon would be valid till 2 months of creation.
6497 amit.gupta 100
		Calendar cal = Calendar.getInstance();
101
		cal.add(Calendar.DATE, 60);
102
		Date endOn = cal.getTime();
6679 anupam.sin 103
		String arguments = "{";
104
		if(!(StringUtils.isEmpty(userEmail) || discount == null || discount <= 0) ) {
6500 amit.gupta 105
			try {
6679 anupam.sin 106
 
107
			    arguments = arguments + "\"endOn\":" + String.valueOf(endOn.getTime());
108
 
109
			    if(userEmail.equals("*")) {
110
			        arguments = arguments + ", \"emails\":\"*\"";
111
			    } else {
112
			        arguments = arguments + ", \"emails\":[\"" + userEmail + "\"]";
113
			    }
114
 
115
			    arguments = arguments + ", \"couponType\":\"" + couponType + "\"";
116
 
117
			    arguments = arguments + ", \"discountType\":\"" + discountType + "\"";
118
 
119
			    arguments = arguments + ", \"discount\":" + discount.toString();
120
 
121
			    if(userLimit == null || userLimit < 1) {
122
			        arguments = arguments + ", \"usage_limit_for_user\":1";
123
			    } else {
124
			        arguments = arguments + ", \"usage_limit_for_user\":" + userLimit.toString();
125
			    }
126
 
127
			    if(globalLimit == null || globalLimit < 1) {
128
                    arguments = arguments + ", \"globalLimit\":1";
129
                } else {
130
                    arguments = arguments + ", \"globalLimit\":" + globalLimit.toString();
131
                }
132
 
133
			    if(maxDiscount == null || maxDiscount < 1) {
134
                    //Do not add this option
135
			        ;
136
                } else {
137
                    arguments = arguments + ", \"maxDiscount\":" + maxDiscount.toString();
138
                }
139
 
140
			    if(minDiscountableVal == null || minDiscountableVal < 1) {
141
                    //Do not add this option
142
                    ;
143
                } else {
144
                    arguments = arguments + ", \"minDiscountableVal\":" + minDiscountableVal.toString();
145
                }
146
 
147
			    if(startHour == null || startHour < 1 || startHour > 23 || startHour > endHour) {
148
                    //Do not add this option
149
                    ;
150
                } else {
151
                    arguments = arguments + ", \"startHour\":" + startHour.toString();
152
                }
153
 
154
			    if(startMinute == null || startMinute < 1 || startMinute < 59) {
155
                    //Do not add this option
156
                    ;
157
                } else {
158
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
159
                }
160
 
161
			    if(endHour == null || endHour < 1 || endHour > 23) {
162
                    //Do not add this option
163
                    ;
164
                } else {
165
                    arguments = arguments + ", \"endHour\":" + endHour.toString();
166
                }
167
 
168
			    if(endMinute == null || endMinute < 1 || endMinute < 59) {
169
                    //Do not add this option
170
                    ;
171
                } else {
172
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
173
                }
174
 
175
			    arguments = arguments + ", \"isCod\":False}";
176
 
177
				coupon = pClient.createCoupon(type, arguments, false, null);
6500 amit.gupta 178
			} catch (Exception e){
179
				return "authsuccess";
180
			}
6497 amit.gupta 181
		}
182
		return "authsuccess";	
183
	}
184
 
185
 
186
	@Override
187
	public void setServletContext(ServletContext context) {
188
		this.context= context;
189
	}
190
 
191
 
192
	@Override
193
	public void setServletRequest(HttpServletRequest req) {
194
	       this.request = req;
195
	        this.session = req.getSession();
196
	}
197
 
198
	//Crea
199
	public String getCoupon() {
200
		return coupon;
201
 	}
202
 
203
 
204
	public void setCoupon(String coupon) {
205
		this.coupon = coupon;
206
	}
207
 
208
 
209
	public long getType() {
210
		return type;
211
	}
212
 
213
 
214
	public void setType(long type) {
215
		this.type = type;
216
	}
217
 
218
	public String getUserEmail() {
219
		return userEmail;
220
	}
221
 
222
	public void setUserEmail(String userEmail) {
223
		this.userEmail = userEmail;
224
	}
225
 
226
	public List<Coupon> getCoupons() {
227
		try {
228
			return pClient.getActiveCodes(27l);
229
		} catch (PromotionException e) {
230
			// TODO Auto-generated catch block
231
			e.printStackTrace();
232
		} catch (TException e) {
233
			// TODO Auto-generated catch block
234
			e.printStackTrace();
235
		}
236
		return null;
237
	}
238
 
239
	public List<String> getArguments(Coupon coupon){
240
		List<String> result = new ArrayList<String>();
241
		try {
242
			JSONObject obj = new JSONObject(coupon.getArguments());
243
			result.add(obj.getJSONArray("emails").getString(0));
244
			result.add(obj.getString("discount"));
245
		} catch (JSONException e) {
246
			// TODO Auto-generated catch block
247
			e.printStackTrace();
248
		}
249
		return result;
250
	}
251
 
252
	public void setId(String id) {
253
		this.coupon = id;
254
	}
255
 
256
	public String destroy(){
257
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath().split("/")[1])) {
258
			return "athfail";
259
		}
260
		try {
261
			pClient.deleteCoupon(coupon);
262
		} catch (PromotionException e) {
263
			// TODO Auto-generated catch block
264
			e.printStackTrace();
265
		} catch (TException e) {
266
			// TODO Auto-generated catch block
267
			e.printStackTrace();
268
		}
269
		return "redirect"; 
270
	}
271
	public String show(){
272
		return destroy(); 
273
	}
6574 amit.gupta 274
 
275
 
276
    public String getServletContextPath() {
277
        return context.getContextPath();
278
    }
6679 anupam.sin 279
 
280
 
281
    public String getDiscountType() {
282
        return discountType;
283
    }
284
 
285
 
286
    public void setDiscountType(String discountType) {
287
        this.discountType = discountType;
288
    }
289
 
290
 
291
    public Long getDiscount() {
292
        return discount;
293
    }
294
 
295
 
296
    public void setDiscount(Long discount) {
297
        this.discount = discount;
298
    }
299
 
300
 
301
    public String getCouponType() {
302
        return couponType;
303
    }
304
 
305
 
306
    public void setCouponType(String couponType) {
307
        this.couponType = couponType;
308
    }
309
 
310
 
311
    public Long getUserLimit() {
312
        return userLimit;
313
    }
314
 
315
 
316
    public void setUserLimit(Long userLimit) {
317
        this.userLimit = userLimit;
318
    }
319
 
320
 
321
    public Long getGlobalLimit() {
322
        return globalLimit;
323
    }
324
 
325
 
326
    public void setGlobalLimit(Long globalLimit) {
327
        this.globalLimit = globalLimit;
328
    }
329
 
330
 
331
    public Long getMaxDiscount() {
332
        return maxDiscount;
333
    }
334
 
335
 
336
    public void setMaxDiscount(Long maxDiscount) {
337
        this.maxDiscount = maxDiscount;
338
    }
339
 
340
 
341
    public Long getMinDiscountableVal() {
342
        return minDiscountableVal;
343
    }
344
 
345
 
346
    public void setMinDiscountableVal(Long minDiscountableVal) {
347
        this.minDiscountableVal = minDiscountableVal;
348
    }
349
 
350
 
351
    public Long getStartHour() {
352
        return startHour;
353
    }
354
 
355
 
356
    public void setStartHour(Long startHour) {
357
        this.startHour = startHour;
358
    }
359
 
360
 
361
    public Long getStartMinute() {
362
        return startMinute;
363
    }
364
 
365
 
366
    public void setStartMinute(Long startMinute) {
367
        this.startMinute = startMinute;
368
    }
369
 
370
 
371
    public Long getEndHour() {
372
        return endHour;
373
    }
374
 
375
 
376
    public void setEndHour(Long endHour) {
377
        this.endHour = endHour;
378
    }
379
 
380
 
381
    public Long getEndMinute() {
382
        return endMinute;
383
    }
384
 
385
 
386
    public void setEndMinute(Long endMinute) {
387
        this.endMinute = endMinute;
388
    }
6497 amit.gupta 389
}