Subversion Repositories SmartDukaan

Rev

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