Subversion Repositories SmartDukaan

Rev

Rev 6524 | Rev 7798 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6524 Rev 7791
Line 1... Line -...
1
/**
-
 
2
 * 
-
 
3
 */
-
 
4
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
5
 
-
 
-
 
2
import java.util.GregorianCalendar;
-
 
3
import java.text.SimpleDateFormat;
-
 
4
import java.util.Calendar;
6
import java.util.HashMap;
5
import java.util.HashMap;
-
 
6
import java.util.List;
7
import java.util.Map;
7
import java.util.Map;
8
import java.util.StringTokenizer;
8
import java.util.StringTokenizer;
9
 
9
 
10
import in.shop2020.datalogger.EventType;
10
import in.shop2020.datalogger.EventType;
11
import in.shop2020.logistics.LogisticsInfo;
11
import in.shop2020.logistics.LogisticsInfo;
Line 28... Line 28...
28
 */
28
 */
29
public class EstimateController extends BaseController {
29
public class EstimateController extends BaseController {
30
	
30
	
31
	private static final long serialVersionUID = 8023801600023970837L;
31
	private static final long serialVersionUID = 8023801600023970837L;
32
 
32
 
-
 
33
	public static Map<Integer, String> businessDayToActualDateMap = new HashMap<Integer, String>();
33
	private static Logger log = Logger.getLogger(Class.class);	
34
	private static Logger log = Logger.getLogger(Class.class);	
34
	private String id;
35
	private String id;
35
	private long itemId;
36
	private long itemId;
36
	private String pincode;
37
	private String pincode;
37
	private long days = -1;
38
	private String days = "-1";
38
	private boolean isCODAvailable;
39
	private boolean isCODAvailable;
39
	private boolean isOTGAvailable;
40
	private boolean isOTGAvailable;
40
	private Map<String, String> response = new HashMap<String, String>();
41
	private Map<String, String> response = new HashMap<String, String>();
41
	
42
	
42
	public EstimateController() {
43
	public EstimateController() {
Line 48... Line 49...
48
    	LogisticsClient logisticsServiceClient = null;
49
    	LogisticsClient logisticsServiceClient = null;
49
    	try {
50
    	try {
50
			logisticsServiceClient = new LogisticsClient();
51
			logisticsServiceClient = new LogisticsClient();
51
			Client logisticsClient = logisticsServiceClient.getClient();
52
			Client logisticsClient = logisticsServiceClient.getClient();
52
			LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);
53
			LogisticsInfo logistincInfo = logisticsClient.getLogisticsEstimation(itemId, pincode, DeliveryType.PREPAID);
-
 
54
			
53
			days = logistincInfo.getDeliveryTime();
55
			if(logistincInfo.getDeliveryTime()!=-1L){
-
 
56
				days = checkValidDeliveryEstimate((int)logistincInfo.getDeliveryTime());
-
 
57
			}
-
 
58
			
54
			isCODAvailable = logistincInfo.isCodAllowed();
59
			isCODAvailable = logistincInfo.isCodAllowed();
55
			isOTGAvailable = logistincInfo.isOtgAvailable();
60
			isOTGAvailable = logistincInfo.isOtgAvailable();
56
    	} catch (LogisticsServiceException e)	{
61
    	} catch (LogisticsServiceException e)	{
57
    		days = -1;
62
    		days = "-1";
58
    		isCODAvailable = false;
63
    		isCODAvailable = false;
59
    		log.error("Unable to get estimate/COD availability for " + itemId, e);
64
    		log.error("Unable to get estimate/COD availability for " + itemId, e);
60
    		
65
    		
61
		} catch(TException e)	{
66
		} catch(TException e)	{
62
			
67
			
63
		} catch (Exception e)	{
68
		} catch (Exception e)	{
64
    		
69
    		
65
		}
70
		}
66
		response.put("delivery_estimate", Long.toString(days));
71
		response.put("delivery_estimate", days);
67
		response.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));
72
		response.put("is_cod_available_for_location", Boolean.toString(isCODAvailable));
68
		response.put("on_time_guarantee", Boolean.toString(isOTGAvailable));
73
		response.put("on_time_guarantee", Boolean.toString(isOTGAvailable));
69
		
74
		
70
		try{
75
		try{
71
			if(pincode.length() == 6 && !pincode.equals("110001")){
76
			if(pincode.length() == 6 && !pincode.equals("110001")){
Line 78... Line 83...
78
			log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());
83
			log.warn(e.getMessage()+"pincode : "+ pincode + " Actual referer : " + request.getHeader("referer") + e.getStackTrace());
79
		}
84
		}
80
    	return new DefaultHttpHeaders("index");
85
    	return new DefaultHttpHeaders("index");
81
    }
86
    }
82
    
87
    
-
 
88
    public static String checkValidDeliveryEstimate(int days) throws TException {
-
 
89
    	Calendar now = new GregorianCalendar();
-
 
90
    	int hour = now.get(Calendar.HOUR_OF_DAY);
-
 
91
    	if(businessDayToActualDateMap.containsKey(days)){
-
 
92
    		if(hour != 24){	
-
 
93
    			return businessDayToActualDateMap.get(days);
-
 
94
    		}
-
 
95
    		businessDayToActualDateMap.clear();
-
 
96
		}
-
 
97
    	
-
 
98
    	
-
 
99
    	now.add(Calendar.DAY_OF_MONTH, days);
-
 
100
		Calendar to_range = new GregorianCalendar();
-
 
101
		to_range.add(Calendar.DAY_OF_MONTH, 30);
-
 
102
 
-
 
103
    	LogisticsClient logisticsServiceClient = null;
-
 
104
	
-
 
105
		logisticsServiceClient = new LogisticsClient();
-
 
106
		Client logisticsClient = logisticsServiceClient.getClient();
-
 
107
		
-
 
108
    	List<Long> holidays = logisticsClient.getHolidays(now.getTimeInMillis(), to_range.getTimeInMillis());
-
 
109
 
-
 
110
    	boolean check= true;
-
 
111
    	outer:while(check){
-
 
112
    		if(Calendar.SUNDAY==now.get(Calendar.DAY_OF_WEEK)){
-
 
113
    			now.add(Calendar.DAY_OF_MONTH, 1);
-
 
114
    			check = true;
-
 
115
    			continue outer;
-
 
116
    		}
-
 
117
    		for(int i=0; i<holidays.size(); i++){
-
 
118
    			long now_in_long= now.getTimeInMillis();
-
 
119
    			if(now_in_long==holidays.get(i)){
-
 
120
    				now.add(Calendar.DAY_OF_MONTH, 1);
-
 
121
    				check = true;
-
 
122
    				continue outer;
-
 
123
    			}
-
 
124
    		}
-
 
125
    		check= false;
-
 
126
    		SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMMM-yyyy");
-
 
127
			businessDayToActualDateMap.put(days, dateformat.format(now.getTime()));
-
 
128
    	}
-
 
129
    	return businessDayToActualDateMap.get(days);
-
 
130
	}
-
 
131
    
83
    /**
132
    /**
84
     * 
133
     * 
85
     * @param id
134
     * @param id
86
     */
135
     */
87
    public void setId(String id) {
136
    public void setId(String id) {