Subversion Repositories SmartDukaan

Rev

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