Subversion Repositories SmartDukaan

Rev

Rev 8472 | Rev 8544 | 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
 
3
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
8532 vikram.rag 4
import in.shop2020.thrift.clients.CatalogClient;
8285 kshitij.so 5
import in.shop2020.thrift.clients.InventoryClient;
6
 
7
import java.io.FileNotFoundException;
8
import java.io.FileOutputStream;
9
import java.io.FileReader;
10
import java.io.IOException;
11
import java.io.OutputStream;
12
import java.util.Arrays;
13
import java.util.GregorianCalendar;
14
import java.util.List;
15
import java.util.Map;
16
 
17
import javax.xml.datatype.DatatypeConfigurationException;
18
import javax.xml.datatype.DatatypeFactory;
19
import javax.xml.datatype.XMLGregorianCalendar;
20
 
21
import org.apache.thrift.TException;
22
import org.apache.thrift.transport.TTransportException;
23
 
24
import au.com.bytecode.opencsv.CSVReader;
25
 
26
import com.amazonaws.mws.MarketplaceWebService;
27
import com.amazonaws.mws.MarketplaceWebServiceClient;
28
import com.amazonaws.mws.MarketplaceWebServiceConfig;
8363 vikram.rag 29
import com.amazonaws.mws.MarketplaceWebServiceException;
8285 kshitij.so 30
import com.amazonaws.mws.model.GetReportListRequest;
31
import com.amazonaws.mws.model.GetReportRequest;
32
import com.amazonaws.mws.model.IdList;
33
import com.amazonaws.mws.model.RequestReportRequest;
34
 
35
public class FetchAmazonInventory {
36
	public static void main(String... args){
37
		/************************************************************************
8532 vikram.rag 38
		 * Access Key ID and Secret Access Key ID, obtained from:
39
		 * http://aws.amazon.com
40
		 ***********************************************************************/
8285 kshitij.so 41
		final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
42
		final String secretAccessKey = "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";
43
 
44
		final String appName = "Test";
45
		final String appVersion = "1.0";
46
		final String merchantId = "AF6E3O0VE0X4D";
47
 
8532 vikram.rag 48
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
8285 kshitij.so 49
 
8532 vikram.rag 50
		/************************************************************************
51
		 * Uncomment to set the appropriate MWS endpoint.
52
		 ************************************************************************/
53
		// US
54
		// config.setServiceURL("https://mws.amazonservices.com");
55
		// UK
56
		// config.setServiceURL("https://mws.amazonservices.co.uk");
57
		// Germany
58
		// config.setServiceURL("https://mws.amazonservices.de");
59
		// France
60
		// config.setServiceURL("https://mws.amazonservices.fr");
61
		// Italy
62
		// config.setServiceURL("https://mws.amazonservices.it");
63
		// Japan
64
		// config.setServiceURL("https://mws.amazonservices.jp");
65
		// China
66
		// config.setServiceURL("https://mws.amazonservices.com.cn");
67
		// Canada
68
		// config.setServiceURL("https://mws.amazonservices.ca");
69
		// India
70
		config.setServiceURL("https://mws.amazonservices.in");
8285 kshitij.so 71
 
8532 vikram.rag 72
		/************************************************************************
73
		 * You can also try advanced configuration options. Available options are:
74
		 *
75
		 *  - Signature Version
76
		 *  - Proxy Host and Proxy Port
77
		 *  - User Agent String to be sent to Marketplace Web Service
78
		 *
79
		 ***********************************************************************/
8285 kshitij.so 80
 
8532 vikram.rag 81
		/************************************************************************
82
		 * Instantiate Http Client Implementation of Marketplace Web Service        
83
		 ***********************************************************************/
8285 kshitij.so 84
 
8532 vikram.rag 85
		MarketplaceWebService service = new MarketplaceWebServiceClient(
86
				accessKeyId, secretAccessKey, appName, appVersion, config);
8285 kshitij.so 87
 
8532 vikram.rag 88
		/************************************************************************
89
		 * Uncomment to try out Mock Service that simulates Marketplace Web Service 
90
		 * responses without calling Marketplace Web Service  service.
91
		 *
92
		 * Responses are loaded from local XML files. You can tweak XML files to
93
		 * experiment with various outputs during development
94
		 *
95
		 * XML files available under com/amazonaws/mws/mock tree
96
		 *
97
		 ***********************************************************************/
98
		// MarketplaceWebService service = new MarketplaceWebServiceMock();
99
 
100
		/************************************************************************
101
		 * Setup request parameters and uncomment invoke to try out 
102
		 * sample for Request Report 
103
		 ***********************************************************************/
104
 
105
		/************************************************************************
106
		 * Marketplace and Merchant IDs are required parameters for all 
107
		 * Marketplace Web Service calls.
108
		 ***********************************************************************/
109
		// marketplaces from which data should be included in the report; look at the
110
		// API reference document on the MWS website to see which marketplaces are
111
		// included if you do not specify the list yourself
112
		final IdList marketplaces = new IdList(Arrays.asList(
8285 kshitij.so 113
		"A21TJRUUN4KGV"));        
8532 vikram.rag 114
		RequestReportRequest request = new RequestReportRequest()
115
		.withMerchant(merchantId)
116
		.withMarketplaceIdList(marketplaces)
117
		.withReportType("_GET_AFN_INVENTORY_DATA_")
118
		.withReportOptions("ShowSalesChannel=true");
8285 kshitij.so 119
 
8532 vikram.rag 120
		// demonstrates how to set the date range
8285 kshitij.so 121
		DatatypeFactory df = null;
122
		try {
123
			df = DatatypeFactory.newInstance();
124
		} catch (DatatypeConfigurationException e) {
125
			e.printStackTrace();
126
			throw new RuntimeException(e);
127
		}
128
		//XMLGregorianCalendar startDate = df.newXMLGregorianCalendar(new GregorianCalendar());
129
		//XMLGregorianCalendar endDate = df.newXMLGregorianCalendar(new GregorianCalendar());
130
		//request.setStartDate(startDate);
131
		//request.setEndDate(endDate);
8532 vikram.rag 132
		// @TODO: set additional request parameters here
8285 kshitij.so 133
		Map<String,String> requestIdreportIdmap;
134
		///Request report
135
		while(true){
8363 vikram.rag 136
			String requestId = null;
137
			try {
138
				requestId = RequestReportSample.invokeRequestReport(service, request);
139
			} catch (MarketplaceWebServiceException e1) {
140
				// TODO Auto-generated catch block
141
				e1.printStackTrace();
8472 vikram.rag 142
				continue;
8363 vikram.rag 143
			}
8285 kshitij.so 144
			while(true){
145
				GetReportListRequest requestreportlist = new GetReportListRequest();
146
				requestreportlist.setMerchant( merchantId );
147
				final IdList requestIdList = new IdList(Arrays.asList(requestId));        
148
				requestreportlist.setReportRequestIdList(requestIdList);
149
				///Request report status
150
				requestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);
151
 
152
				GetReportRequest requestreport = new GetReportRequest();
153
				requestreport.setMerchant( merchantId );
154
 
155
				///Fetch report only if it is ready
156
				if(requestIdreportIdmap.get(requestId)!=null){
157
					requestreport.setReportId( requestIdreportIdmap.get(requestId) );
158
					OutputStream report=null;
159
					try {
8363 vikram.rag 160
						report = new FileOutputStream( "/home/amazoninventoryreport.csv" );
8285 kshitij.so 161
					} catch (FileNotFoundException e) {
162
						// TODO Auto-generated catch block
163
						e.printStackTrace();
164
					}
165
					requestreport.setReportOutputStream( report );
166
					GetReportSample.invokeGetReport(service, requestreport);
8363 vikram.rag 167
					//System.out.println("Report ready please check");
8285 kshitij.so 168
					CSVReader reader = null; 
169
					try {
8363 vikram.rag 170
						//reader = new CSVReader(new FileReader("/home/vikram/Desktop/amazoninventoryreport.txt"),'\t');
171
						reader = new CSVReader(new FileReader("/home/amazoninventoryreport.csv"),'\t');
8285 kshitij.so 172
					} catch (FileNotFoundException e) {
173
						// TODO Auto-generated catch block
174
						e.printStackTrace();
175
					}
176
					String [] nextLine;
8532 vikram.rag 177
					CatalogClient catalogServiceClient = null;
8285 kshitij.so 178
					InventoryClient inventoryServiceClient = null;
179
					try {
180
						inventoryServiceClient = new InventoryClient();
8532 vikram.rag 181
						//catalogServiceClient = new CatalogClient();
182
						catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
8285 kshitij.so 183
					} catch (Exception e) {
184
						// TODO Auto-generated catch block
185
						e.printStackTrace();
8532 vikram.rag 186
					}	
187
					in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
188
					in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
8285 kshitij.so 189
					try {
190
						while ((nextLine = reader.readNext()) != null) {
191
							// nextLine[] is an array of values from the line
8363 vikram.rag 192
							//System.out.println(nextLine[0] +" "+ nextLine[1]+" " + nextLine[2]+" " + nextLine[3] +" "+ nextLine[4] +" " + nextLine[5]);
8285 kshitij.so 193
							if(nextLine[0].startsWith("FBA") && nextLine[4].equalsIgnoreCase("SELLABLE") ){
194
								//System.out.println("Item ID" + nextLine[0].replaceAll("FBA","") + "---"+"Inventory" + nextLine[5]);
8532 vikram.rag 195
								if(catalogClient.getAmazonItemDetails(Long.parseLong(nextLine[0].replaceAll("FBA",""))).getItemid()!=0){
196
									AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
197
									amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[5]));
198
									amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));
199
									inventoryClient.addOrUpdateAmazonFbaInventory(amazonfbainventorysnapshot);
200
								}
8285 kshitij.so 201
							}
202
						}
203
					} catch (IOException e) {
204
						// TODO Auto-generated catch block
205
						e.printStackTrace();
206
					} catch (TException e) {
207
						// TODO Auto-generated catch block
208
						e.printStackTrace();
209
					}
210
					break;
211
				}
212
				else{ 
8363 vikram.rag 213
					//System.out.println("Report not ready");
8285 kshitij.so 214
					try {
215
						Thread.sleep(5*60*1000);
216
					} catch (InterruptedException e) {
217
						// TODO Auto-generated catch block
218
						e.printStackTrace();
219
					}
220
				}
221
			}
222
			try {
223
				Thread.sleep(30*60*1000);
224
			} catch (InterruptedException e) {
225
				// TODO Auto-generated catch block
226
				e.printStackTrace();
227
			}
228
		}
229
 
8532 vikram.rag 230
		// Note that depending on the type of report being downloaded, a report can reach 
231
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
232
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
233
		// the in-memory size limit and have to re-work your solution.
234
		//
8285 kshitij.so 235
 
8532 vikram.rag 236
 
8285 kshitij.so 237
	}
238
 
239
}