Subversion Repositories SmartDukaan

Rev

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