Subversion Repositories SmartDukaan

Rev

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