Subversion Repositories SmartDukaan

Rev

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