| 458 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
| 7791 |
rajveer |
2 |
import java.util.GregorianCalendar;
|
|
|
3 |
import java.text.SimpleDateFormat;
|
|
|
4 |
import java.util.Calendar;
|
| 3830 |
chandransh |
5 |
import java.util.HashMap;
|
| 7791 |
rajveer |
6 |
import java.util.List;
|
| 3830 |
chandransh |
7 |
import java.util.Map;
|
| 507 |
rajveer |
8 |
import java.util.StringTokenizer;
|
|
|
9 |
|
| 5072 |
amar.kumar |
10 |
import in.shop2020.datalogger.EventType;
|
| 4865 |
rajveer |
11 |
import in.shop2020.logistics.LogisticsInfo;
|
| 507 |
rajveer |
12 |
import in.shop2020.logistics.LogisticsService.Client;
|
| 4630 |
mandeep.dh |
13 |
import in.shop2020.logistics.DeliveryType;
|
| 786 |
rajveer |
14 |
import in.shop2020.logistics.LogisticsServiceException;
|
| 3126 |
rajveer |
15 |
import in.shop2020.thrift.clients.LogisticsClient;
|
| 5072 |
amar.kumar |
16 |
import in.shop2020.utils.DataLogger;
|
| 507 |
rajveer |
17 |
|
| 832 |
rajveer |
18 |
import org.apache.log4j.Logger;
|
| 458 |
rajveer |
19 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
20 |
import org.apache.struts2.rest.HttpHeaders;
|
| 786 |
rajveer |
21 |
import org.apache.thrift.TException;
|
| 458 |
rajveer |
22 |
|
| 3830 |
chandransh |
23 |
import com.google.gson.Gson;
|
|
|
24 |
|
| 458 |
rajveer |
25 |
/**
|
|
|
26 |
* @author rajveer
|
|
|
27 |
*
|
|
|
28 |
*/
|
|
|
29 |
public class EstimateController extends BaseController {
|
|
|
30 |
|
| 1044 |
chandransh |
31 |
private static final long serialVersionUID = 8023801600023970837L;
|
|
|
32 |
|
| 7791 |
rajveer |
33 |
public static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
|
| 832 |
rajveer |
34 |
private static Logger log = Logger.getLogger(Class.class);
|
| 458 |
rajveer |
35 |
private String id;
|
| 507 |
rajveer |
36 |
private long itemId;
|
|
|
37 |
private String pincode;
|
| 7791 |
rajveer |
38 |
private String days = "-1";
|
| 3830 |
chandransh |
39 |
private boolean isCODAvailable;
|
| 6524 |
rajveer |
40 |
private boolean isOTGAvailable;
|
| 7809 |
rajveer |
41 |
private String codDays = "-1";
|
| 3830 |
chandransh |
42 |
private Map<String, String> response = new HashMap<String, String>();
|
|
|
43 |
|
| 458 |
rajveer |
44 |
public EstimateController() {
|
|
|
45 |
super();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// GET /logout
|
| 786 |
rajveer |
49 |
public HttpHeaders show(){
|
| 7809 |
rajveer |
50 |
Calendar cal = Calendar.getInstance();
|
| 3126 |
rajveer |
51 |
LogisticsClient logisticsServiceClient = null;
|
| 507 |
rajveer |
52 |
try {
|
| 3126 |
rajveer |
53 |
logisticsServiceClient = new LogisticsClient();
|
| 507 |
rajveer |
54 |
Client logisticsClient = logisticsServiceClient.getClient();
|
| 4865 |
rajveer |
55 |
LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);
|
| 7791 |
rajveer |
56 |
|
|
|
57 |
if(logistincInfo.getDeliveryTime()!=-1L){
|
| 7839 |
rajveer |
58 |
days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);
|
|
|
59 |
if(logistincInfo.isCodAllowed()){
|
| 7859 |
rajveer |
60 |
codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);
|
| 7809 |
rajveer |
61 |
}
|
| 7791 |
rajveer |
62 |
}
|
|
|
63 |
|
| 4865 |
rajveer |
64 |
isCODAvailable = logistincInfo.isCodAllowed();
|
| 6524 |
rajveer |
65 |
isOTGAvailable = logistincInfo.isOtgAvailable();
|
| 3830 |
chandransh |
66 |
} catch (LogisticsServiceException e) {
|
| 7791 |
rajveer |
67 |
days = "-1";
|
| 3830 |
chandransh |
68 |
isCODAvailable = false;
|
|
|
69 |
log.error("Unable to get estimate/COD availability for " + itemId, e);
|
|
|
70 |
|
|
|
71 |
} catch(TException e) {
|
| 786 |
rajveer |
72 |
|
| 3830 |
chandransh |
73 |
} catch (Exception e) {
|
| 786 |
rajveer |
74 |
|
|
|
75 |
}
|
| 7791 |
rajveer |
76 |
response.put("delivery_estimate", days);
|
| 7809 |
rajveer |
77 |
response.put("cod_delivery_estimate", codDays);
|
| 3830 |
chandransh |
78 |
response.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));
|
| 6524 |
rajveer |
79 |
response.put("on_time_guarantee", Boolean.toString(isOTGAvailable));
|
| 7840 |
rajveer |
80 |
SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
|
| 7809 |
rajveer |
81 |
response.put("system_date", dateformat.format(cal.getTime()));
|
| 3830 |
chandransh |
82 |
|
| 5072 |
amar.kumar |
83 |
try{
|
| 5340 |
amar.kumar |
84 |
if(pincode.length() == 6 && !pincode.equals("110001")){
|
|
|
85 |
String requestOrigin = request.getHeader("referer").contains("cart")?"Cart":"Product";
|
|
|
86 |
DataLogger.logData(EventType.DELIVERY_ESTIMATE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
|
|
|
87 |
pincode, (new Long(days)).toString(), (new Long(itemId)).toString(), (new Boolean(isCODAvailable)).toString(),
|
|
|
88 |
requestOrigin);
|
|
|
89 |
}
|
| 5072 |
amar.kumar |
90 |
}catch(Exception e){
|
| 5191 |
amar.kumar |
91 |
log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());
|
| 5072 |
amar.kumar |
92 |
}
|
| 458 |
rajveer |
93 |
return new DefaultHttpHeaders("index");
|
|
|
94 |
}
|
| 3830 |
chandransh |
95 |
|
| 7839 |
rajveer |
96 |
|
|
|
97 |
public static String getDeliveryDateString(int days, DeliveryType type) throws TException {
|
| 7791 |
rajveer |
98 |
Calendar now = new GregorianCalendar();
|
|
|
99 |
int hour = now.get(Calendar.HOUR_OF_DAY);
|
| 7839 |
rajveer |
100 |
if(type == DeliveryType.COD && hour < 15){
|
|
|
101 |
days = days + 1;
|
|
|
102 |
}
|
| 7791 |
rajveer |
103 |
if(businessDayToActualDateMap.containsKey(days)){
|
| 7798 |
rajveer |
104 |
if(hour != 0){
|
| 7791 |
rajveer |
105 |
return businessDayToActualDateMap.get(days);
|
|
|
106 |
}
|
|
|
107 |
businessDayToActualDateMap.clear();
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
now.add(Calendar.DAY_OF_MONTH, days);
|
|
|
112 |
Calendar to_range = new GregorianCalendar();
|
|
|
113 |
to_range.add(Calendar.DAY_OF_MONTH, 30);
|
|
|
114 |
|
|
|
115 |
LogisticsClient logisticsServiceClient = null;
|
|
|
116 |
|
|
|
117 |
logisticsServiceClient = new LogisticsClient();
|
|
|
118 |
Client logisticsClient = logisticsServiceClient.getClient();
|
|
|
119 |
|
|
|
120 |
List<Long> holidays = logisticsClient.getHolidays(now.getTimeInMillis(), to_range.getTimeInMillis());
|
|
|
121 |
|
|
|
122 |
boolean check= true;
|
|
|
123 |
outer:while(check){
|
|
|
124 |
if(Calendar.SUNDAY==now.get(Calendar.DAY_OF_WEEK)){
|
|
|
125 |
now.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
126 |
check = true;
|
|
|
127 |
continue outer;
|
|
|
128 |
}
|
|
|
129 |
for(int i=0; i<holidays.size(); i++){
|
|
|
130 |
long now_in_long= now.getTimeInMillis();
|
|
|
131 |
if(now_in_long==holidays.get(i)){
|
|
|
132 |
now.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
133 |
check = true;
|
|
|
134 |
continue outer;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
check= false;
|
| 7840 |
rajveer |
138 |
SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");
|
| 7791 |
rajveer |
139 |
businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
|
|
|
140 |
}
|
|
|
141 |
return businessDayToActualDateMap.get(days);
|
|
|
142 |
}
|
|
|
143 |
|
| 458 |
rajveer |
144 |
/**
|
|
|
145 |
*
|
|
|
146 |
* @param id
|
|
|
147 |
*/
|
|
|
148 |
public void setId(String id) {
|
|
|
149 |
this.id = id;
|
| 2148 |
chandransh |
150 |
StringTokenizer tokenizer = new StringTokenizer(this.id, "_");
|
| 507 |
rajveer |
151 |
this.pincode = tokenizer.nextToken();
|
|
|
152 |
this.itemId = Long.parseLong(tokenizer.nextToken());
|
| 458 |
rajveer |
153 |
}
|
|
|
154 |
|
| 3830 |
chandransh |
155 |
public String getResponseJSONString() {
|
|
|
156 |
Gson gson = new Gson();
|
|
|
157 |
return gson.toJson(response);
|
| 458 |
rajveer |
158 |
}
|
| 3830 |
chandransh |
159 |
}
|