Subversion Repositories SmartDukaan

Rev

Rev 13761 | Rev 13787 | 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;
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",
11447 vikram.rag 60
				"yukti.jain@shop2020.in","manisha.sharma@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()+""));
172
			} catch (Exception e) {
173
				try {
174
					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>());
175
				}
176
				catch (MessagingException ex) {
177
					ex.printStackTrace();
178
				}
179
			}
180
			/*jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
10049 vikram.rag 181
					"\","+"\"attributeValues\""+":"+
13757 manish.sha 182
					"{\"selling_price\""+":"+"\""+ new Double(entry.getSellingPrice()).intValue()+"\""+"},"+"\"listingValidations\":" +
183
			        "{\"PRICE_ERROR_CHECK\":" +
184
			        "\"disable\"}"+"}");
10197 vikram.rag 185
 
13766 manish.sha 186
 
10211 vikram.rag 187
			i++;
10212 vikram.rag 188
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size()) ){
189
				if(i%10!=0){
190
					jsonRequest.append(",");
191
				}
192
 
10049 vikram.rag 193
			}
10201 vikram.rag 194
			System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());
10049 vikram.rag 195
			if(i%10==0 || i==flipkartPricingItems.size()){
10201 vikram.rag 196
				System.out.println("Inside send code block");
10049 vikram.rag 197
				StringEntity input = null;
198
				try {
199
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
200
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
201
					jsonRequest = new StringBuffer();
202
				} catch (UnsupportedEncodingException e) {
203
					e.printStackTrace();
204
				}
205
				input.setContentType("application/json");
206
 
207
				httppost.setEntity(input);
208
				HttpResponse response = null;
209
				try {
210
					System.out.println("Before send " );
211
					response = httpclient.execute(httppost);
212
					System.out.println("After send " );
213
				} catch (IOException e) {
214
					e.printStackTrace();
215
				}
216
				BufferedReader rd = null;
217
				try {
218
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
219
				} catch (IllegalStateException e) {
220
					e.printStackTrace();
221
				} catch (IOException e) {
222
					e.printStackTrace();
223
				}
13756 manish.sha 224
				feedResponse.append("");
10049 vikram.rag 225
				String line;
226
				try {
227
					while ((line = rd.readLine()) != null) {
228
						System.out.println("Response " + line);
229
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
230
						}
231
						else{
232
							feedResponse.append(line);
233
						}
234
 
235
					}
236
				} catch (IOException e) {
237
					e.printStackTrace();
238
				}
13761 manish.sha 239
			}*/
10049 vikram.rag 240
		}
241
		System.out.println("Before sending email");
242
		String text = tableHeader+pricingItems.toString()+tableFooter;
243
		try {
10196 vikram.rag 244
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 245
		} catch (MessagingException e) {
246
			e.printStackTrace();
247
		}
10196 vikram.rag 248
		Client catalogClient = null;
13761 manish.sha 249
 
250
		StringBuffer feedResponse = new StringBuffer();
251
		for(Long itemId: feedResponseMap.keySet()){
252
			boolean updated = feedResponseMap.get(itemId);
253
			if(updated){
254
				feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status:  Successful");
255
				updatedItems.add(itemId);
256
			}else{
257
				feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status: Failed");
10204 vikram.rag 258
			}
13761 manish.sha 259
 
10204 vikram.rag 260
		}
13761 manish.sha 261
		if(feedResponse.toString().length()>0){
262
 
10204 vikram.rag 263
			try {
13761 manish.sha 264
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing Updation Response ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
265
				if(updatedItems.size()>0){
10196 vikram.rag 266
					try {
13761 manish.sha 267
						catalogClient = new CatalogClient().getClient();
268
						catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
269
					} catch (TException e) {
270
						try {
271
							new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
272
						} catch (TTransportException e1) {
273
							e1.printStackTrace();
274
						} catch (TException e1) {
275
							e1.printStackTrace();
276
						}
277
						e.printStackTrace();
10196 vikram.rag 278
					}
279
				}
10197 vikram.rag 280
 
10049 vikram.rag 281
			} catch (MessagingException e) {
10196 vikram.rag 282
				try {
10197 vikram.rag 283
					catalogClient = new CatalogClient().getClient();
284
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 285
				} catch (TException ex) {
286
					try {
287
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
288
					} catch (TTransportException e1) {
289
						e1.printStackTrace();
290
					} catch (TException e1) {
291
						e1.printStackTrace();
292
					}
293
					ex.printStackTrace();
294
				}
10049 vikram.rag 295
				e.printStackTrace();
296
			}
297
		}
298
	}
299
 
300
	static String getProductName(Item item){
301
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
302
 
303
	}
304
	static String getName(String name){
305
		if(name==null || name.length()==0){
306
			return "";
307
		}
308
		else{
309
			return name;
310
		}
311
	}
312
}