Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
8285 kshitij.so 1
package com.amazonaws.mws.samples;
2
 
11172 vikram.rag 3
import in.shop2020.model.v1.inventory.AmazonFCWarehouseLocation;
8285 kshitij.so 4
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
8532 vikram.rag 5
import in.shop2020.thrift.clients.CatalogClient;
8285 kshitij.so 6
import in.shop2020.thrift.clients.InventoryClient;
7
 
11172 vikram.rag 8
import java.io.BufferedReader;
8285 kshitij.so 9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.FileReader;
12
import java.io.IOException;
13
import java.io.OutputStream;
9482 vikram.rag 14
import java.util.ArrayList;
8285 kshitij.so 15
import java.util.Arrays;
16
import java.util.GregorianCalendar;
17
import java.util.List;
18
import java.util.Map;
19
 
20
import javax.xml.datatype.DatatypeConfigurationException;
21
import javax.xml.datatype.DatatypeFactory;
22
import javax.xml.datatype.XMLGregorianCalendar;
23
 
24
import org.apache.thrift.TException;
25
import org.apache.thrift.transport.TTransportException;
26
 
27
import au.com.bytecode.opencsv.CSVReader;
28
 
29
import com.amazonaws.mws.MarketplaceWebService;
30
import com.amazonaws.mws.MarketplaceWebServiceClient;
31
import com.amazonaws.mws.MarketplaceWebServiceConfig;
8363 vikram.rag 32
import com.amazonaws.mws.MarketplaceWebServiceException;
8285 kshitij.so 33
import com.amazonaws.mws.model.GetReportListRequest;
34
import com.amazonaws.mws.model.GetReportRequest;
35
import com.amazonaws.mws.model.IdList;
36
import com.amazonaws.mws.model.RequestReportRequest;
37
 
38
public class FetchAmazonInventory {
39
	public static void main(String... args){
40
		/************************************************************************
8532 vikram.rag 41
		 * Access Key ID and Secret Access Key ID, obtained from:
42
		 * http://aws.amazon.com
43
		 ***********************************************************************/
8285 kshitij.so 44
		final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
45
		final String secretAccessKey = "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";
46
 
47
		final String appName = "Test";
48
		final String appVersion = "1.0";
49
		final String merchantId = "AF6E3O0VE0X4D";
50
 
8532 vikram.rag 51
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
8285 kshitij.so 52
 
8532 vikram.rag 53
		/************************************************************************
54
		 * Uncomment to set the appropriate MWS endpoint.
55
		 ************************************************************************/
56
		// US
57
		// config.setServiceURL("https://mws.amazonservices.com");
58
		// UK
59
		// config.setServiceURL("https://mws.amazonservices.co.uk");
60
		// Germany
61
		// config.setServiceURL("https://mws.amazonservices.de");
62
		// France
63
		// config.setServiceURL("https://mws.amazonservices.fr");
64
		// Italy
65
		// config.setServiceURL("https://mws.amazonservices.it");
66
		// Japan
67
		// config.setServiceURL("https://mws.amazonservices.jp");
68
		// China
69
		// config.setServiceURL("https://mws.amazonservices.com.cn");
70
		// Canada
71
		// config.setServiceURL("https://mws.amazonservices.ca");
72
		// India
73
		config.setServiceURL("https://mws.amazonservices.in");
8285 kshitij.so 74
 
8532 vikram.rag 75
		/************************************************************************
76
		 * You can also try advanced configuration options. Available options are:
77
		 *
78
		 *  - Signature Version
79
		 *  - Proxy Host and Proxy Port
80
		 *  - User Agent String to be sent to Marketplace Web Service
81
		 *
82
		 ***********************************************************************/
8285 kshitij.so 83
 
8532 vikram.rag 84
		/************************************************************************
85
		 * Instantiate Http Client Implementation of Marketplace Web Service        
86
		 ***********************************************************************/
8285 kshitij.so 87
 
8532 vikram.rag 88
		MarketplaceWebService service = new MarketplaceWebServiceClient(
89
				accessKeyId, secretAccessKey, appName, appVersion, config);
8285 kshitij.so 90
 
8532 vikram.rag 91
		/************************************************************************
92
		 * Uncomment to try out Mock Service that simulates Marketplace Web Service 
93
		 * responses without calling Marketplace Web Service  service.
94
		 *
95
		 * Responses are loaded from local XML files. You can tweak XML files to
96
		 * experiment with various outputs during development
97
		 *
98
		 * XML files available under com/amazonaws/mws/mock tree
99
		 *
100
		 ***********************************************************************/
101
		// MarketplaceWebService service = new MarketplaceWebServiceMock();
102
 
103
		/************************************************************************
104
		 * Setup request parameters and uncomment invoke to try out 
105
		 * sample for Request Report 
106
		 ***********************************************************************/
107
 
108
		/************************************************************************
109
		 * Marketplace and Merchant IDs are required parameters for all 
110
		 * Marketplace Web Service calls.
111
		 ***********************************************************************/
112
		// marketplaces from which data should be included in the report; look at the
113
		// API reference document on the MWS website to see which marketplaces are
114
		// included if you do not specify the list yourself
115
		final IdList marketplaces = new IdList(Arrays.asList(
8285 kshitij.so 116
		"A21TJRUUN4KGV"));        
8532 vikram.rag 117
		RequestReportRequest request = new RequestReportRequest()
118
		.withMerchant(merchantId)
119
		.withMarketplaceIdList(marketplaces)
11172 vikram.rag 120
		.withReportType("_GET_FBA_MYI_ALL_INVENTORY_DATA_")
8532 vikram.rag 121
		.withReportOptions("ShowSalesChannel=true");
8285 kshitij.so 122
 
8532 vikram.rag 123
		// demonstrates how to set the date range
8285 kshitij.so 124
		DatatypeFactory df = null;
125
		try {
126
			df = DatatypeFactory.newInstance();
127
		} catch (DatatypeConfigurationException e) {
128
			e.printStackTrace();
129
			throw new RuntimeException(e);
130
		}
131
		//XMLGregorianCalendar startDate = df.newXMLGregorianCalendar(new GregorianCalendar());
132
		//XMLGregorianCalendar endDate = df.newXMLGregorianCalendar(new GregorianCalendar());
133
		//request.setStartDate(startDate);
134
		//request.setEndDate(endDate);
8532 vikram.rag 135
		// @TODO: set additional request parameters here
8285 kshitij.so 136
		Map<String,String> requestIdreportIdmap;
137
		///Request report
138
		while(true){
8363 vikram.rag 139
			String requestId = null;
140
			try {
141
				requestId = RequestReportSample.invokeRequestReport(service, request);
142
			} catch (MarketplaceWebServiceException e1) {
143
				// TODO Auto-generated catch block
144
				e1.printStackTrace();
8472 vikram.rag 145
				continue;
8363 vikram.rag 146
			}
8285 kshitij.so 147
			while(true){
148
				GetReportListRequest requestreportlist = new GetReportListRequest();
149
				requestreportlist.setMerchant( merchantId );
150
				final IdList requestIdList = new IdList(Arrays.asList(requestId));        
151
				requestreportlist.setReportRequestIdList(requestIdList);
152
				///Request report status
153
				requestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);
154
 
155
				GetReportRequest requestreport = new GetReportRequest();
156
				requestreport.setMerchant( merchantId );
157
 
158
				///Fetch report only if it is ready
159
				if(requestIdreportIdmap.get(requestId)!=null){
160
					requestreport.setReportId( requestIdreportIdmap.get(requestId) );
161
					OutputStream report=null;
162
					try {
15704 kshitij.so 163
						report = new FileOutputStream( "/tmp/amazoninventoryreport.csv" );
8285 kshitij.so 164
					} catch (FileNotFoundException e) {
165
						// TODO Auto-generated catch block
166
						e.printStackTrace();
167
					}
168
					requestreport.setReportOutputStream( report );
169
					GetReportSample.invokeGetReport(service, requestreport);
8363 vikram.rag 170
					//System.out.println("Report ready please check");
11172 vikram.rag 171
					BufferedReader reader = null;
8285 kshitij.so 172
					try {
15704 kshitij.so 173
						reader = new BufferedReader(new FileReader("/tmp/amazoninventoryreport.csv"));
11172 vikram.rag 174
					} catch (FileNotFoundException e1) {
8285 kshitij.so 175
						// TODO Auto-generated catch block
11172 vikram.rag 176
						e1.printStackTrace();
8285 kshitij.so 177
					}
11172 vikram.rag 178
					String[] nextLine = null;
179
					String Line;
9482 vikram.rag 180
					List<AmazonFbaInventorySnapshot> allamazoninventory = new ArrayList<AmazonFbaInventorySnapshot>();
8285 kshitij.so 181
					try {
11172 vikram.rag 182
						while ((Line = reader.readLine()) != null) {
183
							nextLine = Line.split("\t");
184
							System.out.println("SKU " + nextLine[0] + " "+ nextLine[10] +" "+nextLine[11]+" "+nextLine[12]+" "+nextLine[16]);
185
							if(nextLine[0].startsWith("FBA")){
186
								AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
187
								if(!nextLine[10].isEmpty())
188
									amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));
189
								else{
190
									amazonfbainventorysnapshot.setAvailability(0);
191
								}
15032 kshitij.so 192
								try{
11172 vikram.rag 193
								amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));
15032 kshitij.so 194
								}
195
								catch(Exception e){
196
                                    continue;
197
                                }
11172 vikram.rag 198
								amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Mumbai);
199
								if(nextLine[12].length()>0){
200
									amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
201
								}
202
								else{
203
									amazonfbainventorysnapshot.setReserved(0);
204
								}
205
								if(nextLine[11].length()>0){
206
									amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
207
								}
208
								else{
209
									amazonfbainventorysnapshot.setUnfulfillable(0);
210
								}
211
								if(nextLine[16].length()>0){
212
									amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
213
								}
214
								else{
215
									amazonfbainventorysnapshot.setInbound(0);
216
								}
217
								allamazoninventory.add(amazonfbainventorysnapshot); 
9482 vikram.rag 218
 
8285 kshitij.so 219
							}
11172 vikram.rag 220
							else if(nextLine[0].startsWith("FBB")){
11497 vikram.rag 221
 
11172 vikram.rag 222
								AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
11497 vikram.rag 223
								try{
224
									amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));
225
									amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBB","")));
226
									amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Bangalore);
227
									amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
228
									amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
229
									amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
230
								}
231
								catch(Exception ex){
232
									continue;
233
								}
11172 vikram.rag 234
								allamazoninventory.add(amazonfbainventorysnapshot); 
235
							}
12897 kshitij.so 236
							else if(nextLine[0].startsWith("FBG")){
237
                                AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
238
                                if(!nextLine[10].isEmpty())
239
                                    amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));
240
                                else{
241
                                    amazonfbainventorysnapshot.setAvailability(0);
242
                                }
15032 kshitij.so 243
                                try{
12897 kshitij.so 244
                                amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBG","")));
15032 kshitij.so 245
                                }
246
                                catch(Exception e){
247
                                    continue;
248
                                }
12897 kshitij.so 249
                                amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Gurgaon);
250
                                if(nextLine[12].length()>0){
251
                                    amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
252
                                }
253
                                else{
254
                                    amazonfbainventorysnapshot.setReserved(0);
255
                                }
256
                                if(nextLine[11].length()>0){
257
                                    amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
258
                                }
259
                                else{
260
                                    amazonfbainventorysnapshot.setUnfulfillable(0);
261
                                }
262
                                if(nextLine[16].length()>0){
263
                                    amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
264
                                }
265
                                else{
266
                                    amazonfbainventorysnapshot.setInbound(0);
267
                                }
268
                                allamazoninventory.add(amazonfbainventorysnapshot); 
269
                            }
15704 kshitij.so 270
							else if(nextLine[0].startsWith("FBD")){
271
                                AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
272
                                if(!nextLine[10].isEmpty())
273
                                    amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));
274
                                else{
275
                                    amazonfbainventorysnapshot.setAvailability(0);
276
                                }
277
                                try{
278
                                amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBD","")));
279
                                }
280
                                catch(Exception e){
281
                                    continue;
282
                                }
283
                                amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Delhi);
284
                                if(nextLine[12].length()>0){
285
                                    amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
286
                                }
287
                                else{
288
                                    amazonfbainventorysnapshot.setReserved(0);
289
                                }
290
                                if(nextLine[11].length()>0){
291
                                    amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
292
                                }
293
                                else{
294
                                    amazonfbainventorysnapshot.setUnfulfillable(0);
295
                                }
296
                                if(nextLine[16].length()>0){
297
                                    amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
298
                                }
299
                                else{
300
                                    amazonfbainventorysnapshot.setInbound(0);
301
                                }
302
                                allamazoninventory.add(amazonfbainventorysnapshot); 
303
                            }
304
 
11498 vikram.rag 305
							else{
306
								continue;
307
							}
8285 kshitij.so 308
						}
11548 vikram.rag 309
						try{
310
							new InventoryClient().getClient().addOrUpdateAllAmazonFbaInventory(allamazoninventory);
311
						}
312
						catch(Exception ex){
313
							try {
314
								new InventoryClient().getClient().addOrUpdateAllAmazonFbaInventory(allamazoninventory);
315
							} catch (Exception e) {
316
								e.printStackTrace();
317
							}
318
						}
8285 kshitij.so 319
					} catch (IOException e) {
320
						// TODO Auto-generated catch block
321
						e.printStackTrace();
322
					}
323
					break;
324
				}
325
				else{ 
8363 vikram.rag 326
					//System.out.println("Report not ready");
8285 kshitij.so 327
					try {
328
						Thread.sleep(5*60*1000);
329
					} catch (InterruptedException e) {
330
						// TODO Auto-generated catch block
331
						e.printStackTrace();
332
					}
333
				}
334
			}
335
			try {
336
				Thread.sleep(30*60*1000);
337
			} catch (InterruptedException e) {
338
				// TODO Auto-generated catch block
339
				e.printStackTrace();
340
			}
341
		}
342
 
8532 vikram.rag 343
		// Note that depending on the type of report being downloaded, a report can reach 
344
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
345
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
346
		// the in-memory size limit and have to re-work your solution.
347
		//
8285 kshitij.so 348
 
8532 vikram.rag 349
 
8285 kshitij.so 350
	}
351
 
352
}