Subversion Repositories SmartDukaan

Rev

Rev 13789 | 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;
13761 manish.sha 7
import in.shop2020.serving.services.FlipkartPricingPannel;
10049 vikram.rag 8
import in.shop2020.thrift.clients.CatalogClient;
9
import in.shop2020.utils.GmailUtils;
10
 
11
import java.io.BufferedReader;
12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStreamReader;
15
import java.io.UnsupportedEncodingException;
11444 vikram.rag 16
import java.nio.charset.Charset;
10049 vikram.rag 17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
import javax.mail.MessagingException;
11444 vikram.rag 22
 
23
import org.apache.commons.codec.binary.Base64;
24
import org.apache.http.HttpHeaders;
10049 vikram.rag 25
import org.apache.http.HttpResponse;
26
import org.apache.http.auth.AuthScope;
27
import org.apache.http.auth.UsernamePasswordCredentials;
28
import org.apache.http.client.ClientProtocolException;
29
import org.apache.http.client.methods.HttpPost;
30
import org.apache.http.entity.StringEntity;
31
import org.apache.http.impl.client.DefaultHttpClient;
32
import org.apache.thrift.TException;
33
import org.apache.thrift.transport.TTransportException;
13761 manish.sha 34
import org.json.JSONException;
10049 vikram.rag 35
 
36
 
37
 
38
public class CreateSubmitFlipkartPricingFeed {
10196 vikram.rag 39
	private static long time = System.currentTimeMillis();
10202 vikram.rag 40
	private static List<MarketPlaceItemPrice> flipkartPricingItems = new ArrayList<MarketPlaceItemPrice>();
10049 vikram.rag 41
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
42
	private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
10196 vikram.rag 43
	static List<Long> updatedItems = new ArrayList<Long>();
10049 vikram.rag 44
	static java.text.SimpleDateFormat sdf;
45
	static String emailFromAddress;
46
	static String password;
47
	static GmailUtils mailer;
48
	static String sendTo[];
13761 manish.sha 49
	static String errorSendTo[];
10202 vikram.rag 50
	private static List<MarketPlaceItemPrice> flipkartPricingUpdateItems;
10049 vikram.rag 51
	static
52
	{
53
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
54
		emailFromAddress = "build@shop2020.in";
55
		password = "cafe@nes";
56
		mailer = new GmailUtils();
13766 manish.sha 57
		//sendTo = new String[]{"manish.sharma@shop2020.in"};
13757 manish.sha 58
		sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
10049 vikram.rag 59
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
13793 manish.sha 60
				"yukti.jain@shop2020.in","yatin.singh@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};
13766 manish.sha 61
 
13761 manish.sha 62
		errorSendTo = new String[]{"kshitij.sood@shop2020.in","manish.sharma@shop2020.in","amit.gupta@shop2020.in","anikendra.das@shop2020.in"};
10197 vikram.rag 63
		Client catalogServiceClient = null;
64
		try {
65
			catalogServiceClient = new CatalogClient().getClient();
66
		} catch (TTransportException e) {
67
			e.printStackTrace();
68
		}
69
		try {
70
			time = System.currentTimeMillis();
10202 vikram.rag 71
			flipkartPricingUpdateItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
72
			for(MarketPlaceItemPrice flipkartPricingUpdateItem:flipkartPricingUpdateItems){
73
				if(flipkartPricingUpdateItem.getLastUpdatedOn() > flipkartPricingUpdateItem.getLastUpdatedOnMarketplace() && !flipkartPricingUpdateItem.isIsPriceOverride() && flipkartPricingUpdateItem.isIsListedOnSource() && flipkartPricingUpdateItem.getSellingPrice()!=0){
74
					flipkartPricingItems.add(flipkartPricingUpdateItem);
75
				}
76
			}
10199 vikram.rag 77
			List<FlipkartItem> flipkartItems = null;
78
			boolean retry = true;
79
			while(retry){
80
				try{
81
					flipkartItems = catalogServiceClient.getAllFlipkartItems();
82
					retry = false;
83
				}
84
				catch(Exception e){
85
					catalogServiceClient = new CatalogClient().getClient();
86
				}
87
			}
10049 vikram.rag 88
			for(FlipkartItem flipkartItem:flipkartItems){
89
				flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
90
			}
10200 vikram.rag 91
			retry = true;
92
			List<Item> aliveItems = null;
93
			while(retry){
94
				try{
95
					aliveItems = catalogServiceClient.getAllAliveItems();
96
					retry = false;
97
				}
98
				catch(Exception e){
99
					catalogServiceClient = new CatalogClient().getClient();
100
				}
101
			}
10197 vikram.rag 102
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
103
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
104
			}
10049 vikram.rag 105
 
10197 vikram.rag 106
		} catch (TException e) {
107
			e.printStackTrace();
108
		}
109
		if(flipkartPricingItems.size()==0){
110
			String text = "";
111
			try {
112
				mailer.sendSSLMessage(sendTo,"No Change in Flipkart item Prices ", emailFromAddress, password, text);
113
				System.exit(0);
114
			} catch (MessagingException e) {
115
				e.printStackTrace();
116
			}
117
		}
118
 
10049 vikram.rag 119
	}
120
 
121
	public static void main(String[] args) throws ClientProtocolException, TException{
122
		DefaultHttpClient httpclient = new DefaultHttpClient();
11444 vikram.rag 123
		//httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
10206 vikram.rag 124
		//httpclient.getCredentialsProvider().setCredentials(
125
		//		new AuthScope("sandbox-api.flipkart.net", 443),
126
		//		new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
11444 vikram.rag 127
		//httpclient.getCredentialsProvider().setCredentials(
128
		//		new AuthScope("api.flipkart.net", 443),
129
		//		new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
130
 
10206 vikram.rag 131
		HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
11444 vikram.rag 132
 
133
		String auth = "m2z93iskuj81qiid"+":"+"0c7ab6a5-98c0-4cdc-8be3-72c591e0add4";
134
		byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
135
		String authHeader = "Basic " + new String(encodedAuth);
136
		httppost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
10206 vikram.rag 137
		//HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
10049 vikram.rag 138
		StringBuffer jsonRequest = new StringBuffer();
139
		StringBuffer jsonStart = new StringBuffer();
140
		jsonStart = jsonStart.append("{\"listings\":[");
141
		StringBuffer jsonEnd = new StringBuffer();
142
		jsonEnd.append("]}");
143
		//System.out.println("JSON request  " + jsonRequest);
144
		int i=0;
145
		StringBuffer pricingItems =new StringBuffer();
146
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
147
			+ "<td><b>" + "Product Name" +"</b></td>"
148
			+"<td><b>" + "Item ID" + "</b></td>"
149
			+"<td><b>" + "Selling Price" + "</b></td>"
150
			+"</tr>";
151
		String tableFooter = "</table></html>";
152
		Item item;
13761 manish.sha 153
		Map<Long, Boolean> feedResponseMap = new HashMap<Long, Boolean>();
154
		FlipkartPricingPannel fkPricing = new FlipkartPricingPannel();
10049 vikram.rag 155
		for(MarketPlaceItemPrice entry:flipkartPricingItems){
11152 vikram.rag 156
			if(aliveItemsMap.containsKey(entry.getItem_id())){
157
				item = aliveItemsMap.get(entry.getItem_id());	
158
			}
159
			else{
160
				continue;
161
			}
10049 vikram.rag 162
			System.out.println("Item ID  " + entry.getItem_id());
163
			System.out.println("Product Name " + getProductName(item));
164
			System.out.println("Selling Price " + entry.getSellingPrice());
13766 manish.sha 165
			pricingItems.append("<tr>" 
166
					+ "<td>" +getProductName(item)+"</td>"
167
					+"<td>" + item.getId() + "</td>"
168
					+"<td>" + entry.getSellingPrice() + "</td>"
169
					+"</tr>");
13761 manish.sha 170
			try {
171
				feedResponseMap.put(entry.getItem_id(), fkPricing.updatePrice(flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart(), entry.getSellingPrice()+""));
13787 manish.sha 172
			} catch (JSONException e) {
173
				e.printStackTrace();
13761 manish.sha 174
				try {
175
					mailer.sendSSLMessage(errorSendTo, "Error While Flipkart Pricing Updation Item Id: "+ entry.getItem_id(), "Due to Json Exception in Flipkart Pricing Panel", emailFromAddress, password,new ArrayList<File>());
176
				}
177
				catch (MessagingException ex) {
178
					ex.printStackTrace();
179
				}
13787 manish.sha 180
			} catch (IOException e) {
181
				e.printStackTrace();
182
				try{
183
					mailer.sendSSLMessage(errorSendTo, "Error While Flipkart Pricing Updation Item Id: "+ entry.getItem_id(), "Due to IO Exception in Flipkart Pricing Panel", emailFromAddress, password,new ArrayList<File>());
184
				}
185
				catch (MessagingException ex) {
186
					ex.printStackTrace();
187
				}
13761 manish.sha 188
			}
189
			/*jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
10049 vikram.rag 190
					"\","+"\"attributeValues\""+":"+
13757 manish.sha 191
					"{\"selling_price\""+":"+"\""+ new Double(entry.getSellingPrice()).intValue()+"\""+"},"+"\"listingValidations\":" +
192
			        "{\"PRICE_ERROR_CHECK\":" +
193
			        "\"disable\"}"+"}");
10197 vikram.rag 194
 
13766 manish.sha 195
 
10211 vikram.rag 196
			i++;
10212 vikram.rag 197
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size()) ){
198
				if(i%10!=0){
199
					jsonRequest.append(",");
200
				}
201
 
10049 vikram.rag 202
			}
10201 vikram.rag 203
			System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());
10049 vikram.rag 204
			if(i%10==0 || i==flipkartPricingItems.size()){
10201 vikram.rag 205
				System.out.println("Inside send code block");
10049 vikram.rag 206
				StringEntity input = null;
207
				try {
208
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
209
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
210
					jsonRequest = new StringBuffer();
211
				} catch (UnsupportedEncodingException e) {
212
					e.printStackTrace();
213
				}
214
				input.setContentType("application/json");
215
 
216
				httppost.setEntity(input);
217
				HttpResponse response = null;
218
				try {
219
					System.out.println("Before send " );
220
					response = httpclient.execute(httppost);
221
					System.out.println("After send " );
222
				} catch (IOException e) {
223
					e.printStackTrace();
224
				}
225
				BufferedReader rd = null;
226
				try {
227
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
228
				} catch (IllegalStateException e) {
229
					e.printStackTrace();
230
				} catch (IOException e) {
231
					e.printStackTrace();
232
				}
13756 manish.sha 233
				feedResponse.append("");
10049 vikram.rag 234
				String line;
235
				try {
236
					while ((line = rd.readLine()) != null) {
237
						System.out.println("Response " + line);
238
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
239
						}
240
						else{
241
							feedResponse.append(line);
242
						}
243
 
244
					}
245
				} catch (IOException e) {
246
					e.printStackTrace();
247
				}
13761 manish.sha 248
			}*/
10049 vikram.rag 249
		}
250
		System.out.println("Before sending email");
251
		String text = tableHeader+pricingItems.toString()+tableFooter;
252
		try {
10196 vikram.rag 253
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 254
		} catch (MessagingException e) {
255
			e.printStackTrace();
256
		}
10196 vikram.rag 257
		Client catalogClient = null;
13761 manish.sha 258
 
259
		StringBuffer feedResponse = new StringBuffer();
260
		for(Long itemId: feedResponseMap.keySet()){
261
			boolean updated = feedResponseMap.get(itemId);
262
			if(updated){
13789 manish.sha 263
				feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status:  Successful\n");
13761 manish.sha 264
				updatedItems.add(itemId);
265
			}else{
13789 manish.sha 266
				feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status: Failed\n");
10204 vikram.rag 267
			}
13761 manish.sha 268
 
10204 vikram.rag 269
		}
13761 manish.sha 270
		if(feedResponse.toString().length()>0){
271
 
10204 vikram.rag 272
			try {
13761 manish.sha 273
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing Updation Response ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
274
				if(updatedItems.size()>0){
10196 vikram.rag 275
					try {
13761 manish.sha 276
						catalogClient = new CatalogClient().getClient();
277
						catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
278
					} catch (TException e) {
279
						try {
280
							new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
281
						} catch (TTransportException e1) {
282
							e1.printStackTrace();
283
						} catch (TException e1) {
284
							e1.printStackTrace();
285
						}
286
						e.printStackTrace();
10196 vikram.rag 287
					}
288
				}
10197 vikram.rag 289
 
10049 vikram.rag 290
			} catch (MessagingException e) {
10196 vikram.rag 291
				try {
10197 vikram.rag 292
					catalogClient = new CatalogClient().getClient();
293
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 294
				} catch (TException ex) {
295
					try {
296
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
297
					} catch (TTransportException e1) {
298
						e1.printStackTrace();
299
					} catch (TException e1) {
300
						e1.printStackTrace();
301
					}
302
					ex.printStackTrace();
303
				}
10049 vikram.rag 304
				e.printStackTrace();
305
			}
306
		}
307
	}
308
 
309
	static String getProductName(Item item){
310
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
311
 
312
	}
313
	static String getName(String name){
314
		if(name==null || name.length()==0){
315
			return "";
316
		}
317
		else{
318
			return name;
319
		}
320
	}
321
}