| 12449 |
kshitij.so |
1 |
package in.shop2020.support.utils;
|
| 12355 |
vikram.rag |
2 |
|
| 12449 |
kshitij.so |
3 |
import in.shop2020.model.v1.catalog.AmazonPromotion;
|
| 12661 |
kshitij.so |
4 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
|
|
5 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
|
|
6 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
7 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
8 |
import in.shop2020.utils.GmailUtils;
|
| 12449 |
kshitij.so |
9 |
|
| 12355 |
vikram.rag |
10 |
import java.io.BufferedReader;
|
|
|
11 |
import java.io.BufferedWriter;
|
| 12661 |
kshitij.so |
12 |
import java.io.File;
|
| 12355 |
vikram.rag |
13 |
import java.io.FileInputStream;
|
|
|
14 |
import java.io.FileNotFoundException;
|
|
|
15 |
import java.io.FileReader;
|
|
|
16 |
import java.io.FileWriter;
|
|
|
17 |
import java.io.IOException;
|
|
|
18 |
import java.security.DigestInputStream;
|
|
|
19 |
import java.security.MessageDigest;
|
|
|
20 |
import java.security.NoSuchAlgorithmException;
|
| 12661 |
kshitij.so |
21 |
import java.util.ArrayList;
|
| 12355 |
vikram.rag |
22 |
import java.util.Arrays;
|
| 12449 |
kshitij.so |
23 |
import java.util.Calendar;
|
| 12661 |
kshitij.so |
24 |
import java.util.GregorianCalendar;
|
| 12449 |
kshitij.so |
25 |
import java.util.List;
|
| 12355 |
vikram.rag |
26 |
|
|
|
27 |
import org.apache.commons.codec.binary.Base64;
|
| 12661 |
kshitij.so |
28 |
import org.apache.thrift.TException;
|
|
|
29 |
import org.apache.thrift.transport.TTransportException;
|
|
|
30 |
import org.slf4j.Logger;
|
|
|
31 |
import org.slf4j.LoggerFactory;
|
| 12355 |
vikram.rag |
32 |
|
|
|
33 |
import com.amazonaws.mws.MarketplaceWebService;
|
|
|
34 |
import com.amazonaws.mws.MarketplaceWebServiceClient;
|
|
|
35 |
import com.amazonaws.mws.MarketplaceWebServiceConfig;
|
|
|
36 |
import com.amazonaws.mws.MarketplaceWebServiceException;
|
|
|
37 |
import com.amazonaws.mws.model.IdList;
|
|
|
38 |
import com.amazonaws.mws.model.SubmitFeedRequest;
|
|
|
39 |
import com.amazonaws.mws.model.SubmitFeedResponse;
|
| 12449 |
kshitij.so |
40 |
import com.ibm.icu.text.SimpleDateFormat;
|
| 12661 |
kshitij.so |
41 |
import com.amazonaws.mws.samples.CheckPromoFeedSubmission;
|
| 12355 |
vikram.rag |
42 |
|
| 12661 |
kshitij.so |
43 |
public class PromotionFeed extends Thread{
|
|
|
44 |
|
|
|
45 |
private static Logger logger = LoggerFactory.getLogger(PromotionFeed.class);
|
|
|
46 |
private List<AmazonPromotion> amazonPromotions;
|
|
|
47 |
public String[] sendTo = new String[]{ "rajneesh.arora@shop2020.in","kshitij.sood@shop2020.in","anikendra.das@shop2020.in",
|
|
|
48 |
"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com","chandan.kumar@shop2020.in"};
|
|
|
49 |
//public String[] sendTo = new String[]{ "kshitij.sood@saholic.com"};
|
|
|
50 |
public String emailFromAddress = "build-staging@shop2020.in";
|
|
|
51 |
public String password = "shop2020";
|
|
|
52 |
public String[] engineering = new String[]{ "eng@shop2020.in"};
|
|
|
53 |
|
|
|
54 |
public PromotionFeed(List<AmazonPromotion> amazonPromotions){
|
|
|
55 |
this.amazonPromotions = amazonPromotions;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
void CreatePromotionFeed(List<AmazonPromotion> amazonPromotions) throws IOException, InterruptedException, MarketplaceWebServiceException{
|
|
|
59 |
BufferedReader reader = null;
|
|
|
60 |
BufferedWriter writer = null;
|
|
|
61 |
String promotionFilename = null;
|
|
|
62 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
63 |
Calendar calendar = Calendar.getInstance();
|
|
|
64 |
try {
|
|
|
65 |
promotionFilename = "/tmp/Promotion-"+System.currentTimeMillis()+".txt";
|
|
|
66 |
reader = new BufferedReader(new FileReader("/temp-uploads/amazon-promo-flat-file.txt"));
|
|
|
67 |
writer = new BufferedWriter(new FileWriter(promotionFilename));
|
|
|
68 |
String Line;
|
|
|
69 |
while ((Line = reader.readLine()) != null) {
|
|
|
70 |
writer.write(Line+"\n");
|
|
|
71 |
}
|
|
|
72 |
for(AmazonPromotion am:amazonPromotions){
|
|
|
73 |
calendar.setTimeInMillis(am.getStartDate());
|
|
|
74 |
String start = formatter.format(calendar.getTime());
|
|
|
75 |
calendar.setTimeInMillis(am.getEndDate());
|
| 12449 |
kshitij.so |
76 |
String end = formatter.format(calendar.getTime());
|
| 12661 |
kshitij.so |
77 |
writer.write(am.getSku()+"\t"+
|
|
|
78 |
""+"\t"
|
|
|
79 |
+""+"\t"
|
|
|
80 |
+""+"\t"
|
|
|
81 |
+""+"\t"
|
|
|
82 |
+""+"\t"
|
|
|
83 |
+"\t"
|
|
|
84 |
+"\t"
|
|
|
85 |
+"\t"
|
|
|
86 |
+"\t"
|
|
|
87 |
+"PartialUpdate"+"\t"
|
|
|
88 |
+am.getStandardPrice()+"\t"
|
|
|
89 |
+""+"\t"
|
|
|
90 |
+""+"\t"
|
|
|
91 |
+""+"\t"
|
|
|
92 |
+""+"\t"
|
|
|
93 |
+"1"+"\t"
|
|
|
94 |
+am.getSalePrice()+"\t"
|
|
|
95 |
+start+"\t"
|
|
|
96 |
+end+"\t"
|
|
|
97 |
+""+"\t"+"\n");
|
|
|
98 |
}
|
| 12355 |
vikram.rag |
99 |
|
| 12661 |
kshitij.so |
100 |
} catch (FileNotFoundException e1) {
|
|
|
101 |
e1.printStackTrace();
|
|
|
102 |
}
|
|
|
103 |
finally{
|
|
|
104 |
writer.close();
|
|
|
105 |
reader.close();
|
|
|
106 |
}
|
|
|
107 |
try {
|
|
|
108 |
SubmitPromotionFeed(promotionFilename,amazonPromotions);
|
|
|
109 |
} catch (TException e) {
|
|
|
110 |
logger.error("TException in subit promtiono feed ",e);
|
|
|
111 |
GmailUtils mailer = new GmailUtils();
|
|
|
112 |
try{
|
|
|
113 |
//mailer.sendSSLMessage(sendTo, "Amazon Promotion ( Submission Id " + feedSubmissionId + " )","", emailFromAddress , password, new ArrayList<File>(), tempFile);
|
|
|
114 |
mailer.sendSSLMessage(engineering,"Thrift Exception in configuring amazon promotion",emailFromAddress, password, "");
|
|
|
115 |
}
|
|
|
116 |
catch(Exception ex){
|
|
|
117 |
logger.info("Exception"+ex);
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
public void run(){
|
|
|
123 |
try {
|
|
|
124 |
this.CreatePromotionFeed(this.amazonPromotions);
|
|
|
125 |
} catch (IOException e) {
|
|
|
126 |
logger.error("IO exception while creating promo",e);
|
|
|
127 |
} catch (InterruptedException e) {
|
|
|
128 |
logger.error("IE exception while creating promo",e);
|
|
|
129 |
} catch (MarketplaceWebServiceException e) {
|
|
|
130 |
logger.error("MWS exception while creating promo",e);
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public void SubmitPromotionFeed(String file,List<AmazonPromotion> amazonPromotions) throws InterruptedException, MarketplaceWebServiceException, IOException, TException{
|
|
|
135 |
MarketplaceWebService service = getMarketplaceServiceInstance();
|
|
|
136 |
SubmitFeedRequest requestPromotion = new SubmitFeedRequest();
|
|
|
137 |
requestPromotion.setMerchant("AF6E3O0VE0X4D");
|
|
|
138 |
requestPromotion.setMarketplaceIdList(new IdList(Arrays.asList("A21TJRUUN4KGV")));
|
|
|
139 |
requestPromotion.setFeedType("_POST_FLAT_FILE_LISTINGS_DATA_");
|
|
|
140 |
FileInputStream promotionfis = new FileInputStream(file);
|
|
|
141 |
requestPromotion.setContentMD5(computeContentMD5HeaderValue(promotionfis));
|
|
|
142 |
requestPromotion.setFeedContent(promotionfis);
|
|
|
143 |
String feedSubmissionId = invokeSubmitFeed(service,requestPromotion);
|
|
|
144 |
sendPromotionConfigurationEmail(feedSubmissionId);
|
|
|
145 |
CheckPromoFeedSubmission ck = new CheckPromoFeedSubmission();
|
|
|
146 |
int checkResultCount=0;
|
|
|
147 |
List<String> failedSkus=null;
|
|
|
148 |
List<AmazonPromotion> successfulPromo = new ArrayList<AmazonPromotion>();
|
|
|
149 |
while (failedSkus == null){
|
|
|
150 |
failedSkus = ck.ProcessFeed(feedSubmissionId);
|
|
|
151 |
if (failedSkus == null){
|
|
|
152 |
checkResultCount++;
|
|
|
153 |
if (checkResultCount == 7){
|
|
|
154 |
String text = "";
|
|
|
155 |
String[] sendTo = { "eng@shop2020.in" };
|
|
|
156 |
String emailSubjectTxt = "Unable To Receive Promo Feed Result.Submission Id "+feedSubmissionId;
|
|
|
157 |
String emailFromAddress = "build-staging@shop2020.in";
|
|
|
158 |
String password = "shop2020";
|
|
|
159 |
GmailUtils mailer = new GmailUtils();
|
|
|
160 |
try {
|
|
|
161 |
mailer.sendSSLMessage(sendTo, emailSubjectTxt, text, emailFromAddress, password, new ArrayList<File>());
|
|
|
162 |
}
|
|
|
163 |
catch (Exception e) {
|
|
|
164 |
e.printStackTrace();
|
|
|
165 |
}
|
|
|
166 |
return;
|
|
|
167 |
}
|
|
|
168 |
logger.info("=====Feed Result Not Ready Retry Again After 3 Minutes=============");
|
|
|
169 |
Thread.sleep(180000);
|
|
|
170 |
}
|
|
|
171 |
else{
|
|
|
172 |
for (AmazonPromotion am :amazonPromotions){
|
|
|
173 |
if (!failedSkus.contains(am.getSku())){
|
|
|
174 |
successfulPromo.add(am);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
Client catalogClient;
|
|
|
178 |
try {
|
|
|
179 |
catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
180 |
} catch (TTransportException e) {
|
|
|
181 |
logger.error("Catalog Service ex ",e);
|
|
|
182 |
return;
|
|
|
183 |
}
|
|
|
184 |
catalogClient.updateAmazonPromotion(successfulPromo);
|
|
|
185 |
if (failedSkus.size()>0){
|
|
|
186 |
sendFailerMail(failedSkus,feedSubmissionId);
|
|
|
187 |
}
|
|
|
188 |
else{
|
|
|
189 |
sendSuccessMail(feedSubmissionId);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
public void sendFailerMail(List<String> failedSkus,String feedSubmissionId){
|
|
|
196 |
Client catalogClient;
|
|
|
197 |
StringBuffer sb = new StringBuffer();
|
|
|
198 |
sb.append("<html><table border=\"1\" align=\"center\">"
|
|
|
199 |
+ "<caption><b>" + "Amazon Promotion Failure" + "</b></caption>"
|
|
|
200 |
+ "<tr>" + "<td style=\"text-align:center;\"><b>" + "AMAZON-SKU" + "</b></td>" +
|
|
|
201 |
"<td style=\"text-align:center;\"><b>"
|
|
|
202 |
+ "PRODUCT-NAME" + "</b>"
|
|
|
203 |
+ "</tr>");
|
|
|
204 |
try {
|
|
|
205 |
catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
206 |
} catch (TTransportException e) {
|
|
|
207 |
logger.error("Catalog Service ex ",e);
|
|
|
208 |
return;
|
|
|
209 |
}
|
|
|
210 |
GmailUtils mailer = new GmailUtils();
|
|
|
211 |
for (String sku :failedSkus){
|
|
|
212 |
Item item = null;
|
|
|
213 |
try {
|
|
|
214 |
item = catalogClient.getItem(Long.valueOf(sku.substring(3)));
|
|
|
215 |
} catch (Exception e) {
|
|
|
216 |
logger.error("Exception while parsing sku ",e);
|
|
|
217 |
return;
|
|
|
218 |
}
|
|
|
219 |
sb.append("<tr>"+"<td style=\"text-align:center;\">"+sku+"</td>"
|
|
|
220 |
+"<td style=\"text-align:center;\">"+getVaildName(item.getBrand())+" "
|
|
|
221 |
+getVaildName(item.getModelName())+" "+getVaildName(item.getModelNumber())+" "+getVaildName(item.getColor())+"</td>"
|
|
|
222 |
+"</tr>"
|
|
|
223 |
);
|
|
|
224 |
}
|
|
|
225 |
sb.append("</table></html>");
|
|
|
226 |
// String tempFile = "/tmp/"+getTimeInMilliseconds()+"-amazon-promo-mail.htm";
|
|
|
227 |
// BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
|
|
|
228 |
// out.write(sb.toString());
|
|
|
229 |
// out.flush();
|
|
|
230 |
// out.close();
|
|
|
231 |
try{
|
|
|
232 |
//mailer.sendSSLMessage(sendTo, "Amazon Promotion ( Submission Id " + feedSubmissionId + " )","", emailFromAddress , password, new ArrayList<File>(), tempFile);
|
|
|
233 |
mailer.sendSSLMessage(sendTo,"Amazon Promotion ( Submission Id " + feedSubmissionId + " )",emailFromAddress, password, sb.toString());
|
|
|
234 |
}
|
|
|
235 |
catch(Exception e){
|
|
|
236 |
logger.info("Exception"+e);
|
|
|
237 |
}
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
public void sendPromotionConfigurationEmail(String feedSubmissionId) throws IOException{
|
|
|
241 |
Client catalogClient;
|
|
|
242 |
StringBuffer sb = new StringBuffer();
|
|
|
243 |
sb.append("<html><table border=\"1\" align=\"center\">"
|
|
|
244 |
+ "<caption><b>" + "Amazon Promotion Details" + "</b></caption>"
|
|
|
245 |
+ "<tr>" + "<td style=\"text-align:center;\"><b>" + "AMAZON-SKU" + "</b></td>" +
|
|
|
246 |
"<td style=\"text-align:center;\"><b>"
|
|
|
247 |
+ "PRODUCT-NAME" + "</b></td>" + "<td style=\"text-align:center;\"><b>" + "STANDARD-PRICE"
|
|
|
248 |
+ "</b></td>" + "<td style=\"text-align:center;\"><b>" + "SALE-PRICE" + "</b></td>"
|
|
|
249 |
+ "<td style=\"text-align:center;\"><b>" + "SUBSIDY" + "</b></td>" +
|
|
|
250 |
"<td style=\"text-align:center;\"><b>"
|
|
|
251 |
+ "START-DATE" + "</b></td>" + "<td style=\"text-align:center;\"><b>" + "END-DATE"
|
|
|
252 |
+ "</tr>");
|
|
|
253 |
try {
|
|
|
254 |
catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
|
|
255 |
} catch (TTransportException e) {
|
|
|
256 |
logger.error("Catalog Service ex ",e);
|
|
|
257 |
return;
|
|
|
258 |
}
|
|
|
259 |
GmailUtils mailer = new GmailUtils();
|
|
|
260 |
for (AmazonPromotion amPromotion : this.amazonPromotions){
|
|
|
261 |
Item item = null;
|
|
|
262 |
try {
|
|
|
263 |
item = catalogClient.getItem(Long.valueOf(amPromotion.getSku().substring(3)));
|
|
|
264 |
} catch (Exception e) {
|
|
|
265 |
logger.error("Exception while parsing sku ",e);
|
|
|
266 |
return;
|
|
|
267 |
}
|
|
|
268 |
sb.append("<tr>"+"<td style=\"text-align:center;\">"+amPromotion.getSku()+"</td>"
|
|
|
269 |
+"<td style=\"text-align:center;\">"+getVaildName(item.getBrand())+" "
|
|
|
270 |
+getVaildName(item.getModelName())+" "+getVaildName(item.getModelNumber())+" "+getVaildName(item.getColor())+"</td>"
|
|
|
271 |
+"<td style=\"text-align:center;\">"+amPromotion.getStandardPrice()+"</td>"
|
|
|
272 |
+"<td style=\"text-align:center;\">"+amPromotion.getSalePrice()+"</td>"
|
|
|
273 |
+"<td style=\"text-align:center;\">"+amPromotion.getSubsidyAmount()+"</td>"
|
|
|
274 |
+"<td style=\"text-align:center;\">"+getDate(amPromotion.getStartDate())+"</td>"
|
|
|
275 |
+"<td style=\"text-align:center;\">"+getDate(amPromotion.getEndDate())+"</td>"+"</tr>"
|
|
|
276 |
);
|
|
|
277 |
}
|
|
|
278 |
sb.append("</table></html>");
|
|
|
279 |
// String tempFile = "/tmp/"+getTimeInMilliseconds()+"-amazon-promo-mail.htm";
|
|
|
280 |
// BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
|
|
|
281 |
// out.write(sb.toString());
|
|
|
282 |
// out.flush();
|
|
|
283 |
// out.close();
|
|
|
284 |
try{
|
|
|
285 |
//mailer.sendSSLMessage(sendTo, "Amazon Promotion ( Submission Id " + feedSubmissionId + " )","", emailFromAddress , password, new ArrayList<File>(), tempFile);
|
|
|
286 |
mailer.sendSSLMessage(sendTo,"Amazon Promotion ( Submission Id " + feedSubmissionId + " )",emailFromAddress, password, sb.toString());
|
|
|
287 |
}
|
|
|
288 |
catch(Exception e){
|
|
|
289 |
logger.info("Exception"+e);
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
public void sendSuccessMail(String feedSubmissionId){
|
|
|
294 |
GmailUtils mailer = new GmailUtils();
|
|
|
295 |
try{
|
|
|
296 |
//mailer.sendSSLMessage(sendTo, "Amazon Promotion ( Submission Id " + feedSubmissionId + " )","", emailFromAddress , password, new ArrayList<File>(), tempFile);
|
|
|
297 |
mailer.sendSSLMessage(sendTo,"Amazon Promotion Successfully configured( Submission Id " + feedSubmissionId + " )",emailFromAddress, password, "");
|
|
|
298 |
}
|
|
|
299 |
catch(Exception e){
|
|
|
300 |
logger.info("Exception"+e);
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
public String getVaildName(String name){
|
|
|
305 |
return name!=null?name:"";
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
public String getDate(long timestamp){
|
|
|
309 |
return new java.util.Date(timestamp).toLocaleString();
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
public String getTimeInMilliseconds(){
|
|
|
313 |
Calendar cal=GregorianCalendar.getInstance();
|
|
|
314 |
return String.valueOf(cal.getTimeInMillis());
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
public MarketplaceWebService getMarketplaceServiceInstance(){
|
|
|
318 |
final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
|
|
|
319 |
final String secretAccessKey = "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg";
|
|
|
320 |
final String appName = "Test";
|
|
|
321 |
final String appVersion = "1.0";
|
|
|
322 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
323 |
config.setServiceURL("https://mws.amazonservices.in");
|
|
|
324 |
return new MarketplaceWebServiceClient(
|
|
|
325 |
accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
public static String computeContentMD5HeaderValue(FileInputStream fis) {
|
|
|
329 |
try {
|
|
|
330 |
DigestInputStream dis = new DigestInputStream(fis,
|
|
|
331 |
MessageDigest.getInstance("MD5"));
|
|
|
332 |
byte[] buffer = new byte[8192];
|
|
|
333 |
while (dis.read(buffer) > 0)
|
|
|
334 |
;
|
|
|
335 |
String md5Content = new String(Base64.encodeBase64(dis
|
|
|
336 |
.getMessageDigest().digest()));
|
|
|
337 |
// Effectively resets the stream to be beginning of the file via a
|
|
|
338 |
fis.getChannel().position(0);
|
|
|
339 |
return md5Content;
|
|
|
340 |
} catch (NoSuchAlgorithmException e) {
|
|
|
341 |
e.printStackTrace();
|
|
|
342 |
} catch (IOException e) {
|
|
|
343 |
e.printStackTrace();
|
|
|
344 |
}
|
|
|
345 |
return null;
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
public String invokeSubmitFeed(MarketplaceWebService service,SubmitFeedRequest request) throws InterruptedException, MarketplaceWebServiceException {
|
|
|
349 |
SubmitFeedResponse response = service.submitFeed(request);
|
|
|
350 |
return response.getSubmitFeedResult().getFeedSubmissionInfo().getFeedSubmissionId();
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
}
|