Subversion Repositories SmartDukaan

Rev

Rev 8528 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8224 manish.sha 1
package com.amazonaws.mws.samples;
2
 
3
import in.shop2020.thrift.clients.HelperClient;
4
 
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
7
import java.io.FileReader;
8
import java.io.IOException;
9
import java.io.OutputStream;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.Calendar;
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
 
22
import org.apache.log4j.Logger;
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;
8365 vikram.rag 29
import com.amazonaws.mws.MarketplaceWebServiceException;
8224 manish.sha 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
import com.amazonaws.mws.samples.GetReportSample;
35
 
36
public class FeedbackRequestEmailSender {
8727 manish.sha 37
	protected static Logger log = Logger.getLogger(FeedbackRequestEmailSender.class);
8224 manish.sha 38
 
8727 manish.sha 39
	public RequestReportRequest getReportRequest(String merchantId, IdList marketplaces, String reporttype, String reportOptions, XMLGregorianCalendar startDate, XMLGregorianCalendar endDate){
40
		System.out.println("In method of getting Report Request");
41
		RequestReportRequest reqReportRequest = new RequestReportRequest()
42
		.withMerchant(merchantId)
43
		.withMarketplaceIdList(marketplaces)
44
		.withReportType(reporttype)
45
		.withReportOptions(reportOptions);
46
		reqReportRequest.setStartDate(startDate);
47
		reqReportRequest.setEndDate(endDate);
48
		System.out.println("Got Report Request");
49
		return reqReportRequest;
50
	}
51
 
52
	public String getReportRequestId(MarketplaceWebService webservice, RequestReportRequest reqReportRequest){
53
		System.out.println("In method of getting Report Request Id");
54
		String requestReportRequestId = null;
55
		while(true){
56
			try {
57
				requestReportRequestId = RequestReportSample.invokeRequestReport(webservice, reqReportRequest);
58
				break;
59
			} catch (MarketplaceWebServiceException e1) {
60
				// TODO Auto-generated catch block
61
				e1.printStackTrace();
62
			}
63
		}
64
		System.out.println("Got Report Request Id");
65
		System.out.println(requestReportRequestId);
66
		System.out.println("requestReportRequestId success");
67
		return requestReportRequestId;
68
	}
69
 
70
 
71
	 public Map<String,String> getrequestReportIdmap(String merchantId, MarketplaceWebService webservice, String requestReportRequestId){
72
		 System.out.println("In method of getting Report Request Id Map");
73
		 Map<String,String> requestReportIdmap;
74
	whileLoop:while(true){
75
		     GetReportListRequest requestShipmentReportList = new GetReportListRequest();
76
			 requestShipmentReportList.setMerchant( merchantId );
77
 
78
			 final IdList shipmentReportRequestIdList = new IdList(Arrays.asList(requestReportRequestId));        
79
			 requestShipmentReportList.setReportRequestIdList(shipmentReportRequestIdList);
80
 
81
			 requestReportIdmap = GetReportListSample.invokeGetReportList(webservice, requestShipmentReportList);
82
 
83
			 if(requestReportIdmap.get(requestReportRequestId)!=null){
84
				 break whileLoop;
85
			 }
86
			 else{ 
87
				log.info("Report not ready\n");
88
				try {
89
					Thread.sleep(5*60*1000);
90
				} catch (InterruptedException e) {
91
					System.out.println("Error During getting Map Response");
92
					log.error("Error During getting Response :- ", e);
93
				}
94
			 }
95
		 }
96
		 System.out.println("Got Report Request Id Map");
97
		 return requestReportIdmap;
98
	 }
99
 
100
	public void writeReportLocally(MarketplaceWebService webservice, String merchantId, String reportId, String filePath, String reportType){
101
		System.out.println("In method of writting Report");
102
		GetReportRequest requestReport = new GetReportRequest();
103
		requestReport.setMerchant( merchantId );		
104
 
105
		requestReport.setReportId(reportId);
106
		OutputStream report=null;
107
		try {
108
			report = new FileOutputStream(filePath);
109
		} catch (FileNotFoundException e) {
110
			log.error("Error Getting Shipment Report :- ", e);
111
		}
112
		requestReport.setReportOutputStream( report );
113
		GetReportSample.invokeGetReport(webservice, requestReport);
114
 
115
		System.out.println(reportType +" Report ready please check");
116
	}
117
 
118
	public CSVReader getReportReader(String filePath, String reportType){
119
		System.out.println("In method of getting report reader");
120
		CSVReader reportReader = null; 
121
		try {
122
			reportReader = new CSVReader(new FileReader(filePath),'\t');
123
		} catch (FileNotFoundException e) {
124
			System.out.println("Error Reading "+reportType+" Report ");
125
			log.error("Error Reading Shipment Report :- ", e);
126
		}
127
		System.out.println("Got Report reader");
128
		return reportReader;
129
	}
130
 
131
	public List<String> getFBACustomerEmails(CSVReader shpmntReportReader, CSVReader rtrnsReportReader, DatatypeFactory df){
132
 
133
		List<String> fbaCustomersEmails  = new ArrayList<String>();
134
 
135
		String [] shipmentReportData;
136
		String [] returnsReportData;
137
 
138
		boolean matchFoundInReturns= false;
139
		System.out.println("In method of getting email ids");
140
		try {
141
			while ((shipmentReportData = shpmntReportReader.readNext()) != null) {
142
				if(!shipmentReportData[0].equalsIgnoreCase("amazon-order-id")){
143
					returnReportWhile:while((returnsReportData = rtrnsReportReader.readNext()) != null){
144
						if(!returnsReportData[1].equalsIgnoreCase("amazon-order-id")){
145
							if(shipmentReportData[0].equalsIgnoreCase(returnsReportData[1])){
146
								matchFoundInReturns= true;
147
								break returnReportWhile;
148
							}
149
							else{
150
								XMLGregorianCalendar shipmentDate= df.newXMLGregorianCalendar(shipmentReportData[8].substring(0, shipmentReportData[8].indexOf("T")));
151
								XMLGregorianCalendar estimatedDate= df.newXMLGregorianCalendar(shipmentReportData[44].substring(0, shipmentReportData[44].indexOf("T")));
152
								if(shipmentDate.compare(estimatedDate)==-1)
153
									matchFoundInReturns= false;
154
							}
155
						}
156
					}
157
 
158
				if(!matchFoundInReturns){
159
					fbaCustomersEmails.add(shipmentReportData[10]);
160
				}
161
				}
162
 
163
			}
164
		} catch (IOException e) {
165
			log.error("Error Reading IO operations :- ", e);
166
		}
167
		System.out.println("Got email ids");
168
		return fbaCustomersEmails;
169
	}
170
 
8224 manish.sha 171
	public static void main(String... args){
8727 manish.sha 172
 
8224 manish.sha 173
		/************************************************************************
174
		 * Access Key ID and Secret Access Key ID, obtained from:
175
		 * http://aws.amazon.com
176
		 ***********************************************************************/
8727 manish.sha 177
 
8269 manish.sha 178
		final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
8224 manish.sha 179
		final String secretAccessKey = "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";
180
 
181
		final String appName = "Test";
182
		final String appVersion = "1.0";
183
		final String merchantId = "AF6E3O0VE0X4D";
8269 manish.sha 184
		/************************************************************************
8224 manish.sha 185
		 * Uncomment to set the appropriate MWS endpoint.
8269 manish.sha 186
		 ************************************************************************/
8224 manish.sha 187
		// US
188
		// config.setServiceURL("https://mws.amazonservices.com");
189
		// UK
190
		// config.setServiceURL("https://mws.amazonservices.co.uk");
191
		// Germany
192
		// config.setServiceURL("https://mws.amazonservices.de");
193
		// France
194
		// config.setServiceURL("https://mws.amazonservices.fr");
195
		// Italy
196
		// config.setServiceURL("https://mws.amazonservices.it");
197
		// Japan
198
		// config.setServiceURL("https://mws.amazonservices.jp");
199
		// China
200
		// config.setServiceURL("https://mws.amazonservices.com.cn");
201
		// Canada
202
		// config.setServiceURL("https://mws.amazonservices.ca");
203
		// India
8727 manish.sha 204
 
205
 
8269 manish.sha 206
		/************************************************************************
8224 manish.sha 207
		 * You can also try advanced configuration options. Available options are:
208
		 *
209
		 *  - Signature Version
210
		 *  - Proxy Host and Proxy Port
211
		 *  - User Agent String to be sent to Marketplace Web Service
212
		 *
8269 manish.sha 213
		 ***********************************************************************/
8224 manish.sha 214
 
8269 manish.sha 215
		/************************************************************************
8224 manish.sha 216
		 * Instantiate Http Client Implementation of Marketplace Web Service        
8269 manish.sha 217
		 ***********************************************************************/
8727 manish.sha 218
 
8224 manish.sha 219
 
8727 manish.sha 220
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
221
		config.setServiceURL("https://mws.amazonservices.in");
8224 manish.sha 222
		MarketplaceWebService service = new MarketplaceWebServiceClient(
223
				accessKeyId, secretAccessKey, appName, appVersion, config);
224
 
8269 manish.sha 225
		/************************************************************************
8224 manish.sha 226
		 * Uncomment to try out Mock Service that simulates Marketplace Web Service 
227
		 * responses without calling Marketplace Web Service  service.
228
		 *
229
		 * Responses are loaded from local XML files. You can tweak XML files to
230
		 * experiment with various outputs during development
231
		 *
232
		 * XML files available under com/amazonaws/mws/mock tree
233
		 *
8269 manish.sha 234
		 ***********************************************************************/
8224 manish.sha 235
		// MarketplaceWebService service = new MarketplaceWebServiceMock();
236
 
8269 manish.sha 237
		/************************************************************************
8224 manish.sha 238
		 * Setup request parameters and uncomment invoke to try out 
239
		 * sample for Request Report 
8269 manish.sha 240
		 ***********************************************************************/
8224 manish.sha 241
 
8269 manish.sha 242
		/************************************************************************
8224 manish.sha 243
		 * Marketplace and Merchant IDs are required parameters for all 
244
		 * Marketplace Web Service calls.
8269 manish.sha 245
		 ***********************************************************************/
8224 manish.sha 246
		// marketplaces from which data should be included in the report; look at the
247
		// API reference document on the MWS website to see which marketplaces are
248
		// included if you do not specify the list yourself
249
		final IdList marketplaces = new IdList(Arrays.asList(
250
		"A21TJRUUN4KGV"));        
8727 manish.sha 251
		FeedbackRequestEmailSender feedbackRequestEmailSender = new FeedbackRequestEmailSender();
252
 
8224 manish.sha 253
		// demonstrates how to set the date range
254
		DatatypeFactory df = null;
255
		try {
256
			df = DatatypeFactory.newInstance();
257
		} catch (DatatypeConfigurationException e) {
258
			e.printStackTrace();
259
			throw new RuntimeException(e);
260
		}
8727 manish.sha 261
		Calendar current_Date= new GregorianCalendar();
8224 manish.sha 262
 
8727 manish.sha 263
		XMLGregorianCalendar endDateReturnReport = df.newXMLGregorianCalendar(new GregorianCalendar(current_Date.get(Calendar.YEAR),current_Date.get(Calendar.MONTH),current_Date.get(Calendar.DAY_OF_MONTH)));
8505 vikram.rag 264
 
8727 manish.sha 265
		current_Date.add(Calendar.DAY_OF_MONTH, -21);
8224 manish.sha 266
 
8727 manish.sha 267
		XMLGregorianCalendar startDateBoth = df.newXMLGregorianCalendar(new GregorianCalendar(current_Date.get(Calendar.YEAR),current_Date.get(Calendar.MONTH),current_Date.get(Calendar.DAY_OF_MONTH)));
268
		current_Date.add(Calendar.DAY_OF_MONTH, 1);
8224 manish.sha 269
 
8727 manish.sha 270
		XMLGregorianCalendar endDateFullFilledReport = df.newXMLGregorianCalendar(new GregorianCalendar(current_Date.get(Calendar.YEAR),current_Date.get(Calendar.MONTH),current_Date.get(Calendar.DAY_OF_MONTH)));
8505 vikram.rag 271
 
8727 manish.sha 272
		RequestReportRequest fullfilledShipmentReportRequest = feedbackRequestEmailSender.getReportRequest(merchantId, marketplaces, "_GET_AMAZON_FULFILLED_SHIPMENTS_DATA_", "ShowSalesChannel=true", startDateBoth, endDateFullFilledReport);
273
 
274
		RequestReportRequest returnsReportRequest = feedbackRequestEmailSender.getReportRequest(merchantId, marketplaces, "_GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA_", "ShowSalesChannel=true", startDateBoth, endDateReturnReport);
275
 
276
		String shipmentReportRequestId = feedbackRequestEmailSender.getReportRequestId(service, fullfilledShipmentReportRequest);
277
 
278
		String returnsReportRequestId = feedbackRequestEmailSender.getReportRequestId(service, returnsReportRequest);
279
 
280
		Map<String,String> requestIdShipmentReportIdmap = feedbackRequestEmailSender.getrequestReportIdmap(merchantId, service, shipmentReportRequestId);
8224 manish.sha 281
 
282
 
8727 manish.sha 283
		Map<String,String> requestIdReturnsReportIdmap = feedbackRequestEmailSender.getrequestReportIdmap(merchantId, service, returnsReportRequestId);
8224 manish.sha 284
 
8727 manish.sha 285
 
8224 manish.sha 286
		///Request report
287
 
288
		String emailBody= "Dear Customer, <br><br>" +
289
		"Thank you very much for shopping with Saholic at Amazon.in .<br><br>"+
290
		"We hope that you are happy with your purchase and if you are, please spare some time to leave positive feedback for us.<br><br>" +
291
		"Kindly follow below mentioned steps:-<br><br>" +
292
		"1. Login into your Amazon account<br>" +
293
		"2. Go to Your Order Section<br>" +
294
		"3. Click on Seller Feedback<br><br>" +
295
		"Your comments and feedback help us improve our products and services for other customers.<br><br>" +
296
		"Please do not leave any negative feedback. If you have any problems or concerns about your recent purchase, please get in touch with our customer service as soon as possible and we will do everything we can to help.<br><br>" +
297
		"Yours Sincerely,<br>" +
298
		"Saholic Team<br>";
299
 
300
		String emailIdFrom= "help@saholic.com";
8727 manish.sha 301
			//Fetch report only if it is ready
302
		feedbackRequestEmailSender.writeReportLocally(service, merchantId, requestIdShipmentReportIdmap.get(shipmentReportRequestId), "/tmp/AmazonFullFilledShipmentReport.txt", "Fullfilled Shipment");
303
		feedbackRequestEmailSender.writeReportLocally(service, merchantId, requestIdReturnsReportIdmap.get(returnsReportRequestId), "/tmp/AmazonReturnsReport.txt", "Returns");	
304
 
305
		CSVReader shipmentReportReader = feedbackRequestEmailSender.getReportReader("/tmp/AmazonFullFilledShipmentReport.txt", "Fullfilled Shipment") ;
306
		CSVReader returnsReportReader = feedbackRequestEmailSender.getReportReader("/tmp/AmazonReturnsReport.txt", "Returns") ; 
307
 
308
		List<String> fbaCustomersEmails  = feedbackRequestEmailSender.getFBACustomerEmails(shipmentReportReader, returnsReportReader, df);
8269 manish.sha 309
 
8528 manish.sha 310
		HelperClient helperServiceClient;
8224 manish.sha 311
		try {
8528 manish.sha 312
			helperServiceClient = new HelperClient("helper_service_server_host_prod", "helper_service_server_port_prod");
313
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
8267 manish.sha 314
			System.out.print("Email Id List: \n");
8528 manish.sha 315
			for(int i=0; i<fbaCustomersEmails.size();i++){
8269 manish.sha 316
				System.out.print(fbaCustomersEmails.get(i)+"\n");
8528 manish.sha 317
				client.saveUserEmailForSending(Arrays.asList(new String[] {fbaCustomersEmails.get(i)}), emailIdFrom, "Feedback Request", emailBody, "AmazonMFN", "AmazonFeedback", null, Arrays.asList(new String[] {"amit.sirohi@shop2020.in"}), 1);
8269 manish.sha 318
			}
8224 manish.sha 319
		} catch (Exception e) {
8528 manish.sha 320
			log.error("Error Getting Helper Client :- ", e);
8224 manish.sha 321
		}
322
 
323
 
324
 
325
		// Note that depending on the type of report being downloaded, a report can reach 
326
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
327
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
328
		// the in-memory size limit and have to re-work your solution.
329
		//
8727 manish.sha 330
		System.exit(0);
8224 manish.sha 331
 
332
	}
333
 
334
}