Subversion Repositories SmartDukaan

Rev

Rev 8525 | 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
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
 
8526 manish.sha 39
	//protected static Logger log = Logger.getLogger(FeedbackRequestEmailSender.class);
8224 manish.sha 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)));
8505 vikram.rag 141
 
8224 manish.sha 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)));
8505 vikram.rag 148
 
8224 manish.sha 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
 
8526 manish.sha 177
		String shipmentReportRequestId = null;	
178
		int i=1;
8505 vikram.rag 179
		while(true){
180
 
181
			try {
182
				shipmentReportRequestId = RequestReportSample.invokeRequestReport(service, fullfilledShipmentReportRequest);
183
				break;
184
			} catch (MarketplaceWebServiceException e1) {
185
				// TODO Auto-generated catch block
186
				e1.printStackTrace();
187
			}
8526 manish.sha 188
			i =i +1;
8365 vikram.rag 189
		}
8526 manish.sha 190
		System.out.println("i's Value.... "+i);
8365 vikram.rag 191
		String returnsReportRequestId = null;
8526 manish.sha 192
		int j=1;
8505 vikram.rag 193
		while(true){
194
			try {
195
				returnsReportRequestId = RequestReportSample.invokeRequestReport(service, returnsReportRequest);
196
				break;
197
			} catch (MarketplaceWebServiceException e1) {
198
				// TODO Auto-generated catch block
199
				e1.printStackTrace();
200
			}
8526 manish.sha 201
			j =j +1;
8365 vikram.rag 202
		}
8526 manish.sha 203
		System.out.println("j's Value.... "+j);
204
		int m=0;
8224 manish.sha 205
		while(true){
206
			GetReportListRequest requestShipmentReportList = new GetReportListRequest();
207
			requestShipmentReportList.setMerchant( merchantId );
208
 
209
			GetReportListRequest requestReturnsReportList = new GetReportListRequest();
210
			requestReturnsReportList.setMerchant( merchantId );
211
 
212
			final IdList shipmentReportRequestIdList = new IdList(Arrays.asList(shipmentReportRequestId));        
213
			requestShipmentReportList.setReportRequestIdList(shipmentReportRequestIdList);
214
			///Request report status
215
			requestIdShipmentReportIdmap = GetReportListSample.invokeGetReportList(service, requestShipmentReportList);
216
 
217
			final IdList returnsReportRequestIdList = new IdList(Arrays.asList(returnsReportRequestId));        
218
			requestReturnsReportList.setReportRequestIdList(returnsReportRequestIdList);
219
 
220
			requestIdReturnsReportIdmap= GetReportListSample.invokeGetReportList(service, requestReturnsReportList);
221
 
8505 vikram.rag 222
			///Fetch report only if it is ready
223
			if(requestIdShipmentReportIdmap.get(shipmentReportRequestId)!=null && requestIdReturnsReportIdmap.get(returnsReportRequestId)!=null){
224
				GetReportRequest requestShipmentReport = new GetReportRequest();
225
				requestShipmentReport.setMerchant( merchantId );
8224 manish.sha 226
 
8505 vikram.rag 227
				GetReportRequest requestReturnsReport = new GetReportRequest();
228
				requestReturnsReport.setMerchant( merchantId );
8224 manish.sha 229
 
230
				requestShipmentReport.setReportId( requestIdShipmentReportIdmap.get(shipmentReportRequestId) );
231
				OutputStream shipmentReport=null;
232
				try {
233
					shipmentReport = new FileOutputStream( "/tmp/AmazonFullFilledShipmentReport.txt" );
234
				} catch (FileNotFoundException e) {
8526 manish.sha 235
					//log.error("Error Getting Shipment Report :- ", e);
8224 manish.sha 236
				}
237
				requestShipmentReport.setReportOutputStream( shipmentReport );
238
				GetReportSample.invokeGetReport(service, requestShipmentReport);
239
 
8267 manish.sha 240
				System.out.println("Shipment Report ready please check\n");
8224 manish.sha 241
				CSVReader shipmentReportReader = null; 
242
				try {
243
					shipmentReportReader = new CSVReader(new FileReader("/tmp/AmazonFullFilledShipmentReport.txt"),'\t');
244
				} catch (FileNotFoundException e) {
8526 manish.sha 245
					//log.error("Error Reading Shipment Report :- ", e);
8224 manish.sha 246
				}
247
 
248
				requestReturnsReport.setReportId( requestIdReturnsReportIdmap.get(returnsReportRequestId) );
249
				OutputStream returnsReport=null;
250
				try {
251
					returnsReport = new FileOutputStream( "/tmp/AmazonReturnsReport.txt" );
252
				} catch (FileNotFoundException e) {
8526 manish.sha 253
					//log.error("Error Getting Returns Report :- ", e);
8224 manish.sha 254
				}
255
				requestReturnsReport.setReportOutputStream( returnsReport );
256
				GetReportSample.invokeGetReport(service, requestReturnsReport);
257
 
8267 manish.sha 258
				System.out.println("Return Report ready please check\n");
8224 manish.sha 259
				CSVReader returnsReportReader = null; 
260
				try {
261
					returnsReportReader = new CSVReader(new FileReader("/tmp/AmazonReturnsReport.txt"),'\t');
262
				} catch (FileNotFoundException e) {
8526 manish.sha 263
					//log.error("Error Reading Returns Report :- ", e);
8224 manish.sha 264
				}
265
 
266
 
267
				String [] shipmentReportData;
268
				String [] returnsReportData;
269
 
270
				boolean matchFoundInReturns= false;
271
 
272
				try {
273
					while ((shipmentReportData = shipmentReportReader.readNext()) != null) {
274
						if(!shipmentReportData[0].equalsIgnoreCase("amazon-order-id")){
275
							returnReportWhile:while((returnsReportData = returnsReportReader.readNext()) != null){
276
								if(!returnsReportData[1].equalsIgnoreCase("amazon-order-id")){
277
									if(shipmentReportData[0].equalsIgnoreCase(returnsReportData[1])){
278
										matchFoundInReturns= true;
279
										break returnReportWhile;
280
									}
281
									else{
282
										XMLGregorianCalendar shipmentDate= df.newXMLGregorianCalendar(shipmentReportData[8].substring(0, shipmentReportData[8].indexOf("T")));
283
										XMLGregorianCalendar estimatedDate= df.newXMLGregorianCalendar(shipmentReportData[44].substring(0, shipmentReportData[44].indexOf("T")));
284
										if(shipmentDate.compare(estimatedDate)==-1)
285
											matchFoundInReturns= false;
286
									}
287
								}
288
							}
289
 
290
						if(!matchFoundInReturns){
291
							fbaCustomersEmails.add(shipmentReportData[10]);
292
						}
293
						}
294
 
295
					}
296
				} catch (IOException e) {
8526 manish.sha 297
					//log.error("Error Reading IO operations :- ", e);
8224 manish.sha 298
				} 
8505 vikram.rag 299
 
8224 manish.sha 300
				break;
301
			}
302
			else{ 
8526 manish.sha 303
				System.out.println("Report not ready\n");
8224 manish.sha 304
				try {
305
					Thread.sleep(5*60*1000);
306
				} catch (InterruptedException e) {
8526 manish.sha 307
					//log.error("Error During getting Response :- ", e);
8224 manish.sha 308
				}
309
			}
8526 manish.sha 310
			m=m+1;
8269 manish.sha 311
		}
8526 manish.sha 312
		System.out.println("m's Value.... "+m);
8224 manish.sha 313
 
8526 manish.sha 314
		//HelperClient helperServiceClient;
8224 manish.sha 315
		try {
8526 manish.sha 316
			//helperServiceClient = new HelperClient("helper_service_server_host_prod", "helper_service_server_port_prod");
317
			//in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
8267 manish.sha 318
			System.out.print("Email Id List: \n");
8526 manish.sha 319
			for(int k=0; k<fbaCustomersEmails.size();i++){
8269 manish.sha 320
				System.out.print(fbaCustomersEmails.get(i)+"\n");
8526 manish.sha 321
				//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 322
			}
8224 manish.sha 323
		} catch (Exception e) {
8526 manish.sha 324
			//log.error("Error Getting Helper Client :- ", e);
8224 manish.sha 325
		}
326
 
327
 
328
 
329
		// Note that depending on the type of report being downloaded, a report can reach 
330
		// sizes greater than 1GB. For this reason we recommend that you _always_ program to
331
		// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
332
		// the in-memory size limit and have to re-work your solution.
333
		//
8525 manish.sha 334
		System.exit(1);
8224 manish.sha 335
 
336
	}
337
 
338
}