Subversion Repositories SmartDukaan

Rev

Rev 8267 | Rev 8270 | 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
		 ***********************************************************************/
8269 manish.sha 44
		final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
8224 manish.sha 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
 
8269 manish.sha 53
		/************************************************************************
8224 manish.sha 54
		 * Uncomment to set the appropriate MWS endpoint.
8269 manish.sha 55
		 ************************************************************************/
8224 manish.sha 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
 
8269 manish.sha 75
		/************************************************************************
8224 manish.sha 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
		 *
8269 manish.sha 82
		 ***********************************************************************/
8224 manish.sha 83
 
8269 manish.sha 84
		/************************************************************************
8224 manish.sha 85
		 * Instantiate Http Client Implementation of Marketplace Web Service        
8269 manish.sha 86
		 ***********************************************************************/
8224 manish.sha 87
 
88
		MarketplaceWebService service = new MarketplaceWebServiceClient(
89
				accessKeyId, secretAccessKey, appName, appVersion, config);
90
 
8269 manish.sha 91
		/************************************************************************
8224 manish.sha 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
		 *
8269 manish.sha 100
		 ***********************************************************************/
8224 manish.sha 101
		// MarketplaceWebService service = new MarketplaceWebServiceMock();
102
 
8269 manish.sha 103
		/************************************************************************
8224 manish.sha 104
		 * Setup request parameters and uncomment invoke to try out 
105
		 * sample for Request Report 
8269 manish.sha 106
		 ***********************************************************************/
8224 manish.sha 107
 
8269 manish.sha 108
		/************************************************************************
8224 manish.sha 109
		 * Marketplace and Merchant IDs are required parameters for all 
110
		 * Marketplace Web Service calls.
8269 manish.sha 111
		 ***********************************************************************/
8224 manish.sha 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
 
8269 manish.sha 159
		List<String> fbaCustomersEmails  = new ArrayList<String>();
8224 manish.sha 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";
8269 manish.sha 175
 
176
 
177
 
8224 manish.sha 178
		String shipmentReportRequestId = RequestReportSample.invokeRequestReport(service, fullfilledShipmentReportRequest);
179
		String returnsReportRequestId = RequestReportSample.invokeRequestReport(service, returnsReportRequest);
180
		while(true){
181
			GetReportListRequest requestShipmentReportList = new GetReportListRequest();
182
			requestShipmentReportList.setMerchant( merchantId );
183
 
184
			GetReportListRequest requestReturnsReportList = new GetReportListRequest();
185
			requestReturnsReportList.setMerchant( merchantId );
186
 
187
			final IdList shipmentReportRequestIdList = new IdList(Arrays.asList(shipmentReportRequestId));        
188
			requestShipmentReportList.setReportRequestIdList(shipmentReportRequestIdList);
189
			///Request report status
190
			requestIdShipmentReportIdmap = GetReportListSample.invokeGetReportList(service, requestShipmentReportList);
191
 
192
			final IdList returnsReportRequestIdList = new IdList(Arrays.asList(returnsReportRequestId));        
193
			requestReturnsReportList.setReportRequestIdList(returnsReportRequestIdList);
194
 
195
			requestIdReturnsReportIdmap= GetReportListSample.invokeGetReportList(service, requestReturnsReportList);
196
 
197
			GetReportRequest requestShipmentReport = new GetReportRequest();
198
			requestShipmentReport.setMerchant( merchantId );
199
 
200
			GetReportRequest requestReturnsReport = new GetReportRequest();
201
			requestReturnsReport.setMerchant( merchantId );
202
 
203
 
204
			///Fetch report only if it is ready
205
			if(requestIdShipmentReportIdmap.get(shipmentReportRequestId)!=null && requestIdReturnsReportIdmap.get(returnsReportRequestId)!=null){
206
				requestShipmentReport.setReportId( requestIdShipmentReportIdmap.get(shipmentReportRequestId) );
207
				OutputStream shipmentReport=null;
208
				try {
209
					shipmentReport = new FileOutputStream( "/tmp/AmazonFullFilledShipmentReport.txt" );
210
				} catch (FileNotFoundException e) {
211
					log.error("Error Getting Shipment Report :- ", e);
212
				}
213
				requestShipmentReport.setReportOutputStream( shipmentReport );
214
				GetReportSample.invokeGetReport(service, requestShipmentReport);
215
 
8267 manish.sha 216
				System.out.println("Shipment Report ready please check\n");
8224 manish.sha 217
				CSVReader shipmentReportReader = null; 
218
				try {
219
					shipmentReportReader = new CSVReader(new FileReader("/tmp/AmazonFullFilledShipmentReport.txt"),'\t');
220
				} catch (FileNotFoundException e) {
221
					log.error("Error Reading Shipment Report :- ", e);
222
				}
223
 
224
				requestReturnsReport.setReportId( requestIdReturnsReportIdmap.get(returnsReportRequestId) );
225
				OutputStream returnsReport=null;
226
				try {
227
					returnsReport = new FileOutputStream( "/tmp/AmazonReturnsReport.txt" );
228
				} catch (FileNotFoundException e) {
229
					log.error("Error Getting Returns Report :- ", e);
230
				}
231
				requestReturnsReport.setReportOutputStream( returnsReport );
232
				GetReportSample.invokeGetReport(service, requestReturnsReport);
233
 
8267 manish.sha 234
				System.out.println("Return Report ready please check\n");
8224 manish.sha 235
				CSVReader returnsReportReader = null; 
236
				try {
237
					returnsReportReader = new CSVReader(new FileReader("/tmp/AmazonReturnsReport.txt"),'\t');
238
				} catch (FileNotFoundException e) {
239
					log.error("Error Reading Returns Report :- ", e);
240
				}
241
 
242
 
243
				String [] shipmentReportData;
244
				String [] returnsReportData;
245
 
246
				boolean matchFoundInReturns= false;
247
 
248
				try {
249
					while ((shipmentReportData = shipmentReportReader.readNext()) != null) {
250
						if(!shipmentReportData[0].equalsIgnoreCase("amazon-order-id")){
251
							returnReportWhile:while((returnsReportData = returnsReportReader.readNext()) != null){
252
								if(!returnsReportData[1].equalsIgnoreCase("amazon-order-id")){
253
									if(shipmentReportData[0].equalsIgnoreCase(returnsReportData[1])){
254
										matchFoundInReturns= true;
255
										break returnReportWhile;
256
									}
257
									else{
258
										XMLGregorianCalendar shipmentDate= df.newXMLGregorianCalendar(shipmentReportData[8].substring(0, shipmentReportData[8].indexOf("T")));
259
										XMLGregorianCalendar estimatedDate= df.newXMLGregorianCalendar(shipmentReportData[44].substring(0, shipmentReportData[44].indexOf("T")));
260
										if(shipmentDate.compare(estimatedDate)==-1)
261
											matchFoundInReturns= false;
262
									}
263
								}
264
							}
265
 
266
						if(!matchFoundInReturns){
267
							fbaCustomersEmails.add(shipmentReportData[10]);
268
						}
269
						}
270
 
271
					}
272
				} catch (IOException e) {
273
					log.error("Error Reading IO operations :- ", e);
274
				} 
275
 
276
				break;
277
			}
278
			else{ 
279
				log.info("Report not ready\n");
280
				try {
281
					Thread.sleep(5*60*1000);
282
				} catch (InterruptedException e) {
283
					log.error("Error During getting Response :- ", e);
284
				}
285
			}
8269 manish.sha 286
		}
8224 manish.sha 287
 
8260 manish.sha 288
		HelperClient helperServiceClient;
8224 manish.sha 289
		try {
8260 manish.sha 290
			helperServiceClient = new HelperClient();
291
			in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
8267 manish.sha 292
			System.out.print("Email Id List: \n");
8269 manish.sha 293
			for(int i=0; i<fbaCustomersEmails.size();i++){
294
				System.out.print(fbaCustomersEmails.get(i)+"\n");
295
				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);
296
			}
8224 manish.sha 297
		} catch (Exception e) {
298
			log.error("Error Getting Helper Client :- ", e);
299
		}
300
 
301
 
302
 
303
		// Note that depending on the type of report being downloaded, a report can reach 
304
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
305
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
306
		// the in-memory size limit and have to re-work your solution.
307
		//
308
 
309
 
310
	}
311
 
312
}