Rev 12897 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.amazonaws.mws.samples;import in.shop2020.model.v1.inventory.AmazonFCWarehouseLocation;import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.InventoryClient;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.GregorianCalendar;import java.util.List;import java.util.Map;import javax.xml.datatype.DatatypeConfigurationException;import javax.xml.datatype.DatatypeFactory;import javax.xml.datatype.XMLGregorianCalendar;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import au.com.bytecode.opencsv.CSVReader;import com.amazonaws.mws.MarketplaceWebService;import com.amazonaws.mws.MarketplaceWebServiceClient;import com.amazonaws.mws.MarketplaceWebServiceConfig;import com.amazonaws.mws.MarketplaceWebServiceException;import com.amazonaws.mws.model.GetReportListRequest;import com.amazonaws.mws.model.GetReportRequest;import com.amazonaws.mws.model.IdList;import com.amazonaws.mws.model.RequestReportRequest;public class FetchAmazonInventory {public static void main(String... args){/************************************************************************* Access Key ID and Secret Access Key ID, obtained from:* http://aws.amazon.com***********************************************************************/final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";final String secretAccessKey = "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";final String appName = "Test";final String appVersion = "1.0";final String merchantId = "AF6E3O0VE0X4D";MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();/************************************************************************* Uncomment to set the appropriate MWS endpoint.************************************************************************/// US// config.setServiceURL("https://mws.amazonservices.com");// UK// config.setServiceURL("https://mws.amazonservices.co.uk");// Germany// config.setServiceURL("https://mws.amazonservices.de");// France// config.setServiceURL("https://mws.amazonservices.fr");// Italy// config.setServiceURL("https://mws.amazonservices.it");// Japan// config.setServiceURL("https://mws.amazonservices.jp");// China// config.setServiceURL("https://mws.amazonservices.com.cn");// Canada// config.setServiceURL("https://mws.amazonservices.ca");// Indiaconfig.setServiceURL("https://mws.amazonservices.in");/************************************************************************* You can also try advanced configuration options. Available options are:** - Signature Version* - Proxy Host and Proxy Port* - User Agent String to be sent to Marketplace Web Service************************************************************************//************************************************************************* Instantiate Http Client Implementation of Marketplace Web Service***********************************************************************/MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);/************************************************************************* Uncomment to try out Mock Service that simulates Marketplace Web Service* responses without calling Marketplace Web Service service.** Responses are loaded from local XML files. You can tweak XML files to* experiment with various outputs during development** XML files available under com/amazonaws/mws/mock tree************************************************************************/// MarketplaceWebService service = new MarketplaceWebServiceMock();/************************************************************************* Setup request parameters and uncomment invoke to try out* sample for Request Report***********************************************************************//************************************************************************* Marketplace and Merchant IDs are required parameters for all* Marketplace Web Service calls.***********************************************************************/// marketplaces from which data should be included in the report; look at the// API reference document on the MWS website to see which marketplaces are// included if you do not specify the list yourselffinal IdList marketplaces = new IdList(Arrays.asList("A21TJRUUN4KGV"));RequestReportRequest request = new RequestReportRequest().withMerchant(merchantId).withMarketplaceIdList(marketplaces).withReportType("_GET_FBA_MYI_ALL_INVENTORY_DATA_").withReportOptions("ShowSalesChannel=true");// demonstrates how to set the date rangeDatatypeFactory df = null;try {df = DatatypeFactory.newInstance();} catch (DatatypeConfigurationException e) {e.printStackTrace();throw new RuntimeException(e);}//XMLGregorianCalendar startDate = df.newXMLGregorianCalendar(new GregorianCalendar());//XMLGregorianCalendar endDate = df.newXMLGregorianCalendar(new GregorianCalendar());//request.setStartDate(startDate);//request.setEndDate(endDate);// @TODO: set additional request parameters hereMap<String,String> requestIdreportIdmap;///Request reportwhile(true){String requestId = null;try {requestId = RequestReportSample.invokeRequestReport(service, request);} catch (MarketplaceWebServiceException e1) {// TODO Auto-generated catch blocke1.printStackTrace();continue;}while(true){GetReportListRequest requestreportlist = new GetReportListRequest();requestreportlist.setMerchant( merchantId );final IdList requestIdList = new IdList(Arrays.asList(requestId));requestreportlist.setReportRequestIdList(requestIdList);///Request report statusrequestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);GetReportRequest requestreport = new GetReportRequest();requestreport.setMerchant( merchantId );///Fetch report only if it is readyif(requestIdreportIdmap.get(requestId)!=null){requestreport.setReportId( requestIdreportIdmap.get(requestId) );OutputStream report=null;try {report = new FileOutputStream( "/home/amazoninventoryreport.csv" );} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}requestreport.setReportOutputStream( report );GetReportSample.invokeGetReport(service, requestreport);//System.out.println("Report ready please check");BufferedReader reader = null;try {reader = new BufferedReader(new FileReader("/home/amazoninventoryreport.csv"));} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String[] nextLine = null;String Line;List<AmazonFbaInventorySnapshot> allamazoninventory = new ArrayList<AmazonFbaInventorySnapshot>();try {while ((Line = reader.readLine()) != null) {nextLine = Line.split("\t");System.out.println("SKU " + nextLine[0] + " "+ nextLine[10] +" "+nextLine[11]+" "+nextLine[12]+" "+nextLine[16]);if(nextLine[0].startsWith("FBA")){AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;if(!nextLine[10].isEmpty())amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));else{amazonfbainventorysnapshot.setAvailability(0);}try{amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));}catch(Exception e){continue;}amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Mumbai);if(nextLine[12].length()>0){amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));}else{amazonfbainventorysnapshot.setReserved(0);}if(nextLine[11].length()>0){amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));}else{amazonfbainventorysnapshot.setUnfulfillable(0);}if(nextLine[16].length()>0){amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));}else{amazonfbainventorysnapshot.setInbound(0);}allamazoninventory.add(amazonfbainventorysnapshot);}else if(nextLine[0].startsWith("FBB")){AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;try{amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBB","")));amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Bangalore);amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));}catch(Exception ex){continue;}allamazoninventory.add(amazonfbainventorysnapshot);}else if(nextLine[0].startsWith("FBG")){AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;if(!nextLine[10].isEmpty())amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[10]));else{amazonfbainventorysnapshot.setAvailability(0);}try{amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBG","")));}catch(Exception e){continue;}amazonfbainventorysnapshot.setLocation(AmazonFCWarehouseLocation.Gurgaon);if(nextLine[12].length()>0){amazonfbainventorysnapshot.setReserved(Long.parseLong(nextLine[12]));}else{amazonfbainventorysnapshot.setReserved(0);}if(nextLine[11].length()>0){amazonfbainventorysnapshot.setUnfulfillable(Long.parseLong(nextLine[11]));}else{amazonfbainventorysnapshot.setUnfulfillable(0);}if(nextLine[16].length()>0){amazonfbainventorysnapshot.setInbound(Long.parseLong(nextLine[16]));}else{amazonfbainventorysnapshot.setInbound(0);}allamazoninventory.add(amazonfbainventorysnapshot);}else{continue;}}try{new InventoryClient().getClient().addOrUpdateAllAmazonFbaInventory(allamazoninventory);}catch(Exception ex){try {new InventoryClient().getClient().addOrUpdateAllAmazonFbaInventory(allamazoninventory);} catch (Exception e) {e.printStackTrace();}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}else{//System.out.println("Report not ready");try {Thread.sleep(5*60*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}try {Thread.sleep(30*60*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Note that depending on the type of report being downloaded, a report can reach// sizes greater than 1GB. For this reason we recommend that you _always_ program to// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach// the in-memory size limit and have to re-work your solution.//}}