Subversion Repositories SmartDukaan

Rev

Rev 12355 | Rev 12661 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12449 kshitij.so 1
package in.shop2020.support.utils;
12355 vikram.rag 2
 
12449 kshitij.so 3
import in.shop2020.model.v1.catalog.AmazonPromotion;
4
 
12355 vikram.rag 5
import java.io.BufferedReader;
6
import java.io.BufferedWriter;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileReader;
10
import java.io.FileWriter;
11
import java.io.IOException;
12
import java.security.DigestInputStream;
13
import java.security.MessageDigest;
14
import java.security.NoSuchAlgorithmException;
15
import java.util.Arrays;
12449 kshitij.so 16
import java.util.Calendar;
17
import java.util.List;
12355 vikram.rag 18
 
19
import org.apache.commons.codec.binary.Base64;
20
 
21
import com.amazonaws.mws.MarketplaceWebService;
22
import com.amazonaws.mws.MarketplaceWebServiceClient;
23
import com.amazonaws.mws.MarketplaceWebServiceConfig;
24
import com.amazonaws.mws.MarketplaceWebServiceException;
25
import com.amazonaws.mws.model.IdList;
26
import com.amazonaws.mws.model.SubmitFeedRequest;
27
import com.amazonaws.mws.model.SubmitFeedResponse;
12449 kshitij.so 28
import com.ibm.icu.text.SimpleDateFormat;
12355 vikram.rag 29
 
30
public class PromotionFeed {
12449 kshitij.so 31
	public String CreatePromotionFeed(List<AmazonPromotion> amazonPromotions) throws IOException{ 
12355 vikram.rag 32
		BufferedReader reader = null;
33
		BufferedWriter writer = null;
34
		String promotionFilename = null;
12449 kshitij.so 35
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
36
		Calendar calendar = Calendar.getInstance();
12355 vikram.rag 37
		try {
12449 kshitij.so 38
			promotionFilename = "/tmp/Promotion-"+System.currentTimeMillis()+".txt";
39
			reader = new BufferedReader(new FileReader("/tmp/p.txt"));
12355 vikram.rag 40
			writer = new BufferedWriter(new FileWriter(promotionFilename));
41
			String Line;
42
			while ((Line = reader.readLine()) != null) {
43
				writer.write(Line+"\n");
44
			}
12449 kshitij.so 45
			for(AmazonPromotion am:amazonPromotions){
46
			    calendar.setTimeInMillis(am.getStartDate());
47
		        String start = formatter.format(calendar.getTime());
48
		        calendar.setTimeInMillis(am.getEndDate());
49
                String end = formatter.format(calendar.getTime());
50
				writer.write(am.getSku()+"\t"+
12355 vikram.rag 51
						""+"\t"
52
						+""+"\t"
53
						+""+"\t"
54
						+""+"\t"
55
						+""+"\t"
56
						+"\t"
57
						+"\t"
58
						+"\t"
59
						+"\t"
60
						+"PartialUpdate"+"\t"
12449 kshitij.so 61
						+am.getStandardPrice()+"\t"
12355 vikram.rag 62
						+""+"\t"	
63
						+""+"\t"
64
						+""+"\t"
65
						+""+"\t"
12449 kshitij.so 66
						+"1"+"\t"
67
						+am.getSalePrice()+"\t"
68
						+start+"\t"
69
						+end+"\t"
12355 vikram.rag 70
						+""+"\t"+"\n");
71
			}
72
 
73
		} catch (FileNotFoundException e1) {
74
			e1.printStackTrace();
75
		}
76
		finally{
77
			writer.close();
78
			reader.close();
79
			System.out.println("Done");
80
		}
81
		return promotionFilename;
82
	}
83
	public String SubmitPromotionFeed(String file) throws FileNotFoundException, InterruptedException, MarketplaceWebServiceException{
84
		MarketplaceWebService service = getMarketplaceServiceInstance();
85
		SubmitFeedRequest requestPromotion = new SubmitFeedRequest();
86
		requestPromotion.setMerchant("AF6E3O0VE0X4D");
87
		requestPromotion.setMarketplaceIdList(new IdList(Arrays.asList("A21TJRUUN4KGV")));
88
		requestPromotion.setFeedType("_POST_FLAT_FILE_LISTINGS_DATA_");
89
		FileInputStream promotionfis = new FileInputStream(file);
90
		requestPromotion.setContentMD5(computeContentMD5HeaderValue(promotionfis));
91
		requestPromotion.setFeedContent(promotionfis);
92
		return invokeSubmitFeed(service,requestPromotion);
93
	}
94
	public MarketplaceWebService getMarketplaceServiceInstance(){
95
		final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
96
		final String secretAccessKey = "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";
97
		final String appName = "Test";
98
		final String appVersion = "1.0";
99
		MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
100
		config.setServiceURL("https://mws.amazonservices.in");
101
		return new MarketplaceWebServiceClient(
102
				accessKeyId, secretAccessKey, appName, appVersion, config);
103
	}
104
	public static String computeContentMD5HeaderValue(FileInputStream fis) {
105
		try {
106
			DigestInputStream dis = new DigestInputStream(fis,
107
					MessageDigest.getInstance("MD5"));
108
			byte[] buffer = new byte[8192];
109
			while (dis.read(buffer) > 0)
110
				;
111
			String md5Content = new String(Base64.encodeBase64(dis
112
					.getMessageDigest().digest()));
113
			// Effectively resets the stream to be beginning of the file via a
114
			fis.getChannel().position(0);
115
			return md5Content;
116
		} catch (NoSuchAlgorithmException e) {
117
			e.printStackTrace();
118
		} catch (IOException e) {
119
			e.printStackTrace();
120
		}
121
		return null;
122
	}
123
	public String invokeSubmitFeed(MarketplaceWebService service,SubmitFeedRequest request) throws InterruptedException, MarketplaceWebServiceException {
124
				SubmitFeedResponse response = service.submitFeed(request);
125
				return response.getSubmitFeedResult().getFeedSubmissionInfo().getFeedSubmissionId();
126
	}
127
}