Subversion Repositories SmartDukaan

Rev

Rev 15223 | 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
 
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")){
14644 manish.sha 197
									Long deliveryDate =(Long) jsonObj.get("deliveredOn");
198
									logger.info("Snapdeal Oneship Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber+ " ...deliveryDate... "+ deliveryDate);
199
									String deliveryDateStr = ourDBDateFormat.format(new Date(deliveryDate));
200
									List<String> deliveredOrdelList = new ArrayList<String>();
201
									deliveredOrdelList.add(referenceNumber);
202
									deliveredOrdelList.add(subOrderCode);
203
									deliveredOrdelList.add(deliveryDateStr);
204
 
205
									deliveredOrderDataList.add(deliveredOrdelList);
206
								}
12428 manish.sha 207
							}
208
						} catch (Exception e) {
209
							logger.error("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders", e);
210
							throw new Exception("Unable to add delivered order details for updation regarding snapdeal oneship delivered orders");
9931 manish.sha 211
						}
212
					}
9883 manish.sha 213
				}
9931 manish.sha 214
 
12428 manish.sha 215
				logger.info("Getting Information for OneShip Snapdeal Cancelled Orders");
216
				String oneShipCancelUrl = "http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?statusCode=CLD&dispatchCategoryId=0&startDate="+startDate+"&endDate="+endDate+"&statusColumn=cancelledOn";
217
				logger.info("One Ship Cancel Url "+oneShipCancelUrl);
218
 
14636 manish.sha 219
				post = new HttpPost("http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch");
15601 manish.sha 220
				cookies = sdSessionCookie.getCookies();
221
				cookieObject = new JSONObject(cookies);
222
				for(String key:headers.keySet())
223
					post.addHeader(key, headers.get(key));
224
				post.addHeader("Cookie","AWSELB="+cookieObject.get("AWSELB")+";JSESSIONID="+cookieObject.get("JSESSIONID")+";");
14636 manish.sha 225
				nameValuePairs = new ArrayList<NameValuePair>();
226
				nameValuePairs.add(new BasicNameValuePair("startDate",startDate));
227
				nameValuePairs.add(new BasicNameValuePair("endDate",endDate));
228
				nameValuePairs.add(new BasicNameValuePair("statusColumn","cancelledOn"));
229
				nameValuePairs.add(new BasicNameValuePair("queryType","CREATED"));
14641 manish.sha 230
				nameValuePairs.add(new BasicNameValuePair("statusCode","CLD"));
14636 manish.sha 231
				post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
12428 manish.sha 232
 
233
				try {
14636 manish.sha 234
					response = client.execute(post);
12428 manish.sha 235
				} catch (Exception e) {
236
					logger.error("Unable to get Http Response for snapdeal oneship cancelled orders", e);
237
					throw new Exception("Unable to get Http Response for snapdeal oneship cancelled orders");
238
				} 
239
 
240
				try {
241
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
242
				} catch (Exception e) {
243
					logger.error("Unable to read Http Response for snapdeal oneship cancelled orders", e);
244
					throw new Exception("Unable to read Http Response for snapdeal oneship cancelled orders");
9883 manish.sha 245
				}
12428 manish.sha 246
 
247
				try {
14637 manish.sha 248
					String resP;
249
					while((resP=rd.readLine())!=null){
250
						if(resP!=null && !resP.isEmpty()){
251
							jsonDataObj = new JSONArray("["+resP+"]");
252
							JSONObject j1 = (JSONObject)jsonDataObj.get(0);
14643 manish.sha 253
							jsonDataShip = new JSONArray(j1.get("shippedDataDTOs").toString());
14637 manish.sha 254
						}
12446 manish.sha 255
					}
12428 manish.sha 256
				} catch (Exception e) {
257
					logger.error("Unable to extract Http Response for snapdeal oneship cancelled orders", e);
258
					throw new Exception("Unable to extract Http Response for snapdeal oneship cancelled orders");
259
				} 
260
 
261
				if(jsonDataShip!=null && jsonDataShip.length()>0){
262
					for(i=0; i< jsonDataShip.length(); i++){
263
						JSONObject jsonObj = null;
264
						try {
265
							jsonObj = jsonDataShip.getJSONObject(i);
266
 
267
							if(jsonObj!=null){
268
								String subOrderCode =(String) jsonObj.get("suborderCode");
269
								String referenceNumber =(String) jsonObj.get("referenceCode");
270
								logger.info("Snapdeal OneShip Order Details...suborderCode... "+subOrderCode+" ...referenceNumber... "+ referenceNumber);
271
								List<String> cancelledOrdelList = new ArrayList<String>();
272
								cancelledOrdelList.add(referenceNumber);
273
								cancelledOrdelList.add(subOrderCode);
274
								cancelledOrderDataList.add(cancelledOrdelList);
275
							}
276
						} catch (Exception e) {
277
							logger.error("Unable to add cancelled order details for updation regarding snapdeal oneship orders", e);
278
							throw new Exception("Unable to add cancelled order details for updation regarding snapdeal oneship orders");
279
						}
280
					}
281
				}
282
 
283
 
284
				start = end + 86400000l;
9883 manish.sha 285
			}
12428 manish.sha 286
 
287
			if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
288
				logger.info("Delivered Orders Size .. "+deliveredOrderDataList.size());
289
				orderDataMap.put("Delivered", deliveredOrderDataList);
290
			}
291
 
292
			if(cancelledOrderDataList!=null && cancelledOrderDataList.size()>0){
293
				logger.info("Cancelled Orders Size .. "+cancelledOrderDataList.size());
294
				orderDataMap.put("Cancelled", cancelledOrderDataList);
295
			}
296
 
297
			if(orderDataMap!=null && orderDataMap.size()>0){
298
				try{
299
					new TransactionClient().getClient().updateSnapdealOrdersStatus(orderDataMap);
300
				} catch(Exception e){
301
					logger.error("Unable to update delivery information for snapdeal orders", e);
302
					throw new Exception("Unable to update delivery information for snapdeal orders");
303
				}
304
			}
305
 
9883 manish.sha 306
		}
12428 manish.sha 307
		catch(Exception e){
308
			e.printStackTrace();
309
			mailer.sendSSLMessage(sendTo, "Error in Snapdeal Order Status Recon", emailFromAddress, password, "<html><body><p>Please check log</p></body></html>");
9883 manish.sha 310
		}
9651 manish.sha 311
	}
312
}