Rev 7895 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import java.util.GregorianCalendar;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.HashMap;import java.util.Map;import java.util.StringTokenizer;import in.shop2020.datalogger.EventType;import in.shop2020.logistics.LogisticsInfo;import in.shop2020.logistics.LogisticsService.Client;import in.shop2020.logistics.DeliveryType;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.thrift.clients.LogisticsClient;import in.shop2020.utils.DataLogger;import org.apache.log4j.Logger;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.thrift.TException;import com.google.gson.Gson;/*** @author rajveer**/public class EstimateController extends BaseController {private static final long serialVersionUID = 8023801600023970837L;public static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();private static Logger log = Logger.getLogger(Class.class);private String id;private long itemId;private String pincode;private String days = "-1";private long businessDays = -1;private boolean isCODAvailable;private boolean isOTGAvailable;private String codDays = "-1";private Map<String, String> response = new HashMap<String, String>();public EstimateController() {super();}// GET /logoutpublic HttpHeaders show(){LogisticsClient logisticsServiceClient = null;try {logisticsServiceClient = new LogisticsClient();Client logisticsClient = logisticsServiceClient.getClient();LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);if(logistincInfo.getDeliveryTime()!=-1L){days = getDeliveryDateString((int)logistincInfo.getDeliveryTime(), DeliveryType.PREPAID);if(logistincInfo.isCodAllowed()){codDays = getDeliveryDateString((int)(logistincInfo.getDeliveryTime()), DeliveryType.COD);}}businessDays = logistincInfo.getDeliveryTime();isCODAvailable = logistincInfo.isCodAllowed();isOTGAvailable = logistincInfo.isOtgAvailable();} catch (LogisticsServiceException e) {days = "-1";isCODAvailable = false;log.error("Unable to get estimate/COD availability for " + itemId, e);} catch(TException e) {} catch (Exception e) {}response.put("delivery_estimate", days);response.put("cod_delivery_estimate", codDays);response.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));response.put("on_time_guarantee", Boolean.toString(isOTGAvailable));response.put("business_days", businessDays + "");try{if(pincode.length() == 6 && !pincode.equals("110001")){String requestOrigin = request.getHeader("referer").contains("cart")?"Cart":"Product";// DataLogger.logData(EventType.DELIVERY_ESTIMATE, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),// pincode, (new Long(days)).toString(), (new Long(itemId)).toString(), (new Boolean(isCODAvailable)).toString(),// requestOrigin);}}catch(Exception e){log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());}return new DefaultHttpHeaders("index");}public static void main(String[] args) throws TException {System.out.println(getDeliveryDateString(1, DeliveryType.COD));System.out.println(getDeliveryDateString(2, DeliveryType.COD));System.out.println(getDeliveryDateString(2, DeliveryType.PREPAID));System.out.println(getDeliveryDateString(3, DeliveryType.PREPAID));}public static String getDeliveryDateString(int days, DeliveryType type) throws TException {Calendar now = new GregorianCalendar();int hour = now.get(Calendar.HOUR_OF_DAY);if(type == DeliveryType.COD && hour < 15){days = days + 1;}if(businessDayToActualDateMap.containsKey(days)){if(hour != 0){return businessDayToActualDateMap.get(days);}businessDayToActualDateMap.clear();}now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 0, 0, 0);LogisticsClient logisticsServiceClient = null;logisticsServiceClient = new LogisticsClient();Client logisticsClient = logisticsServiceClient.getClient();int newdays = (int) logisticsClient.adjustDeliveryDays(now.getTimeInMillis(), days);now.add(Calendar.DAY_OF_MONTH, newdays);SimpleDateFormat dateformat = new SimpleDateFormat("EEE dd-MMM-yy");if(newdays == 1){businessDayToActualDateMap.put(days, "Tomorrow, " + dateformat.format(now.getTime()));}else{businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));}return businessDayToActualDateMap.get(days);}/**** @param id*/public void setId(String id) {this.id = id;StringTokenizer tokenizer = new StringTokenizer(this.id, "_");this.pincode = tokenizer.nextToken();this.itemId = Long.parseLong(tokenizer.nextToken());}public String getResponseJSONString() {Gson gson = new Gson();return gson.toJson(response);}}