Subversion Repositories SmartDukaan

Rev

Rev 9700 | Go to most recent revision | Details | 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.Order;
4
import in.shop2020.model.v1.order.OrderSource;
5
import in.shop2020.model.v1.order.OrderStatus;
6
import in.shop2020.model.v1.order.SnapdealOrder;
7
import in.shop2020.thrift.clients.TransactionClient;
8
 
9
import java.io.BufferedReader;
10
import java.io.IOException;
11
import java.io.InputStreamReader;
12
import java.io.UnsupportedEncodingException;
13
import java.text.SimpleDateFormat;
14
import java.util.ArrayList;
15
import java.util.Date;
16
import java.util.List;
17
 
18
import org.apache.http.HttpResponse;
19
import org.apache.http.NameValuePair;
20
import org.apache.http.client.ClientProtocolException;
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;
27
import org.apache.http.params.HttpParams;
28
import org.json.JSONArray;
29
import org.json.JSONException;
30
import org.json.JSONObject;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33
 
34
public class SnapdealOrderStatusReconciliation{
35
	private static Logger logger;
36
 
37
	public static void main(String[] args) throws UnsupportedEncodingException{
38
		logger = LoggerFactory.getLogger(SnapdealOrderStatusReconciliation.class);
39
		List<OrderStatus> statuses = new ArrayList<OrderStatus>();
40
		statuses.add(OrderStatus.SHIPPED_FROM_WH);
41
		statuses.add(OrderStatus.SHIPPED_TO_LOGST);
42
 
43
		List<List<String>> deliveredOrderDataList = new ArrayList<List<String>>();
44
		long minCreationDate = 0l;
45
		TransactionClient tsc = null;
46
		try {
47
			tsc = new TransactionClient();
48
			minCreationDate = tsc.getClient().getMinCreatedTimeStampUndeliveredOrdersForSource(OrderSource.SNAPDEAL.getValue());
49
		} catch (Exception e) {
50
			logger.error("Unable to establish connection to the transaction service while getting Minimum Order Created Timstamp for Undelivered Orders ", e);
51
		}//Dec 14, 2013 12:00:00 AM
52
 
53
		SimpleDateFormat snapdealDateFormat = new SimpleDateFormat("yyyy-MM-dd");
54
		SimpleDateFormat gotSnapdealDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aaa");
55
		SimpleDateFormat ourDBDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
56
		String startDate= "";
57
		if(minCreationDate > 0l){
58
			startDate = snapdealDateFormat.format(new Date(minCreationDate));
59
		}
60
		else{
61
			startDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
62
		}
63
		logger.info("Snapdeal Order Recon Start Date .. "+startDate);
64
		startDate = startDate.replace("-", "%2F");
65
 
66
		String endDate = snapdealDateFormat.format(new Date(System.currentTimeMillis()));
67
		logger.info("Snapdeal Order Recon End Date .. "+endDate);
68
		endDate = endDate.replace("-", "%2F");
69
 
70
 
71
		HttpClient client = new DefaultHttpClient();
72
		HttpPost post = new HttpPost("http://shipping.snapdeal.com/login_security_check?spring-security-redirect=http://shipping.snapdeal.com/vendor/product-shipment/shippingDashboard&");
73
		HttpGet get;
74
		BufferedReader rd= null;
75
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
76
		nameValuePairs.add(new BasicNameValuePair("j_username",
77
		"khushal.bhatia@saholic.com"));
78
		nameValuePairs.add(new BasicNameValuePair("j_password",
79
		"s@h0l1c"));
80
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
81
		HttpResponse response = null;
82
		try {
83
			response = client.execute(post);
84
		} catch (ClientProtocolException e) {
85
			e.printStackTrace();
86
		} catch (IOException e) {
87
			e.printStackTrace();
88
		}
89
		try {
90
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
91
		} catch (IllegalStateException e1) {
92
			e1.printStackTrace();
93
		} catch (IOException e1) {
94
			e1.printStackTrace();
95
		}
96
		String line = "";
97
		try {
98
			while ((line = rd.readLine()) != null) {
99
				System.out.println(line);
100
			}
101
		} catch (IOException e) {
102
			e.printStackTrace();
103
		}
104
 
105
		//http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?shippedStartDate=2014%2F01%2F01&shippedEndDate=2014%2F01%2F08&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED
106
 
107
		logger.info("Getting Delivery Information for DropShip Snapdeal Orders");
108
		get = new HttpGet("http://shipping.snapdeal.com/vendor/DROPSHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED");
109
 
110
		try {
111
			response = client.execute(get);
112
		} catch (ClientProtocolException e) {
113
			// TODO Auto-generated catch block
114
			e.printStackTrace();
115
		} catch (IOException e) {
116
			// TODO Auto-generated catch block
117
			e.printStackTrace();
118
		}
119
 
120
		try {
121
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
122
		} catch (IllegalStateException e) {
123
			e.printStackTrace();
124
		} catch (IOException e) {
125
			e.printStackTrace();
126
		}
127
 
128
		JSONArray jsonDataShip = null;
129
		String line1 = "";
130
		try {
131
			while ((line1 = rd.readLine()) != null) {
132
				if(line1.contains("jsonString")){
133
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
134
					logger.info("Response get from SnapDeal DropShip.. "+jsonString);
135
					jsonDataShip = new JSONArray(jsonString);
136
					break;
137
				}
138
			}
139
		} catch (IOException e) {
140
			e.printStackTrace();
141
		} catch (JSONException e) {
142
			e.printStackTrace();
143
		}
144
 
145
		if(jsonDataShip!=null && jsonDataShip.length()>0){
146
			for(int i=0; i< jsonDataShip.length(); i++){
147
				JSONObject jsonObj = null;
148
				try {
149
					jsonObj = jsonDataShip.getJSONObject(i);
150
 
151
					if(jsonObj!=null){
152
						System.out.println(jsonObj);
153
						String subOrderCode =(String) jsonObj.get("suborderCode");
154
						String referenceNumber =(String) jsonObj.get("referenceNumber");
155
						String deliveryDate =(String) jsonObj.get("deliveredOn");
156
						deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
157
						List<String> deliveredOrdelList = new ArrayList<String>();
158
						deliveredOrdelList.add(referenceNumber);
159
						deliveredOrdelList.add(subOrderCode);
160
						deliveredOrdelList.add(deliveryDate);
161
 
162
						deliveredOrderDataList.add(deliveredOrdelList);
163
					}
164
				} catch (Exception e) {
165
					e.printStackTrace();
166
				}
167
			}
168
		}
169
 
170
		logger.info("Getting Delivery Information for OneShip Snapdeal Orders");
171
		get = new HttpGet("http://shipping.snapdeal.com/vendor/ONESHIP/product-shipment/shippedData/fetch/?shippedStartDate="+startDate+"&shippedEndDate="+endDate+"&specialPanelAccess=&statusCode=COURIER_DELIVERED&statusColumn=deliveredOn&dispatchCategoryId=0&sCode=COURIER_DELIVERED");
172
 
173
		try {
174
			response = client.execute(get);
175
		} catch (ClientProtocolException e) {
176
			// TODO Auto-generated catch block
177
			e.printStackTrace();
178
		} catch (IOException e) {
179
			// TODO Auto-generated catch block
180
			e.printStackTrace();
181
		}
182
 
183
		try {
184
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
185
		} catch (IllegalStateException e) {
186
			e.printStackTrace();
187
		} catch (IOException e) {
188
			e.printStackTrace();
189
		}
190
 
191
		try {
192
			while ((line1 = rd.readLine()) != null) {
193
				if(line1.contains("jsonString")){
194
					String jsonString = line1.substring(line1.indexOf("["), line1.indexOf("'/>"));
195
					logger.info("Response get from SnapDeal OneShip.. "+jsonString);
196
					jsonDataShip = new JSONArray(jsonString);
197
					break;
198
				}
199
			}
200
		} catch (IOException e) {
201
			e.printStackTrace();
202
		} catch (JSONException e) {
203
			e.printStackTrace();
204
		}
205
 
206
		if(jsonDataShip!=null && jsonDataShip.length()>0){
207
			for(int i=0; i< jsonDataShip.length(); i++){
208
				JSONObject jsonObj = null;
209
				try {
210
					jsonObj = jsonDataShip.getJSONObject(i);
211
 
212
					if(jsonObj!=null){
213
						System.out.println(jsonObj);
214
						String subOrderCode =(String) jsonObj.get("suborderCode");
215
						String referenceNumber =(String) jsonObj.get("referenceNumber");
216
						String deliveryDate =(String) jsonObj.get("deliveredOn");
217
						deliveryDate = ourDBDateFormat.format(gotSnapdealDateFormat.parse(deliveryDate).getTime());
218
						List<String> deliveredOrdelList = new ArrayList<String>();
219
						deliveredOrdelList.add(referenceNumber);
220
						deliveredOrdelList.add(subOrderCode);
221
						deliveredOrdelList.add(deliveryDate);
222
 
223
						deliveredOrderDataList.add(deliveredOrdelList);
224
					}
225
				} catch (Exception e) {
226
					// TODO Auto-generated catch block
227
					e.printStackTrace();
228
				}
229
			}
230
		}
231
		if(tsc!=null){
232
			if(deliveredOrderDataList!=null && deliveredOrderDataList.size()>0){
233
				try{
234
					tsc.getClient().updateSnapdealOrdersStatus(deliveredOrderDataList);
235
				} catch(Exception e){
236
					e.printStackTrace();
237
				}
238
			}
239
		}
240
	}
241
}