Subversion Repositories SmartDukaan

Rev

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