Subversion Repositories SmartDukaan

Rev

Rev 11268 | Rev 12428 | 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.OrderSource;
10178 manish.sha 4
import in.shop2020.model.v1.order.OrderStatus;
9651 manish.sha 5
import in.shop2020.thrift.clients.TransactionClient;
10178 manish.sha 6
 
9651 manish.sha 7
import java.io.BufferedReader;
10565 manish.sha 8
import java.io.IOException;
9651 manish.sha 9
import java.io.InputStreamReader;
10178 manish.sha 10
import java.io.UnsupportedEncodingException;
9651 manish.sha 11
import java.text.SimpleDateFormat;
12
import java.util.ArrayList;
11518 manish.sha 13
import java.util.Calendar;
9651 manish.sha 14
import java.util.Date;
9883 manish.sha 15
import java.util.HashMap;
9651 manish.sha 16
import java.util.List;
9883 manish.sha 17
import java.util.Map;
10178 manish.sha 18
 
19
import org.apache.http.HttpResponse;
20
import org.apache.http.NameValuePair;
10565 manish.sha 21
import org.apache.http.client.ClientProtocolException;
10178 manish.sha 22
import org.apache.http.client.HttpClient;
23
import org.apache.http.client.entity.UrlEncodedFormEntity;
24
import org.apache.http.client.methods.HttpGet;
25
import org.apache.http.client.methods.HttpPost;
26
import org.apache.http.impl.client.DefaultHttpClient;
27
import org.apache.http.message.BasicNameValuePair;
9651 manish.sha 28
import org.json.JSONArray;
29
import org.json.JSONObject;
10178 manish.sha 30
import org.slf4j.LoggerFactory;
9651 manish.sha 31
import org.slf4j.Logger;
32
 
10178 manish.sha 33
 
34
 
9651 manish.sha 35
public class SnapdealOrderStatusReconciliation{
36
	private static Logger logger;
37
 
10178 manish.sha 38
	public static void main(String[] args) throws UnsupportedEncodingException{
9651 manish.sha 39
		logger = LoggerFactory.getLogger(SnapdealOrderStatusReconciliation.class);
10178 manish.sha 40
		/*List<OrderStatus> statuses = new ArrayList<OrderStatus>();
41
		statuses.add(OrderStatus.SHIPPED_FROM_WH);
42
		statuses.add(OrderStatus.SHIPPED_TO_LOGST);*/
43
 
9883 manish.sha 44
		Map<String,List<List<String>>> orderDataMap = new HashMap<String,List<List<String>>>();
9651 manish.sha 45
		List<List<String>> deliveredOrderDataList = new ArrayList<List<String>>();
9883 manish.sha 46
		List<List<String>> cancelledOrderDataList = new ArrayList<List<String>>();
9651 manish.sha 47
		long minCreationDate = 0l;
9931 manish.sha 48
		long currentTime = System.currentTimeMillis();
11518 manish.sha 49
 
50
		Calendar cal = Calendar.getInstance();
51
		cal.add(Calendar.DAY_OF_MONTH, -60);
52
		minCreationDate = cal.getTimeInMillis();
53
		/*TransactionClient tsc = null;
9651 manish.sha 54
		try {
55
			tsc = new TransactionClient();
56
			minCreationDate = tsc.getClient().getMinCreatedTimeStampUndeliveredOrdersForSource(OrderSource.SNAPDEAL.getValue());
11518 manish.sha 57
			minCreationDate = 1393612200000l;
9651 manish.sha 58
		} catch (Exception e) {
59
			logger.error("Unable to establish connection to the transaction service while getting Minimum Order Created Timstamp for Undelivered Orders ", e);
9730 manish.sha 60
		}//Dec 14, 2013 12:00:00 AM  1383291412000l
11518 manish.sha 61
*/
9651 manish.sha 62
		SimpleDateFormat snapdealDateFormat = new SimpleDateFormat("yyyy-MM-dd");
63
		SimpleDateFormat gotSnapdealDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aaa");
64
		SimpleDateFormat ourDBDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
65
		String startDate= "";
9931 manish.sha 66
		Date start_date = new Date(minCreationDate);
67
		Date end_date = new Date(currentTime);
9651 manish.sha 68
		if(minCreationDate > 0l){
69
			startDate = snapdealDateFormat.format(new Date(minCreationDate));
70
		}
71
		else{
9931 manish.sha 72
			startDate = snapdealDateFormat.format(new Date(currentTime));
9651 manish.sha 73
		}
74
		logger.info("Snapdeal Order Recon Start Date .. "+startDate);
10178 manish.sha 75
		//startDate = startDate.replace("-", "%2F");
9651 manish.sha 76
 
10178 manish.sha 77
		String endDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
9651 manish.sha 78
		logger.info("Snapdeal Order Recon End Date .. "+endDate);
10178 manish.sha 79
		//endDate = endDate.replace("-", "%2F");
10565 manish.sha 80
 
81
 
82
		HttpClient client = new DefaultHttpClient();
83
 
84
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/keymaker/login");
85
		HttpResponse response = null;
86
		try {
87
			response = client.execute(get);
88
		} catch (ClientProtocolException e) {
10178 manish.sha 89
 
10565 manish.sha 90
			e.printStackTrace();
91
		} catch (IOException e) {
10178 manish.sha 92
 
10565 manish.sha 93
			e.printStackTrace();
94
		}
95
		BufferedReader rd = null;
96
		try {
97
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
98
		} catch (IllegalStateException e1) {
10178 manish.sha 99
 
10565 manish.sha 100
			e1.printStackTrace();
101
		} catch (IOException e1) {
102
 
103
			e1.printStackTrace();
104
		}
105
		String line = "";
106
		StringBuffer sb = new StringBuffer();
107
		try {
108
			while ((line = rd.readLine()) != null) {
109
				sb.append(line);
110
				//System.out.println(line);
111
			}
112
		} catch (IOException e) {
113
 
114
			e.printStackTrace();
115
		}
116
		int i= sb.toString().indexOf("name=\"lt\" value=");
117
		char[] charArray = sb.toString().toCharArray();
118
		String lt = "";
119
		int j=0;
120
		for(j=i+16;j<=charArray.length;j++){
121
 
122
			if(charArray[j]==' '){
123
				break;
124
			}
125
		}
126
		lt = sb.substring(i+17,j-1);
127
		System.out.println("LT VALUE " + lt);
128
		i= sb.toString().indexOf("name=\"execution\" value=");
129
		charArray = sb.toString().toCharArray();
130
 
131
		String ex = "";
132
		j=0;
133
		for(j=i+24;j<=charArray.length;j++){
134
			if(charArray[j]==' '){
135
				break;
136
			}
137
		}
138
		ex = sb.substring(i+24,j-1);
139
		System.out.println("EXECUTION VALUE " + ex);	
140
 
141
 
142
 
143
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fshipping.snapdeal.com%2Fj_spring_cas_security_check");
9651 manish.sha 144
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
10565 manish.sha 145
		//nameValuePairs.add(new BasicNameValuePair("username",
146
		//"khushal.bhatia@saholic.com"));
147
		nameValuePairs.add(new BasicNameValuePair("username",
10178 manish.sha 148
		"saholic-snapdeal@saholic.com"));
10565 manish.sha 149
		//nameValuePairs.add(new BasicNameValuePair("password",
150
		//"sonline"));
151
		nameValuePairs.add(new BasicNameValuePair("password",
11268 manish.sha 152
		"snapsaholic"));
10565 manish.sha 153
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
154
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
155
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
156
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
10178 manish.sha 157
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
10565 manish.sha 158
		//response = null;
10178 manish.sha 159
		try {
160
			response = client.execute(post);
161
		} catch (Exception e) {
9700 manish.sha 162
			logger.error("Unable to get Http Response for snapdeal seller portal login", e);
9651 manish.sha 163
		}
164
		try {
10178 manish.sha 165
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9700 manish.sha 166
		} catch (Exception e1) {
167
			logger.error("Unable to read Http Response for snapdeal seller portal login", e1);
168
		} 
9731 manish.sha 169
 
10565 manish.sha 170
		//Striline = "";
9651 manish.sha 171
		try {
172
			while ((line = rd.readLine()) != null) {
173
				System.out.println(line);
174
			}
9700 manish.sha 175
		} catch (Exception e) {
176
			logger.error("Unable to extract Http Response for snapdeal seller portal login", e);
9651 manish.sha 177
		}
10178 manish.sha 178
 
10565 manish.sha 179
		JSONArray jsonDataObj = null;
9931 manish.sha 180
		JSONArray jsonDataShip = null;
181
		String line1 = "";
10178 manish.sha 182
 
9931 manish.sha 183
		for(long start = start_date.getTime(); start < end_date.getTime();){
184
			long end = start + 604800000l;
185
			if(end > end_date.getTime()){
186
				end = end_date.getTime();
187
			}
188
			startDate = snapdealDateFormat.format(new Date(start));
189
			startDate = startDate.replace("-", "%2F");
190
			endDate = snapdealDateFormat.format(new Date(end));
191
			endDate = endDate.replace("-", "%2F");
192
 
193
			logger.info("==== Start Date.."+startDate+"\n");
194
			logger.info("==== End Date.."+endDate+"\n");
10178 manish.sha 195
 
196
 
9931 manish.sha 197
			logger.info("Getting Delivery Information for DropShip Snapdeal Orders");
10565 manish.sha 198
			//http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch?statusCode=COURIER_DELIVERED&dispatchCategoryId=0&startDate=2014%2F04%2F01&endDate=2014%2F04%2F07&statusColumn=deliveredOn
199
			String dropshipUrl = "http://shipping.snapdeal.com/vendor/DROPSHIP/product-shipment/shippedData/fetch/?startDate="+startDate+"&endDate="+endDate+"&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0";
9931 manish.sha 200
			logger.info("Drop Ship Url "+dropshipUrl);
10178 manish.sha 201
 
202
			get = new HttpGet(dropshipUrl);
203
 
9931 manish.sha 204
			try {
10178 manish.sha 205
				response = client.execute(get);
9931 manish.sha 206
			} catch (Exception e) {
207
				logger.error("Unable to get Http Response for snapdeal dropship delivered orders", e);
208
			} 
9731 manish.sha 209
 
9931 manish.sha 210
			try {
10178 manish.sha 211
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9931 manish.sha 212
			} catch (Exception e) {
213
				logger.error("Unable to read Http Response for snapdeal dropship delivered orders", e);
214
			} 
9651 manish.sha 215
 
216
 
9931 manish.sha 217
			try {
10565 manish.sha 218
 
219
				String resP = rd.readLine();
220
				jsonDataObj = new JSONArray("["+resP+"]");
221
				JSONObject j1 = (JSONObject)jsonDataObj.get(0);
222
				jsonDataShip = new JSONArray(j1.get("jsonDataString").toString());
9931 manish.sha 223
			} catch (Exception e) {
224
				logger.error("Unable to extract Http Response for snapdeal dropship delivered orders", e);
225
			} 
10178 manish.sha 226
 
9651 manish.sha 227
 
9931 manish.sha 228
			if(jsonDataShip!=null && jsonDataShip.length()>0){
10565 manish.sha 229
				for(i=0; i< jsonDataShip.length(); i++){
9931 manish.sha 230
					JSONObject jsonObj = null;
231
					try {
232
						jsonObj = jsonDataShip.getJSONObject(i);
9651 manish.sha 233
 
9931 manish.sha 234
						if(jsonObj!=null){
235
							String subOrderCode =(String) jsonObj.get("suborderCode");
10178 manish.sha 236
							String referenceNumber =(String) jsonObj.get("referenceCode");
9931 manish.sha 237
							String deliveryDate =(String) jsonObj.get("deliveredOn");
238
							logger.info("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
239
							deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
240
							List<String> deliveredOrdelList = new ArrayList<String>();
241
							deliveredOrdelList.add(referenceNumber);
242
							deliveredOrdelList.add(subOrderCode);
243
							deliveredOrdelList.add(deliveryDate);
244
							deliveredOrderDataList.add(deliveredOrdelList);
245
						}
246
					} catch (Exception e) {
247
						logger.error("Unable to add delivered order details for updation regarding snapdeal dropship delivered orders", e);
9651 manish.sha 248
					}
249
				}
250
			}
9931 manish.sha 251
 
252
			logger.info("Getting Delivery Information for OneShip Snapdeal Orders");
10178 manish.sha 253
			//statusCode=CLD&dispatchCategoryId=0&startDate=2013%2F11%2F01&endDate=2013%2F11%2F07&statusColumn=cancelledOn
10565 manish.sha 254
			String oneShipUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?startDate="+startDate+"&endDate="+endDate+"&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0";
9931 manish.sha 255
			logger.info("One Ship Url "+oneShipUrl);
10178 manish.sha 256
 
257
			get = new HttpGet(oneShipUrl);	
258
 
9931 manish.sha 259
			try {
10178 manish.sha 260
				response = client.execute(get);
9931 manish.sha 261
			} catch (Exception e) {
262
				logger.error("Unable to get Http Response for snapdeal oneship delivered orders", e);
263
			} 
9651 manish.sha 264
 
9931 manish.sha 265
			try {
10178 manish.sha 266
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9931 manish.sha 267
			} catch (Exception e) {
268
				logger.error("Unable to read Http Response for snapdeal oneship delivered orders", e);
10178 manish.sha 269
			}
270
 
9931 manish.sha 271
			try {
10565 manish.sha 272
				String resP = rd.readLine();
273
				jsonDataObj = new JSONArray("["+resP+"]");
274
				JSONObject j1 = (JSONObject)jsonDataObj.get(0);
275
				jsonDataShip = new JSONArray(j1.get("jsonDataString").toString());
9931 manish.sha 276
			} catch (Exception e) {
277
				logger.error("Unable to extract Http Response for snapdeal oneship delivered orders", e);
10178 manish.sha 278
			}
279
 
9931 manish.sha 280
			if(jsonDataShip!=null && jsonDataShip.length()>0){
10565 manish.sha 281
				for(i=0; i< jsonDataShip.length(); i++){
9931 manish.sha 282
					JSONObject jsonObj = null;
283
					try {
284
						jsonObj = jsonDataShip.getJSONObject(i);
9651 manish.sha 285
 
9931 manish.sha 286
						if(jsonObj!=null){
287
							String subOrderCode =(String) jsonObj.get("suborderCode");
10178 manish.sha 288
							String referenceNumber =(String) jsonObj.get("referenceCode");
9931 manish.sha 289
							String deliveryDate =(String) jsonObj.get("deliveredOn");
290
							logger.info("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
291
							deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
292
							List<String> deliveredOrdelList = new ArrayList<String>();
293
							deliveredOrdelList.add(referenceNumber);
294
							deliveredOrdelList.add(subOrderCode);
295
							deliveredOrdelList.add(deliveryDate);
9651 manish.sha 296
 
9931 manish.sha 297
							deliveredOrderDataList.add(deliveredOrdelList);
298
						}
299
					} catch (Exception e) {
300
						logger.error("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders", e);
301
					}
9651 manish.sha 302
				}
303
			}
9931 manish.sha 304
 
305
 
306
			logger.info("Getting Information for Cancelled DropShip Snapdeal Orders");
10178 manish.sha 307
			String dropshipCancelUrl = "http://shipping.snapdeal.com/vendor/DROPSHIP/product-shipment/shippedData/fetch/?statusCode=CLD&dispatchCategoryId=0&startDate="+startDate+"&endDate="+endDate+"&statusColumn=cancelledOn";
9931 manish.sha 308
			logger.info("Drop Ship Cancel Url "+dropshipCancelUrl);
9731 manish.sha 309
 
10178 manish.sha 310
			get = new HttpGet(dropshipCancelUrl);
311
 
9931 manish.sha 312
			try {
10178 manish.sha 313
				response = client.execute(get);
9931 manish.sha 314
			} catch (Exception e) {
315
				logger.error("Unable to get Http Response for snapdeal dropship cancelled orders", e);
316
			} 
9651 manish.sha 317
 
9931 manish.sha 318
			try {
10178 manish.sha 319
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9931 manish.sha 320
			} catch (Exception e) {
321
				logger.error("Unable to read Http Response for snapdeal dropship cancelled orders", e);
322
			}
10178 manish.sha 323
 
9931 manish.sha 324
			try {
10565 manish.sha 325
				String resP = rd.readLine();
326
				jsonDataObj = new JSONArray("["+resP+"]");
327
				JSONObject j1 = (JSONObject)jsonDataObj.get(0);
328
				jsonDataShip = new JSONArray(j1.get("jsonDataString").toString());
9931 manish.sha 329
			} catch (Exception e) {
330
				logger.error("Unable to extract Http Response for snapdeal dropship cancelled orders", e);
331
			} 
9731 manish.sha 332
 
9931 manish.sha 333
			if(jsonDataShip!=null && jsonDataShip.length()>0){
10565 manish.sha 334
				for(i=0; i< jsonDataShip.length(); i++){
9931 manish.sha 335
					JSONObject jsonObj = null;
336
					try {
337
						jsonObj = jsonDataShip.getJSONObject(i);
9883 manish.sha 338
 
9931 manish.sha 339
						if(jsonObj!=null){
340
							String subOrderCode =(String) jsonObj.get("suborderCode");
10178 manish.sha 341
							String referenceNumber =(String) jsonObj.get("referenceCode");
9931 manish.sha 342
							logger.info("Snapdeal Dropship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
343
							List<String> cancelledOrdelList = new ArrayList<String>();
344
							cancelledOrdelList.add(referenceNumber);
345
							cancelledOrdelList.add(subOrderCode);
346
							cancelledOrderDataList.add(cancelledOrdelList);
347
						}
348
					} catch (Exception e) {
349
						logger.error("Unable to add cancelled order details for updation regarding snapdeal dropship orders", e);
350
					}
9883 manish.sha 351
				}
352
			}
9931 manish.sha 353
 
354
			logger.info("Getting Information for OneShip Snapdeal Cancelled Orders");
10178 manish.sha 355
			String oneShipCancelUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?statusCode=CLD&dispatchCategoryId=0&startDate="+startDate+"&endDate="+endDate+"&statusColumn=cancelledOn";
9931 manish.sha 356
			logger.info("One Ship Cancel Url "+oneShipCancelUrl);
10178 manish.sha 357
 
358
			get = new HttpGet(oneShipCancelUrl);
359
 
9931 manish.sha 360
			try {
10178 manish.sha 361
				response = client.execute(get);
9931 manish.sha 362
			} catch (Exception e) {
363
				logger.error("Unable to get Http Response for snapdeal oneship cancelled orders", e);
364
			} 
365
 
366
			try {
10178 manish.sha 367
				rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
9931 manish.sha 368
			} catch (Exception e) {
369
				logger.error("Unable to read Http Response for snapdeal oneship cancelled orders", e);
9883 manish.sha 370
			}
10178 manish.sha 371
 
9931 manish.sha 372
			try {
10565 manish.sha 373
				String resP = rd.readLine();
374
				jsonDataObj = new JSONArray("["+resP+"]");
375
				JSONObject j1 = (JSONObject)jsonDataObj.get(0);
376
				jsonDataShip = new JSONArray(j1.get("jsonDataString").toString());
9931 manish.sha 377
			} catch (Exception e) {
378
				logger.error("Unable to extract Http Response for snapdeal oneship cancelled orders", e);
379
			} 
10178 manish.sha 380
 
9931 manish.sha 381
			if(jsonDataShip!=null && jsonDataShip.length()>0){
10565 manish.sha 382
				for(i=0; i< jsonDataShip.length(); i++){
9931 manish.sha 383
					JSONObject jsonObj = null;
384
					try {
385
						jsonObj = jsonDataShip.getJSONObject(i);
386
 
387
						if(jsonObj!=null){
388
							String subOrderCode =(String) jsonObj.get("suborderCode");
10178 manish.sha 389
							String referenceNumber =(String) jsonObj.get("referenceCode");
9931 manish.sha 390
							logger.info("Snapdeal OneShip Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
391
							List<String> cancelledOrdelList = new ArrayList<String>();
392
							cancelledOrdelList.add(referenceNumber);
393
							cancelledOrdelList.add(subOrderCode);
394
							cancelledOrderDataList.add(cancelledOrdelList);
395
						}
396
					} catch (Exception e) {
397
						logger.error("Unable to add cancelled order details for updation regarding snapdeal oneship orders", e);
9883 manish.sha 398
					}
399
				}
400
			}
9931 manish.sha 401
 
10178 manish.sha 402
 
9931 manish.sha 403
			start = end + 86400000l;
9883 manish.sha 404
		}
10178 manish.sha 405
 
9731 manish.sha 406
		if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
9732 manish.sha 407
			logger.info("Delivered Orders Size .. "+deliveredOrderDataList.size());
9883 manish.sha 408
			orderDataMap.put("Delivered", deliveredOrderDataList);
409
		}
10178 manish.sha 410
 
9883 manish.sha 411
		if(cancelledOrderDataList!=null && cancelledOrderDataList.size()>0){
412
			logger.info("Cancelled Orders Size .. "+cancelledOrderDataList.size());
413
			orderDataMap.put("Cancelled", cancelledOrderDataList);
414
		}
10178 manish.sha 415
 
9883 manish.sha 416
		if(orderDataMap!=null && orderDataMap.size()>0){
9731 manish.sha 417
			try{
9883 manish.sha 418
				new TransactionClient().getClient().updateSnapdealOrdersStatus(orderDataMap);
9731 manish.sha 419
			} catch(Exception e){
420
				logger.error("Unable to update delivery information for snapdeal orders", e);
9651 manish.sha 421
			}
422
		}
9731 manish.sha 423
 
9651 manish.sha 424
	}
425
}