Subversion Repositories SmartDukaan

Rev

Rev 6730 | Rev 8707 | 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;
6730 anupam.sin 76
 
77
    private String couponCode;
7563 anupam.sin 78
 
79
    private int isCod;
6497 amit.gupta 80
 
81
    public CouponGvsController(){
82
	    try {
83
            psc = new PromotionClient();
84
            pClient = psc.getClient();
85
        } catch (Exception e) {
86
            logger.error("Error connecting to promotion service", e);
87
        }
88
	}
89
 
90
 
91
	public String index() {
92
        if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
93
            return "authfail";
94
        }
95
        return "authsuccess";
96
 
97
	}
98
 
99
	public String create(){
100
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
101
			return "authfail";
102
		}
6679 anupam.sin 103
		//Coupon would be valid till 2 months of creation.
6497 amit.gupta 104
		Calendar cal = Calendar.getInstance();
105
		cal.add(Calendar.DATE, 60);
106
		Date endOn = cal.getTime();
6679 anupam.sin 107
		String arguments = "{";
108
		if(!(StringUtils.isEmpty(userEmail) || discount == null || discount <= 0) ) {
6500 amit.gupta 109
			try {
6679 anupam.sin 110
 
111
			    arguments = arguments + "\"endOn\":" + String.valueOf(endOn.getTime());
112
 
113
			    if(userEmail.equals("*")) {
114
			        arguments = arguments + ", \"emails\":\"*\"";
115
			    } else {
6680 anupam.sin 116
			        String[] emails = userEmail.split(",");
117
			        arguments = arguments + ", \"emails\":[";
118
			        int i = 0;
119
			        for (String email : emails) {
120
			            if(i == 0) {
121
			                arguments = arguments + "\"" + email.trim() + "\"";
122
			                i = 1;
123
			            } else {
124
			                arguments = arguments + ",\"" + email.trim() + "\"";
125
			            }
126
			        }
127
			        arguments = arguments + "]";
6679 anupam.sin 128
			    }
129
 
130
			    arguments = arguments + ", \"couponType\":\"" + couponType + "\"";
131
 
132
			    arguments = arguments + ", \"discountType\":\"" + discountType + "\"";
133
 
134
			    arguments = arguments + ", \"discount\":" + discount.toString();
135
 
136
			    if(userLimit == null || userLimit < 1) {
137
			        arguments = arguments + ", \"usage_limit_for_user\":1";
138
			    } else {
139
			        arguments = arguments + ", \"usage_limit_for_user\":" + userLimit.toString();
140
			    }
141
 
142
			    if(globalLimit == null || globalLimit < 1) {
6684 anupam.sin 143
			        //Do not add this option
144
                    ;
6679 anupam.sin 145
                } else {
146
                    arguments = arguments + ", \"globalLimit\":" + globalLimit.toString();
147
                }
148
 
149
			    if(maxDiscount == null || maxDiscount < 1) {
150
                    //Do not add this option
151
			        ;
152
                } else {
153
                    arguments = arguments + ", \"maxDiscount\":" + maxDiscount.toString();
154
                }
155
 
156
			    if(minDiscountableVal == null || minDiscountableVal < 1) {
157
                    //Do not add this option
158
                    ;
159
                } else {
160
                    arguments = arguments + ", \"minDiscountableVal\":" + minDiscountableVal.toString();
161
                }
162
 
163
			    if(startHour == null || startHour < 1 || startHour > 23 || startHour > endHour) {
164
                    //Do not add this option
165
                    ;
166
                } else {
167
                    arguments = arguments + ", \"startHour\":" + startHour.toString();
168
                }
169
 
170
			    if(startMinute == null || startMinute < 1 || startMinute < 59) {
171
                    //Do not add this option
172
                    ;
173
                } else {
174
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
175
                }
176
 
177
			    if(endHour == null || endHour < 1 || endHour > 23) {
178
                    //Do not add this option
179
                    ;
180
                } else {
181
                    arguments = arguments + ", \"endHour\":" + endHour.toString();
182
                }
183
 
184
			    if(endMinute == null || endMinute < 1 || endMinute < 59) {
185
                    //Do not add this option
186
                    ;
187
                } else {
188
                    arguments = arguments + ", \"startMinute\":" + startMinute.toString();
189
                }
190
 
7563 anupam.sin 191
			    if(isCod == 1) {
192
			        arguments = arguments + ", \"isCod\":True}";
193
			    } else {
194
			        arguments = arguments + ", \"isCod\":False}";
195
			    }
6679 anupam.sin 196
 
6730 anupam.sin 197
			    if(couponCode == null) {
198
			        couponCode = "";
199
			    }
200
 
201
				coupon = pClient.createCoupon(type, couponCode, arguments, false, null);
6500 amit.gupta 202
			} catch (Exception e){
203
				return "authsuccess";
204
			}
6497 amit.gupta 205
		}
206
		return "authsuccess";	
207
	}
208
 
209
 
210
	@Override
211
	public void setServletContext(ServletContext context) {
212
		this.context= context;
213
	}
214
 
215
 
216
	@Override
217
	public void setServletRequest(HttpServletRequest req) {
218
	       this.request = req;
219
	        this.session = req.getSession();
220
	}
221
 
222
	//Crea
223
	public String getCoupon() {
224
		return coupon;
225
 	}
226
 
227
 
228
	public void setCoupon(String coupon) {
229
		this.coupon = coupon;
230
	}
231
 
232
 
233
	public long getType() {
234
		return type;
235
	}
236
 
237
 
238
	public void setType(long type) {
239
		this.type = type;
240
	}
241
 
242
	public String getUserEmail() {
243
		return userEmail;
244
	}
245
 
246
	public void setUserEmail(String userEmail) {
247
		this.userEmail = userEmail;
248
	}
249
 
250
	public List<Coupon> getCoupons() {
251
		try {
252
			return pClient.getActiveCodes(27l);
253
		} catch (PromotionException e) {
254
			// TODO Auto-generated catch block
255
			e.printStackTrace();
256
		} catch (TException e) {
257
			// TODO Auto-generated catch block
258
			e.printStackTrace();
259
		}
260
		return null;
261
	}
262
 
263
	public List<String> getArguments(Coupon coupon){
264
		List<String> result = new ArrayList<String>();
265
		try {
266
			JSONObject obj = new JSONObject(coupon.getArguments());
6727 anupam.sin 267
			result.add(obj.getString("emails"));
6730 anupam.sin 268
			try {
269
    			if(obj.getString("discountType").equals("absolute")) {
270
    			    result.add(obj.getString("discount"));
271
    			} else {
272
    			    result.add(obj.getString("discount") + " %");
273
    			}
274
    		} catch (JSONException e){
275
    		    result.add(obj.getString("discount"));
276
    		}
277
 
278
    		try {
279
    			result.add(obj.getString("couponType"));
280
    		} catch (JSONException e){
281
    		    result.add("Physical");
282
    		}
283
 
6497 amit.gupta 284
		} catch (JSONException e) {
6730 anupam.sin 285
			result.add("ERROR");
286
			result.add("ERROR");
287
			result.add("ERROR");
6497 amit.gupta 288
		}
289
		return result;
290
	}
291
 
292
	public void setId(String id) {
293
		this.coupon = id;
294
	}
295
 
296
	public String destroy(){
297
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath().split("/")[1])) {
298
			return "athfail";
299
		}
300
		try {
301
			pClient.deleteCoupon(coupon);
302
		} catch (PromotionException e) {
303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
305
		} catch (TException e) {
306
			// TODO Auto-generated catch block
307
			e.printStackTrace();
308
		}
309
		return "redirect"; 
310
	}
311
	public String show(){
312
		return destroy(); 
313
	}
6574 amit.gupta 314
 
315
 
316
    public String getServletContextPath() {
317
        return context.getContextPath();
318
    }
6679 anupam.sin 319
 
320
 
321
    public String getDiscountType() {
322
        return discountType;
323
    }
324
 
325
 
326
    public void setDiscountType(String discountType) {
327
        this.discountType = discountType;
328
    }
329
 
330
 
331
    public Long getDiscount() {
332
        return discount;
333
    }
334
 
335
 
336
    public void setDiscount(Long discount) {
337
        this.discount = discount;
338
    }
339
 
340
 
341
    public String getCouponType() {
342
        return couponType;
343
    }
344
 
345
 
346
    public void setCouponType(String couponType) {
347
        this.couponType = couponType;
348
    }
349
 
350
 
351
    public Long getUserLimit() {
352
        return userLimit;
353
    }
354
 
355
 
356
    public void setUserLimit(Long userLimit) {
357
        this.userLimit = userLimit;
358
    }
359
 
360
 
361
    public Long getGlobalLimit() {
362
        return globalLimit;
363
    }
364
 
365
 
366
    public void setGlobalLimit(Long globalLimit) {
367
        this.globalLimit = globalLimit;
368
    }
369
 
370
 
371
    public Long getMaxDiscount() {
372
        return maxDiscount;
373
    }
374
 
375
 
376
    public void setMaxDiscount(Long maxDiscount) {
377
        this.maxDiscount = maxDiscount;
378
    }
379
 
380
 
381
    public Long getMinDiscountableVal() {
382
        return minDiscountableVal;
383
    }
384
 
385
 
386
    public void setMinDiscountableVal(Long minDiscountableVal) {
387
        this.minDiscountableVal = minDiscountableVal;
388
    }
389
 
390
 
391
    public Long getStartHour() {
392
        return startHour;
393
    }
394
 
395
 
396
    public void setStartHour(Long startHour) {
397
        this.startHour = startHour;
398
    }
399
 
400
 
401
    public Long getStartMinute() {
402
        return startMinute;
403
    }
404
 
405
 
406
    public void setStartMinute(Long startMinute) {
407
        this.startMinute = startMinute;
408
    }
409
 
410
 
411
    public Long getEndHour() {
412
        return endHour;
413
    }
414
 
415
 
416
    public void setEndHour(Long endHour) {
417
        this.endHour = endHour;
418
    }
419
 
420
 
421
    public Long getEndMinute() {
422
        return endMinute;
423
    }
424
 
425
 
426
    public void setEndMinute(Long endMinute) {
427
        this.endMinute = endMinute;
428
    }
6730 anupam.sin 429
 
430
 
431
    public String getCouponCode() {
432
        return couponCode;
433
    }
434
 
435
 
436
    public void setCouponCode(String couponCode) {
437
        this.couponCode = couponCode;
438
    }
7563 anupam.sin 439
 
440
 
441
    public int getIsCod() {
442
        return isCod;
443
    }
444
 
445
 
446
    public void setIsCod(int isCod) {
447
        this.isCod = isCod;
448
    }
6497 amit.gupta 449
}