Subversion Repositories SmartDukaan

Rev

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