Subversion Repositories SmartDukaan

Rev

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