Subversion Repositories SmartDukaan

Rev

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