Subversion Repositories SmartDukaan

Rev

Rev 15601 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9651 manish.sha 1
package in.shop2020;
2
 
15601 manish.sha 3
import in.shop2020.serving.services.SnapdealSessionCookie;
9651 manish.sha 4
import in.shop2020.thrift.clients.TransactionClient;
12428 manish.sha 5
import in.shop2020.utils.GmailUtils;
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
 
12428 manish.sha 19
import javax.mail.MessagingException;
20
 
10178 manish.sha 21
import org.apache.http.HttpResponse;
22
import org.apache.http.NameValuePair;
10565 manish.sha 23
import org.apache.http.client.ClientProtocolException;
10178 manish.sha 24
import org.apache.http.client.HttpClient;
25
import org.apache.http.client.entity.UrlEncodedFormEntity;
26
import org.apache.http.client.methods.HttpGet;
27
import org.apache.http.client.methods.HttpPost;
28
import org.apache.http.impl.client.DefaultHttpClient;
29
import org.apache.http.message.BasicNameValuePair;
9651 manish.sha 30
import org.json.JSONArray;
31
import org.json.JSONObject;
15601 manish.sha 32
import org.slf4j.Logger;
10178 manish.sha 33
import org.slf4j.LoggerFactory;
9651 manish.sha 34
 
10178 manish.sha 35
 
36
 
9651 manish.sha 37
public class SnapdealOrderStatusReconciliation{
38
	private static Logger logger;
12428 manish.sha 39
 
40
	static String emailFromAddress;
41
	static String password;
42
	static GmailUtils mailer;
43
	static String sendTo[];
44
 
15601 manish.sha 45
	private static Map<String, String> headers = new HashMap<String, String>();
46
 
12428 manish.sha 47
	static {
48
		emailFromAddress = "build-staging@shop2020.in";
49
		password = "shop2020";
50
		mailer = new GmailUtils();
51
		sendTo = new String[] { "kshitij.sood@shop2020.in", "manish.sharma@shop2020.in", "amit.gupta@shop2020.in" };
15601 manish.sha 52
		headers.put("User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
53
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
54
		headers.put("Accept-Language", "en-US,en;q=0.8");
55
		headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
12428 manish.sha 56
	}
9651 manish.sha 57
 
12428 manish.sha 58
	public static void main(String[] args) throws UnsupportedEncodingException, MessagingException{
59
		try{
60
			logger = LoggerFactory.getLogger(SnapdealOrderStatusReconciliation.class);
10178 manish.sha 61
 
12428 manish.sha 62
			Map<String,List<List<String>>> orderDataMap = new HashMap<String,List<List<String>>>();
63
			List<List<String>> deliveredOrderDataList = new ArrayList<List<String>>();
64
			List<List<String>> cancelledOrderDataList = new ArrayList<List<String>>();
65
			long minCreationDate = 0l;
66
			long currentTime = System.currentTimeMillis();
15601 manish.sha 67
 
68
			SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
69
			String cookies = sdSessionCookie.getCookies();
70
			JSONObject cookieObject = new JSONObject(cookies);
9651 manish.sha 71
 
12428 manish.sha 72
			Calendar cal = Calendar.getInstance();
73
			cal.add(Calendar.DAY_OF_MONTH, -60);
74
			minCreationDate = cal.getTimeInMillis();
15601 manish.sha 75
 
14636 manish.sha 76
			SimpleDateFormat snapdealDateFormat = new SimpleDateFormat("yyyy/MM/dd");
12428 manish.sha 77
			SimpleDateFormat gotSnapdealDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aaa");
78
			SimpleDateFormat ourDBDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
79
			String startDate= "";
80
			Date start_date = new Date(minCreationDate);
81
			Date end_date = new Date(currentTime);
82
			if(minCreationDate > 0l){
83
				startDate = snapdealDateFormat.format(new Date(minCreationDate));
10565 manish.sha 84
			}
12428 manish.sha 85
			else{
86
				startDate = snapdealDateFormat.format(new Date(currentTime));
10565 manish.sha 87
			}
12428 manish.sha 88
			logger.info("Snapdeal Order Recon Start Date .. "+startDate);
89
			//startDate = startDate.replace("-", "%2F");
9731 manish.sha 90
 
12428 manish.sha 91
			String endDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
92
			logger.info("Snapdeal Order Recon End Date .. "+endDate);
93
			//endDate = endDate.replace("-", "%2F");
10178 manish.sha 94
 
95
 
12428 manish.sha 96
			HttpClient client = new DefaultHttpClient();
10178 manish.sha 97
 
12428 manish.sha 98
			HttpResponse response = null;
99
			BufferedReader rd = null;
100
			String line = "";
14606 manish.sha 101
 
15601 manish.sha 102
			HttpGet get = new HttpGet("http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/pendingOrders/40?fulfillmentModelCode=ONESHIP");
103
			for(String key:headers.keySet())
104
				get.addHeader(key, headers.get(key));
105
			get.addHeader("Cookie","AWSELB="+cookieObject.get("AWSELB")+";JSESSIONID="+cookieObject.get("JSESSIONID")+";");
106
 
14606 manish.sha 107
			try {
108
				response = client.execute(get);
109
			} catch (ClientProtocolException e) {
110
				e.printStackTrace();
111
			} catch (IOException e) {
112
				e.printStackTrace();
113
			}
9651 manish.sha 114
 
14606 manish.sha 115
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
116
 
117
			while((line=rd.readLine())!=null){
118
			}
119
 
120
 
121
 
12428 manish.sha 122
			JSONArray jsonDataObj = null;
123
			JSONArray jsonDataShip = null;
15601 manish.sha 124
 
12428 manish.sha 125
			for(long start = start_date.getTime(); start < end_date.getTime();){
126
				long end = start + 604800000l;
127
				if(end > end_date.getTime()){
128
					end = end_date.getTime();
129
				}
130
				startDate = snapdealDateFormat.format(new Date(start));
131
				endDate = snapdealDateFormat.format(new Date(end));
132
 
133
				logger.info("==== Start Date.."+startDate+"\n");
134
				logger.info("==== End Date.."+endDate+"\n");
15601 manish.sha 135
 
136
				int i;
12428 manish.sha 137
 
138
				logger.info("Getting Delivery Information for OneShip Snapdeal Orders");
139
				//statusCode=CLD&dispatchCategoryId=0&startDate=2013%2F11%2F01&endDate=2013%2F11%2F07&statusColumn=cancelledOn
14641 manish.sha 140
				String oneShipUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?startDate="+startDate+"&endDate="+endDate+"&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0";
141
				logger.info("One Ship Url "+oneShipUrl);
14636 manish.sha 142
 
15601 manish.sha 143
				HttpPost post = new HttpPost("http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch");
144
				cookies = sdSessionCookie.getCookies();
145
				cookieObject = new JSONObject(cookies);
146
				for(String key:headers.keySet())
147
					post.addHeader(key, headers.get(key));
148
				post.addHeader("Cookie","AWSELB="+cookieObject.get("AWSELB")+";JSESSIONID="+cookieObject.get("JSESSIONID")+";");				
149
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
14636 manish.sha 150
				nameValuePairs.add(new BasicNameValuePair("startDate",startDate));
151
				nameValuePairs.add(new BasicNameValuePair("endDate",endDate));
152
				nameValuePairs.add(new BasicNameValuePair("statusColumn","deliveredOn"));
14641 manish.sha 153
				nameValuePairs.add(new BasicNameValuePair("queryType","CREATED"));
154
				nameValuePairs.add(new BasicNameValuePair("statusCode","COURIER_DELIVERED"));
155
				post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
156
 
12428 manish.sha 157
 
158
				try {
14636 manish.sha 159
					response = client.execute(post);
12428 manish.sha 160
				} catch (Exception e) {
161
					logger.error("Unable to get Http Response for snapdeal oneship delivered orders", e);
162
					throw new Exception("Unable to get Http Response for snapdeal oneship delivered orders");
163
				} 
164
 
165
				try {
166
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
167
				} catch (Exception e) {
168
					logger.error("Unable to read Http Response for snapdeal oneship delivered orders", e);
169
					throw new Exception("Unable to read Http Response for snapdeal oneship delivered orders");
9651 manish.sha 170
				}
9731 manish.sha 171
 
12428 manish.sha 172
				try {
14637 manish.sha 173
					String resP;
174
					while((resP=rd.readLine())!=null){
175
						if(resP!=null && !resP.isEmpty()){
14638 manish.sha 176
							System.out.println("Response... "+resP);
14637 manish.sha 177
							jsonDataObj = new JSONArray("["+resP+"]");
178
							JSONObject j1 = (JSONObject)jsonDataObj.get(0);
14642 manish.sha 179
							jsonDataShip = new JSONArray(j1.get("shippedDataDTOs").toString());
14637 manish.sha 180
						}
12446 manish.sha 181
					}
12428 manish.sha 182
				} catch (Exception e) {
183
					logger.error("Unable to extract Http Response for snapdeal oneship delivered orders", e);
184
					throw new Exception("Unable to extract Http Response for snapdeal oneship delivered orders");
185
				}
9651 manish.sha 186
 
12428 manish.sha 187
				if(jsonDataShip!=null && jsonDataShip.length()>0){
188
					for(i=0; i< jsonDataShip.length(); i++){
189
						JSONObject jsonObj = null;
190
						try {
191
							jsonObj = jsonDataShip.getJSONObject(i);
9731 manish.sha 192
 
12428 manish.sha 193
							if(jsonObj!=null){
194
								String subOrderCode =(String) jsonObj.get("suborderCode");
195
								String referenceNumber =(String) jsonObj.get("referenceCode");
14645 manish.sha 196
								if(!jsonObj.get("deliveredOn").toString().equalsIgnoreCase("null")){
15605 manish.sha 197
									try{
198
										Long deliveryDate =(Long) jsonObj.get("deliveredOn");
199
										logger.info("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
200
										String deliveryDateStr = ourDBDateFormat.format(new Date(deliveryDate));
201
										List<String> deliveredOrdelList = new ArrayList<String>();
202
										deliveredOrdelList.add(referenceNumber);
203
										deliveredOrdelList.add(subOrderCode);
204
										deliveredOrdelList.add(deliveryDateStr);
205
 
206
										deliveredOrderDataList.add(deliveredOrdelList);
207
									} catch (Exception e){
208
										Integer deliveryDate =(Integer) jsonObj.get("deliveredOn");
209
										logger.info("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
210
										String deliveryDateStr = ourDBDateFormat.format(new Date(deliveryDate));
211
										List<String> deliveredOrdelList = new ArrayList<String>();
212
										deliveredOrdelList.add(referenceNumber);
213
										deliveredOrdelList.add(subOrderCode);
214
										deliveredOrdelList.add(deliveryDateStr);
215
 
216
										deliveredOrderDataList.add(deliveredOrdelList);
217
										continue;
218
									}
14644 manish.sha 219
								}
12428 manish.sha 220
							}
221
						} catch (Exception e) {
222
							logger.error("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders", e);
223
							throw new Exception("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders");
9931 manish.sha 224
						}
225
					}
9883 manish.sha 226
				}
9931 manish.sha 227
 
12428 manish.sha 228
				logger.info("Getting Information for OneShip Snapdeal Cancelled Orders");
229
				String oneShipCancelUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?statusCode=CLD&dispatchCategoryId=0&startDate="+startDate+"&endDate="+endDate+"&statusColumn=cancelledOn";
230
				logger.info("One Ship Cancel Url "+oneShipCancelUrl);
231
 
14636 manish.sha 232
				post = new HttpPost("http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch");
15601 manish.sha 233
				cookies = sdSessionCookie.getCookies();
234
				cookieObject = new JSONObject(cookies);
235
				for(String key:headers.keySet())
236
					post.addHeader(key, headers.get(key));
237
				post.addHeader("Cookie","AWSELB="+cookieObject.get("AWSELB")+";JSESSIONID="+cookieObject.get("JSESSIONID")+";");
14636 manish.sha 238
				nameValuePairs = new ArrayList<NameValuePair>();
239
				nameValuePairs.add(new BasicNameValuePair("startDate",startDate));
240
				nameValuePairs.add(new BasicNameValuePair("endDate",endDate));
241
				nameValuePairs.add(new BasicNameValuePair("statusColumn","cancelledOn"));
242
				nameValuePairs.add(new BasicNameValuePair("queryType","CREATED"));
14641 manish.sha 243
				nameValuePairs.add(new BasicNameValuePair("statusCode","CLD"));
14636 manish.sha 244
				post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
12428 manish.sha 245
 
246
				try {
14636 manish.sha 247
					response = client.execute(post);
12428 manish.sha 248
				} catch (Exception e) {
249
					logger.error("Unable to get Http Response for snapdeal oneship cancelled orders", e);
250
					throw new Exception("Unable to get Http Response for snapdeal oneship cancelled orders");
251
				} 
252
 
253
				try {
254
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
255
				} catch (Exception e) {
256
					logger.error("Unable to read Http Response for snapdeal oneship cancelled orders", e);
257
					throw new Exception("Unable to read Http Response for snapdeal oneship cancelled orders");
9883 manish.sha 258
				}
12428 manish.sha 259
 
260
				try {
14637 manish.sha 261
					String resP;
262
					while((resP=rd.readLine())!=null){
263
						if(resP!=null && !resP.isEmpty()){
264
							jsonDataObj = new JSONArray("["+resP+"]");
265
							JSONObject j1 = (JSONObject)jsonDataObj.get(0);
14643 manish.sha 266
							jsonDataShip = new JSONArray(j1.get("shippedDataDTOs").toString());
14637 manish.sha 267
						}
12446 manish.sha 268
					}
12428 manish.sha 269
				} catch (Exception e) {
270
					logger.error("Unable to extract Http Response for snapdeal oneship cancelled orders", e);
271
					throw new Exception("Unable to extract Http Response for snapdeal oneship cancelled orders");
272
				} 
273
 
274
				if(jsonDataShip!=null && jsonDataShip.length()>0){
275
					for(i=0; i< jsonDataShip.length(); i++){
276
						JSONObject jsonObj = null;
277
						try {
278
							jsonObj = jsonDataShip.getJSONObject(i);
279
 
280
							if(jsonObj!=null){
281
								String subOrderCode =(String) jsonObj.get("suborderCode");
282
								String referenceNumber =(String) jsonObj.get("referenceCode");
283
								logger.info("Snapdeal OneShip Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
284
								List<String> cancelledOrdelList = new ArrayList<String>();
285
								cancelledOrdelList.add(referenceNumber);
286
								cancelledOrdelList.add(subOrderCode);
287
								cancelledOrderDataList.add(cancelledOrdelList);
288
							}
289
						} catch (Exception e) {
290
							logger.error("Unable to add cancelled order details for updation regarding snapdeal oneship orders", e);
291
							throw new Exception("Unable to add cancelled order details for updation regarding snapdeal oneship orders");
292
						}
293
					}
294
				}
295
 
296
 
297
				start = end + 86400000l;
9883 manish.sha 298
			}
12428 manish.sha 299
 
300
			if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
301
				logger.info("Delivered Orders Size .. "+deliveredOrderDataList.size());
302
				orderDataMap.put("Delivered", deliveredOrderDataList);
303
			}
304
 
305
			if(cancelledOrderDataList!=null && cancelledOrderDataList.size()>0){
306
				logger.info("Cancelled Orders Size .. "+cancelledOrderDataList.size());
307
				orderDataMap.put("Cancelled", cancelledOrderDataList);
308
			}
309
 
310
			if(orderDataMap!=null && orderDataMap.size()>0){
311
				try{
312
					new TransactionClient().getClient().updateSnapdealOrdersStatus(orderDataMap);
313
				} catch(Exception e){
314
					logger.error("Unable to update delivery information for snapdeal orders", e);
315
					throw new Exception("Unable to update delivery information for snapdeal orders");
316
				}
317
			}
318
 
9883 manish.sha 319
		}
12428 manish.sha 320
		catch(Exception e){
321
			e.printStackTrace();
322
			mailer.sendSSLMessage(sendTo, "Error in Snapdeal Order Status Recon", emailFromAddress, password, "<html><body><p>Please check log</p></body></html>");
9883 manish.sha 323
		}
9651 manish.sha 324
	}
325
}