Subversion Repositories SmartDukaan

Rev

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