Subversion Repositories SmartDukaan

Rev

Rev 11522 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.mobileapi.serving.services;
import in.shop2020.logistics.DeliveryType;
import in.shop2020.logistics.LogisticsInfo;
import in.shop2020.logistics.LogisticsService.Client;
import in.shop2020.logistics.LogisticsServiceException;
import in.shop2020.metamodel.util.LogisticsInfoPojo;
import in.shop2020.mobileapi.serving.controllers.EstimateController;
import in.shop2020.thrift.clients.LogisticsClient;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;

/**
 * @author rajveer
 *
 */
public class LogisticsService {
        
        private static final long serialVersionUID = 8023801600023970837L;

        private static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();

    private static Logger log = Logger.getLogger(EstimateController.class);
        
    // GET /logout
    public static LogisticsInfoPojo getLogisticsInfo(long itemId, String pinCode){
        LogisticsClient logisticsServiceClient = null;
        long businessDays = -1;
                boolean isOTGAvailable = false;
                String days="-1";
                String codDays="-1";
                boolean isCODAvailable = false;
                LogisticsInfoPojo lip = new LogisticsInfoPojo();
                
                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();
                        lip.setBusinessDays(businessDays);
                        lip.setCodDeliveryEstimate(codDays);
                        lip.setDelivelyEstimate(days);
                        lip.setIsCodAvailableForLocation(isCODAvailable);
                        lip.setOnTimeGuarantee(isOTGAvailable);
        } catch (LogisticsServiceException e)   {
                days = "-1";
                isCODAvailable = false;
                log.error("Unable to get estimate/COD availability for " + itemId + " and pincode : " + pinCode);
                
                } catch(TException e)   {
                        
                } catch (Exception e)   {
                
                }
                return lip;
                /*
                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);
        }
}