Subversion Repositories SmartDukaan

Rev

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