Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
10049 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.model.v1.catalog.FlipkartItem;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.catalog.MarketPlaceItemPrice;
7
import in.shop2020.thrift.clients.CatalogClient;
8
import in.shop2020.utils.GmailUtils;
9
 
10
import java.io.BufferedReader;
11
import java.io.File;
12
import java.io.IOException;
13
import java.io.InputStreamReader;
14
import java.io.UnsupportedEncodingException;
15
import java.util.ArrayList;
16
import java.util.HashMap;
17
import java.util.List;
18
import java.util.Map;
19
import javax.mail.MessagingException;
20
import org.apache.http.HttpResponse;
21
import org.apache.http.auth.AuthScope;
22
import org.apache.http.auth.UsernamePasswordCredentials;
23
import org.apache.http.client.ClientProtocolException;
24
import org.apache.http.client.methods.HttpPost;
25
import org.apache.http.entity.StringEntity;
26
import org.apache.http.impl.client.DefaultHttpClient;
27
import org.apache.thrift.TException;
28
import org.apache.thrift.transport.TTransportException;
29
 
30
 
31
 
32
public class CreateSubmitFlipkartPricingFeed {
10196 vikram.rag 33
	private static long time = System.currentTimeMillis();
10049 vikram.rag 34
	private static List<MarketPlaceItemPrice> flipkartPricingItems;
35
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
36
	private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
10196 vikram.rag 37
	static List<Long> updatedItems = new ArrayList<Long>();
10049 vikram.rag 38
	static java.text.SimpleDateFormat sdf;
39
	static String emailFromAddress;
40
	static String password;
41
	static GmailUtils mailer;
42
	static String sendTo[];
43
	static
44
	{
45
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
46
		emailFromAddress = "build@shop2020.in";
47
		password = "cafe@nes";
48
		mailer = new GmailUtils();
49
		sendTo = new String[]{"vikram.raghav@shop2020.in"};
50
		/*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
51
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
52
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
53
		 */
10197 vikram.rag 54
		Client catalogServiceClient = null;
55
		try {
56
			catalogServiceClient = new CatalogClient().getClient();
57
		} catch (TTransportException e) {
58
			e.printStackTrace();
59
		}
60
		try {
61
			time = System.currentTimeMillis();
62
			flipkartPricingItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
10199 vikram.rag 63
			List<FlipkartItem> flipkartItems = null;
64
			boolean retry = true;
65
			while(retry){
66
				try{
67
					flipkartItems = catalogServiceClient.getAllFlipkartItems();
68
					retry = false;
69
				}
70
				catch(Exception e){
71
					catalogServiceClient = new CatalogClient().getClient();
72
				}
73
			}
10049 vikram.rag 74
			for(FlipkartItem flipkartItem:flipkartItems){
75
				flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
76
			}
10197 vikram.rag 77
			List<Item> aliveItems = catalogServiceClient.getAllAliveItems();
78
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
79
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
80
			}
10049 vikram.rag 81
 
10197 vikram.rag 82
		} catch (TException e) {
83
			e.printStackTrace();
84
		}
85
		if(flipkartPricingItems.size()==0){
86
			String text = "";
87
			try {
88
				mailer.sendSSLMessage(sendTo,"No Change in Flipkart item Prices ", emailFromAddress, password, text);
89
				System.exit(0);
90
			} catch (MessagingException e) {
91
				e.printStackTrace();
92
			}
93
		}
94
 
10049 vikram.rag 95
	}
96
 
97
	public static void main(String[] args) throws ClientProtocolException, TException{
98
		DefaultHttpClient httpclient = new DefaultHttpClient();
99
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
10148 vikram.rag 100
		httpclient.getCredentialsProvider().setCredentials(
10049 vikram.rag 101
				new AuthScope("sandbox-api.flipkart.net", 443),
102
				new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
10148 vikram.rag 103
		//httpclient.getCredentialsProvider().setCredentials(
104
		//		new AuthScope("api.flipkart.net", 443),
105
		//		new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
106
		//HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
107
		HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
10049 vikram.rag 108
		StringBuffer jsonRequest = new StringBuffer();
109
		StringBuffer jsonStart = new StringBuffer();
110
		jsonStart = jsonStart.append("{\"listings\":[");
111
		StringBuffer jsonEnd = new StringBuffer();
112
		jsonEnd.append("]}");
113
		//System.out.println("JSON request  " + jsonRequest);
114
		int i=0;
115
		StringBuffer pricingItems =new StringBuffer();
116
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
117
			+ "<td><b>" + "Product Name" +"</b></td>"
118
			+"<td><b>" + "Item ID" + "</b></td>"
119
			+"<td><b>" + "Selling Price" + "</b></td>"
120
			+"</tr>";
121
		String tableFooter = "</table></html>";
122
		Item item;
123
		StringBuffer feedResponse = null;
124
		for(MarketPlaceItemPrice entry:flipkartPricingItems){
10196 vikram.rag 125
			if(!(entry.getLastUpdatedOn() > entry.getLastUpdatedOnMarketplace() && !entry.isIsPriceOverride() && entry.isIsListedOnSource() && entry.getSellingPrice()!=0)){
126
				continue;
127
			}
128
			updatedItems.add(entry.getItem_id());
10049 vikram.rag 129
			feedResponse=  new StringBuffer();
130
			item = aliveItemsMap.get(entry.getItem_id());
131
			System.out.println("Item ID  " + entry.getItem_id());
132
			System.out.println("Product Name " + getProductName(item));
133
			System.out.println("Selling Price " + entry.getSellingPrice());
134
			jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
135
					"\","+"\"attributeValues\""+":"+
136
					"{\"selling_price\""+":"+"\""+entry.getSellingPrice()+"}}");
10197 vikram.rag 137
 
10049 vikram.rag 138
			pricingItems.append("<tr>" 
139
					+ "<td>" +getProductName(item)+"</td>"
140
					+"<td>" + item.getId() + "</td>"
141
					+"<td>" + entry.getSellingPrice() + "</td>"
142
					+"</tr>");
143
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size() -1 ) ){
144
				jsonRequest.append(",");	
145
			}
146
			i++;
147
			if(i%10==0 || i==flipkartPricingItems.size()){
148
				StringEntity input = null;
149
				try {
150
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
151
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
152
					jsonRequest = new StringBuffer();
153
				} catch (UnsupportedEncodingException e) {
154
					e.printStackTrace();
155
				}
156
				input.setContentType("application/json");
157
 
158
				httppost.setEntity(input);
159
				HttpResponse response = null;
160
				try {
161
					System.out.println("Before send " );
162
					response = httpclient.execute(httppost);
163
					System.out.println("After send " );
164
				} catch (IOException e) {
165
					e.printStackTrace();
166
				}
167
				BufferedReader rd = null;
168
				try {
169
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
170
				} catch (IllegalStateException e) {
171
					e.printStackTrace();
172
				} catch (IOException e) {
173
					e.printStackTrace();
174
				}
175
				String line;
176
				try {
177
					while ((line = rd.readLine()) != null) {
178
						System.out.println("Response " + line);
179
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
180
						}
181
						else{
182
							feedResponse.append(line);
183
						}
184
 
185
					}
186
				} catch (IOException e) {
187
					// TODO Auto-generated catch block
188
					e.printStackTrace();
189
				}
190
			}
191
		}
192
		System.out.println("Before sending email");
193
		String text = tableHeader+pricingItems.toString()+tableFooter;
194
		try {
10196 vikram.rag 195
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 196
		} catch (MessagingException e) {
197
			e.printStackTrace();
198
		}
10196 vikram.rag 199
		Client catalogClient = null;
10049 vikram.rag 200
		if(feedResponse.toString().length()>0){
201
			try {
10196 vikram.rag 202
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
203
				try {
10197 vikram.rag 204
					catalogClient = new CatalogClient().getClient();
205
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 206
				} catch (TException e) {
207
					try {
208
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
209
					} catch (TTransportException e1) {
210
						e1.printStackTrace();
211
					} catch (TException e1) {
212
						e1.printStackTrace();
213
					}
214
					e.printStackTrace();
215
				}
10197 vikram.rag 216
 
10049 vikram.rag 217
			} catch (MessagingException e) {
10196 vikram.rag 218
				try {
10197 vikram.rag 219
					catalogClient = new CatalogClient().getClient();
220
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 221
				} catch (TException ex) {
222
					try {
223
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
224
					} catch (TTransportException e1) {
225
						e1.printStackTrace();
226
					} catch (TException e1) {
227
						e1.printStackTrace();
228
					}
229
					ex.printStackTrace();
230
				}
10049 vikram.rag 231
				e.printStackTrace();
232
			}
233
		}
234
		else{
235
			try {
10196 vikram.rag 236
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10049 vikram.rag 237
			} catch (MessagingException e) {
238
				e.printStackTrace();
239
			}
240
		}
241
	}
242
 
243
	static String getProductName(Item item){
244
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
245
 
246
	}
247
	static String getName(String name){
248
		if(name==null || name.length()==0){
249
			return "";
250
		}
251
		else{
252
			return name;
253
		}
254
	}
255
}