Subversion Repositories SmartDukaan

Rev

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