Subversion Repositories SmartDukaan

Rev

Rev 11497 | Rev 11548 | Go to most recent revision | 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 {
8363 vikram.rag 163
						report = new FileOutputStream( "/home/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 {
11172 vikram.rag 173
						reader = new BufferedReader(new FileReader("/home/amazoninventoryreport.csv"));
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
								}
192
								amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));
193
								amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Mumbai);
194
								if(nextLine[12].length()>0){
195
									amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
196
								}
197
								else{
198
									amazonfbainventorysnapshot.setReserved(0);
199
								}
200
								if(nextLine[11].length()>0){
201
									amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
202
								}
203
								else{
204
									amazonfbainventorysnapshot.setUnfulfillable(0);
205
								}
206
								if(nextLine[16].length()>0){
207
									amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
208
								}
209
								else{
210
									amazonfbainventorysnapshot.setInbound(0);
211
								}
212
								allamazoninventory.add(amazonfbainventorysnapshot); 
9482 vikram.rag 213
 
8285 kshitij.so 214
							}
11172 vikram.rag 215
							else if(nextLine[0].startsWith("FBB")){
11497 vikram.rag 216
 
11172 vikram.rag 217
								AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
11497 vikram.rag 218
								try{
219
									amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));
220
									amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBB","")));
221
									amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Bangalore);
222
									amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));
223
									amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));
224
									amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));
225
								}
226
								catch(Exception ex){
227
									continue;
228
								}
11172 vikram.rag 229
								allamazoninventory.add(amazonfbainventorysnapshot); 
230
							}
11498 vikram.rag 231
							else{
232
								continue;
233
							}
8285 kshitij.so 234
						}
11172 vikram.rag 235
						new InventoryClient().getClient().addOrUpdateAllAmazonFbaInventory(allamazoninventory);
8285 kshitij.so 236
					} catch (IOException e) {
237
						// TODO Auto-generated catch block
238
						e.printStackTrace();
239
					} catch (TException e) {
240
						// TODO Auto-generated catch block
241
						e.printStackTrace();
242
					}
243
					break;
244
				}
245
				else{ 
8363 vikram.rag 246
					//System.out.println("Report not ready");
8285 kshitij.so 247
					try {
248
						Thread.sleep(5*60*1000);
249
					} catch (InterruptedException e) {
250
						// TODO Auto-generated catch block
251
						e.printStackTrace();
252
					}
253
				}
254
			}
255
			try {
256
				Thread.sleep(30*60*1000);
257
			} catch (InterruptedException e) {
258
				// TODO Auto-generated catch block
259
				e.printStackTrace();
260
			}
261
		}
262
 
8532 vikram.rag 263
		// Note that depending on the type of report being downloaded, a report can reach 
264
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
265
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
266
		// the in-memory size limit and have to re-work your solution.
267
		//
8285 kshitij.so 268
 
8532 vikram.rag 269
 
8285 kshitij.so 270
	}
271
 
272
}