Subversion Repositories SmartDukaan

Rev

Rev 9837 | Rev 9931 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9651 manish.sha 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.order.Order;
4
import in.shop2020.model.v1.order.OrderSource;
5
import in.shop2020.model.v1.order.OrderStatus;
6
import in.shop2020.model.v1.order.SnapdealOrder;
7
import in.shop2020.thrift.clients.TransactionClient;
8
 
9
import java.io.BufferedReader;
10
import java.io.IOException;
11
import java.io.InputStreamReader;
12
import java.io.UnsupportedEncodingException;
13
import java.text.SimpleDateFormat;
14
import java.util.ArrayList;
15
import java.util.Date;
9883 manish.sha 16
import java.util.HashMap;
9651 manish.sha 17
import java.util.List;
9883 manish.sha 18
import java.util.Map;
9651 manish.sha 19
 
20
import org.apache.http.HttpResponse;
21
import org.apache.http.NameValuePair;
22
import org.apache.http.client.ClientProtocolException;
23
import org.apache.http.client.HttpClient;
24
import org.apache.http.client.entity.UrlEncodedFormEntity;
25
import org.apache.http.client.methods.HttpGet;
26
import org.apache.http.client.methods.HttpPost;
27
import org.apache.http.impl.client.DefaultHttpClient;
28
import org.apache.http.message.BasicNameValuePair;
29
import org.apache.http.params.HttpParams;
30
import org.json.JSONArray;
31
import org.json.JSONException;
32
import org.json.JSONObject;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
 
36
public class SnapdealOrderStatusReconciliation{
37
	private static Logger logger;
38
 
39
	public static void main(String[] args) throws UnsupportedEncodingException{
40
		logger = LoggerFactory.getLogger(SnapdealOrderStatusReconciliation.class);
41
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
42
		statuses.add(OrderStatus.SHIPPED_FROM_WH);
43
		statuses.add(OrderStatus.SHIPPED_TO_LOGST);
44
 
9883 manish.sha 45
		Map<String,List<List<String>>> orderDataMap = new HashMap<String,List<List<String>>>();
9651 manish.sha 46
		List<List<String>> deliveredOrderDataList = new ArrayList<List<String>>();
9883 manish.sha 47
		List<List<String>> cancelledOrderDataList = new ArrayList<List<String>>();
9651 manish.sha 48
		long minCreationDate = 0l;
49
		TransactionClient tsc = null;
50
		try {
51
			tsc = new TransactionClient();
52
			minCreationDate = tsc.getClient().getMinCreatedTimeStampUndeliveredOrdersForSource(OrderSource.SNAPDEAL.getValue());
53
		} catch (Exception e) {
54
			logger.error("Unable to establish connection to the transaction service while getting Minimum Order Created Timstamp for Undelivered Orders ", e);
9730 manish.sha 55
		}//Dec 14, 2013 12:00:00 AM  1383291412000l
9651 manish.sha 56
 
57
		SimpleDateFormat snapdealDateFormat = new SimpleDateFormat("yyyy-MM-dd");
58
		SimpleDateFormat gotSnapdealDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aaa");
59
		SimpleDateFormat ourDBDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
60
		String startDate= "";
61
		if(minCreationDate > 0l){
62
			startDate = snapdealDateFormat.format(new Date(minCreationDate));
63
		}
64
		else{
65
			startDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
66
		}
67
		logger.info("Snapdeal Order Recon Start Date .. "+startDate);
68
		startDate = startDate.replace("-", "%2F");
69
 
70
		String endDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
71
		logger.info("Snapdeal Order Recon End Date .. "+endDate);
72
		endDate = endDate.replace("-", "%2F");
73
 
74
 
75
		HttpClient client = new DefaultHttpClient();
76
		HttpPost post = new HttpPost("http://shipping.snapdeal.com/login_security_check?spring-security-redirect=http://shipping.snapdeal.com/vendor/product-shipment/shippingDashboard&");
77
		HttpGet get;
78
		BufferedReader rd= null;
9731 manish.sha 79
 
9651 manish.sha 80
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
81
		nameValuePairs.add(new BasicNameValuePair("j_username",
82
		"khushal.bhatia@saholic.com"));
83
		nameValuePairs.add(new BasicNameValuePair("j_password",
84
		"s@h0l1c"));
85
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
86
		HttpResponse response = null;
87
		try {
88
			response = client.execute(post);
9700 manish.sha 89
		} catch (Exception e) {
90
			logger.error("Unable to get Http Response for snapdeal seller portal login", e);
9651 manish.sha 91
		}
92
		try {
93
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9700 manish.sha 94
		} catch (Exception e1) {
95
			logger.error("Unable to read Http Response for snapdeal seller portal login", e1);
96
		} 
9731 manish.sha 97
 
9651 manish.sha 98
		String line = "";
99
		try {
100
			while ((line = rd.readLine()) != null) {
101
				System.out.println(line);
102
			}
9700 manish.sha 103
		} catch (Exception e) {
104
			logger.error("Unable to extract Http Response for snapdeal seller portal login", e);
9651 manish.sha 105
		}
106
 
107
		//http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?shippedStartDate=2014%2F01%2F01&shippedEndDate=2014%2F01%2F08&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED
108
 
109
		logger.info("Getting Delivery Information for DropShip Snapdeal Orders");
9730 manish.sha 110
		String dropshipUrl = "http://shipping.snapdeal.com/vendor/DROPSHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED";
111
		logger.info("Drop Ship Url "+dropshipUrl);
9731 manish.sha 112
 
9730 manish.sha 113
		get = new HttpGet(dropshipUrl);
9651 manish.sha 114
 
9837 manish.sha 115
		//HttpClient client1 = new DefaultHttpClient();
9759 manish.sha 116
 
9651 manish.sha 117
		try {
9837 manish.sha 118
			response = client.execute(get);
9700 manish.sha 119
		} catch (Exception e) {
120
			logger.error("Unable to get Http Response for snapdeal dropship delivered orders", e);
121
		} 
9651 manish.sha 122
 
123
		try {
124
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9700 manish.sha 125
		} catch (Exception e) {
126
			logger.error("Unable to read Http Response for snapdeal dropship delivered orders", e);
127
		} 
9651 manish.sha 128
 
129
		JSONArray jsonDataShip = null;
130
		String line1 = "";
131
		try {
132
			while ((line1 = rd.readLine()) != null) {
133
				if(line1.contains("jsonString")){
134
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
9719 manish.sha 135
					logger.info("Response get from SnapDeal DropShip.. ");
9651 manish.sha 136
					jsonDataShip = new JSONArray(jsonString);
137
					break;
138
				}
139
			}
9700 manish.sha 140
		} catch (Exception e) {
141
			logger.error("Unable to extract Http Response for snapdeal dropship delivered orders", e);
142
		} 
9651 manish.sha 143
 
144
		if(jsonDataShip!=null && jsonDataShip.length()>0){
145
			for(int i=0; i< jsonDataShip.length(); i++){
146
				JSONObject jsonObj = null;
147
				try {
148
					jsonObj = jsonDataShip.getJSONObject(i);
149
 
150
					if(jsonObj!=null){
9719 manish.sha 151
						//System.out.println(jsonObj);
9651 manish.sha 152
						String subOrderCode =(String) jsonObj.get("suborderCode");
153
						String referenceNumber =(String) jsonObj.get("referenceNumber");
154
						String deliveryDate =(String) jsonObj.get("deliveredOn");
9719 manish.sha 155
						//System.out.println("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
9700 manish.sha 156
						logger.info("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
9651 manish.sha 157
						deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
158
						List<String> deliveredOrdelList = new ArrayList<String>();
159
						deliveredOrdelList.add(referenceNumber);
160
						deliveredOrdelList.add(subOrderCode);
161
						deliveredOrdelList.add(deliveryDate);
162
						deliveredOrderDataList.add(deliveredOrdelList);
163
					}
164
				} catch (Exception e) {
9700 manish.sha 165
					logger.error("Unable to add delivered order details for updation regarding snapdeal dropship delivered orders", e);
9651 manish.sha 166
				}
167
			}
168
		}
169
 
170
		logger.info("Getting Delivery Information for OneShip Snapdeal Orders");
9730 manish.sha 171
		String oneShipUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED";
172
		logger.info("One Ship Url "+oneShipUrl);
9731 manish.sha 173
 
9730 manish.sha 174
		get = new HttpGet(oneShipUrl);	
9731 manish.sha 175
 
9837 manish.sha 176
		//HttpClient client2 = new DefaultHttpClient();
9759 manish.sha 177
 
9651 manish.sha 178
		try {
9837 manish.sha 179
			response = client.execute(get);
9700 manish.sha 180
		} catch (Exception e) {
181
			logger.error("Unable to get Http Response for snapdeal oneship delivered orders", e);
182
		} 
9651 manish.sha 183
 
184
		try {
185
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9700 manish.sha 186
		} catch (Exception e) {
187
			logger.error("Unable to read Http Response for snapdeal oneship delivered orders", e);
188
		} 
9651 manish.sha 189
 
190
		try {
191
			while ((line1 = rd.readLine()) != null) {
192
				if(line1.contains("jsonString")){
193
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
9719 manish.sha 194
					logger.info("Response get from SnapDeal OneShip.. ");
9651 manish.sha 195
					jsonDataShip = new JSONArray(jsonString);
196
					break;
197
				}
198
			}
9700 manish.sha 199
		} catch (Exception e) {
200
			logger.error("Unable to extract Http Response for snapdeal oneship delivered orders", e);
201
		} 
9731 manish.sha 202
 
9651 manish.sha 203
		if(jsonDataShip!=null && jsonDataShip.length()>0){
204
			for(int i=0; i< jsonDataShip.length(); i++){
205
				JSONObject jsonObj = null;
206
				try {
207
					jsonObj = jsonDataShip.getJSONObject(i);
208
 
209
					if(jsonObj!=null){
9719 manish.sha 210
						//System.out.println(jsonObj);
9651 manish.sha 211
						String subOrderCode =(String) jsonObj.get("suborderCode");
212
						String referenceNumber =(String) jsonObj.get("referenceNumber");
213
						String deliveryDate =(String) jsonObj.get("deliveredOn");
9719 manish.sha 214
						//System.out.println("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
9700 manish.sha 215
						logger.info("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
9651 manish.sha 216
						deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
217
						List<String> deliveredOrdelList = new ArrayList<String>();
218
						deliveredOrdelList.add(referenceNumber);
219
						deliveredOrdelList.add(subOrderCode);
220
						deliveredOrdelList.add(deliveryDate);
221
 
222
						deliveredOrderDataList.add(deliveredOrdelList);
223
					}
224
				} catch (Exception e) {
9700 manish.sha 225
					logger.error("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders", e);
9651 manish.sha 226
				}
227
			}
228
		}
9731 manish.sha 229
 
9883 manish.sha 230
		logger.info("Getting Information for Cancelled DropShip Snapdeal Orders");
231
		String dropshipCancelUrl = "http://shipping.snapdeal.com/vendor/DROPSHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=CANCELLATION_COMPLETE&statusColumn=cancelledOn&dispatchCategoryId=0&sCode=CANCELLATION_COMPLETE";
232
		logger.info("Drop Ship Cancel Url "+dropshipCancelUrl);
233
 
234
		get = new HttpGet(dropshipCancelUrl);
235
 
236
		try {
237
			response = client.execute(get);
238
		} catch (Exception e) {
239
			logger.error("Unable to get Http Response for snapdeal dropship cancelled orders", e);
240
		} 
241
 
242
		try {
243
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
244
		} catch (Exception e) {
245
			logger.error("Unable to read Http Response for snapdeal dropship cancelled orders", e);
246
		}
247
 
248
		try {
249
			while ((line1 = rd.readLine()) != null) {
250
				if(line1.contains("jsonString")){
251
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
252
					logger.info("Response get from SnapDeal DropShip Cancelled Orders.. ");
253
					jsonDataShip = new JSONArray(jsonString);
254
					break;
255
				}
256
			}
257
		} catch (Exception e) {
258
			logger.error("Unable to extract Http Response for snapdeal dropship cancelled orders", e);
259
		} 
260
 
261
		if(jsonDataShip!=null && jsonDataShip.length()>0){
262
			for(int i=0; i< jsonDataShip.length(); i++){
263
				JSONObject jsonObj = null;
264
				try {
265
					jsonObj = jsonDataShip.getJSONObject(i);
266
 
267
					if(jsonObj!=null){
268
						//System.out.println(jsonObj);
269
						String subOrderCode =(String) jsonObj.get("suborderCode");
270
						String referenceNumber =(String) jsonObj.get("referenceNumber");
271
						//System.out.println("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
272
						logger.info("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
273
						List<String> cancelledOrdelList = new ArrayList<String>();
274
						cancelledOrdelList.add(referenceNumber);
275
						cancelledOrdelList.add(subOrderCode);
276
						cancelledOrderDataList.add(cancelledOrdelList);
277
					}
278
				} catch (Exception e) {
279
					logger.error("Unable to add cancelled order details for updation regarding snapdeal dropship orders", e);
280
				}
281
			}
282
		}
283
 
284
		logger.info("Getting Information for OneShip Snapdeal Cancelled Orders");
285
		String oneShipCancelUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=CANCELLATION_COMPLETE&statusColumn=cancelledOn&dispatchCategoryId=0&sCode=CANCELLATION_COMPLETE";
286
		logger.info("One Ship Cancel Url "+oneShipCancelUrl);
287
 
288
		get = new HttpGet(oneShipCancelUrl);
289
 
290
		try {
291
			response = client.execute(get);
292
		} catch (Exception e) {
293
			logger.error("Unable to get Http Response for snapdeal oneship cancelled orders", e);
294
		} 
295
 
296
		try {
297
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
298
		} catch (Exception e) {
299
			logger.error("Unable to read Http Response for snapdeal oneship cancelled orders", e);
300
		}
301
 
302
		try {
303
			while ((line1 = rd.readLine()) != null) {
304
				if(line1.contains("jsonString")){
305
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
306
					logger.info("Response get from SnapDeal OneShip Cancelled Orders.. ");
307
					jsonDataShip = new JSONArray(jsonString);
308
					break;
309
				}
310
			}
311
		} catch (Exception e) {
312
			logger.error("Unable to extract Http Response for snapdeal oneship cancelled orders", e);
313
		} 
314
 
315
		if(jsonDataShip!=null && jsonDataShip.length()>0){
316
			for(int i=0; i< jsonDataShip.length(); i++){
317
				JSONObject jsonObj = null;
318
				try {
319
					jsonObj = jsonDataShip.getJSONObject(i);
320
 
321
					if(jsonObj!=null){
322
						//System.out.println(jsonObj);
323
						String subOrderCode =(String) jsonObj.get("suborderCode");
324
						String referenceNumber =(String) jsonObj.get("referenceNumber");
325
						//System.out.println("Snapdeal OneShip Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
326
						logger.info("Snapdeal OneShip Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
327
						List<String> cancelledOrdelList = new ArrayList<String>();
328
						cancelledOrdelList.add(referenceNumber);
329
						cancelledOrdelList.add(subOrderCode);
330
						cancelledOrderDataList.add(cancelledOrdelList);
331
					}
332
				} catch (Exception e) {
333
					logger.error("Unable to add cancelled order details for updation regarding snapdeal oneship orders", e);
334
				}
335
			}
336
		}
337
 
9731 manish.sha 338
		if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
9732 manish.sha 339
			logger.info("Delivered Orders Size .. "+deliveredOrderDataList.size());
9883 manish.sha 340
			orderDataMap.put("Delivered", deliveredOrderDataList);
341
		}
342
 
343
		if(cancelledOrderDataList!=null && cancelledOrderDataList.size()>0){
344
			logger.info("Cancelled Orders Size .. "+cancelledOrderDataList.size());
345
			orderDataMap.put("Cancelled", cancelledOrderDataList);
346
		}
347
 
348
		if(orderDataMap!=null && orderDataMap.size()>0){
9731 manish.sha 349
			try{
9883 manish.sha 350
				new TransactionClient().getClient().updateSnapdealOrdersStatus(orderDataMap);
9731 manish.sha 351
			} catch(Exception e){
352
				logger.error("Unable to update delivery information for snapdeal orders", e);
9651 manish.sha 353
			}
354
		}
9731 manish.sha 355
 
9651 manish.sha 356
	}
357
}