Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9490 vikram.rag 1
package com.amazonaws.mws.samples;
2
 
3
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
4
import in.shop2020.thrift.clients.CatalogClient;
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.ArrayList;
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;
30
import com.amazonaws.mws.MarketplaceWebServiceException;
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 FetchAmazonInventoryfromFile {
37
	public static void main(String... args){
38
		/************************************************************************
39
		 * Access Key ID and Secret Access Key ID, obtained from:
40
		 * http://aws.amazon.com
41
		 ***********************************************************************/
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
 
49
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
50
 
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");
72
 
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
		 ***********************************************************************/
81
 
82
		/************************************************************************
83
		 * Instantiate Http Client Implementation of Marketplace Web Service        
84
		 ***********************************************************************/
85
 
86
		MarketplaceWebService service = new MarketplaceWebServiceClient(
87
				accessKeyId, secretAccessKey, appName, appVersion, config);
88
 
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(
114
		"A21TJRUUN4KGV"));        
115
		RequestReportRequest request = new RequestReportRequest()
116
		.withMerchant(merchantId)
117
		.withMarketplaceIdList(marketplaces)
118
		.withReportType("_GET_AFN_INVENTORY_DATA_")
119
		.withReportOptions("ShowSalesChannel=true");
120
 
121
		// demonstrates how to set the date range
122
		DatatypeFactory df = null;
123
		try {
124
			df = DatatypeFactory.newInstance();
125
		} catch (DatatypeConfigurationException e) {
126
			e.printStackTrace();
127
			throw new RuntimeException(e);
128
		}
129
					//System.out.println("Report ready please check");
130
					CSVReader reader = null; 
131
					try {
132
						//reader = new CSVReader(new FileReader("/home/vikram/Desktop/amazoninventoryreport.txt"),'\t');
133
						reader = new CSVReader(new FileReader("/home/amazoninventoryreport.csv"),'\t');
134
					} catch (FileNotFoundException e) {
135
						// TODO Auto-generated catch block
136
						e.printStackTrace();
137
					}
138
					String [] nextLine;
139
					//CatalogClient catalogServiceClient = null;
140
					InventoryClient inventoryServiceClient = null;
141
					try {
142
						inventoryServiceClient = new InventoryClient();
143
						//catalogServiceClient = new CatalogClient();
144
						//catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");
145
					} catch (Exception e) {
146
						// TODO Auto-generated catch block
147
						e.printStackTrace();
148
					}	
149
					//in.shop2020.model.v1.catalog.CatalogService.Client catalogClient   = catalogServiceClient.getClient();
150
					in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
151
					List<AmazonFbaInventorySnapshot> allamazoninventory = new ArrayList<AmazonFbaInventorySnapshot>();
152
					try {
153
						while ((nextLine = reader.readNext()) != null) {
154
							// nextLine[] is an array of values from the line
155
							//System.out.println(nextLine[0] +" "+ nextLine[1]+" " + nextLine[2]+" " + nextLine[3] +" "+ nextLine[4] +" " + nextLine[5]);
156
							if(nextLine[0].startsWith("FBA") && nextLine[4].equalsIgnoreCase("SELLABLE") ){
157
								//System.out.println("Item ID" + nextLine[0].replaceAll("FBA","") + "---"+"Inventory" + nextLine[5]);
158
						//if(catalogClient.getAmazonItemDetails(Long.parseLong(nextLine[0].replaceAll("FBA",""))).getItemid()!=0){
159
									AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
160
									amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[5]));
161
									amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));
162
									allamazoninventory.add(amazonfbainventorysnapshot);
163
									System.out.println("Adding item in list " + Long.parseLong(nextLine[0].replaceAll("FBA","")));
164
 
165
						//		}
166
							}
167
						}
168
						inventoryClient.addOrUpdateAllAmazonFbaInventory(allamazoninventory);
169
					} catch (IOException e) {
170
						// TODO Auto-generated catch block
171
						e.printStackTrace();
172
					} catch (TException e) {
173
						// TODO Auto-generated catch block
174
						e.printStackTrace();
175
					}
176
	}
177
}