Subversion Repositories SmartDukaan

Rev

Rev 10213 | Rev 11444 | 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();
10202 vikram.rag 34
	private static List<MarketPlaceItemPrice> flipkartPricingItems = new ArrayList<MarketPlaceItemPrice>();
10049 vikram.rag 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[];
10202 vikram.rag 43
	private static List<MarketPlaceItemPrice> flipkartPricingUpdateItems;
10049 vikram.rag 44
	static
45
	{
46
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
47
		emailFromAddress = "build@shop2020.in";
48
		password = "cafe@nes";
49
		mailer = new GmailUtils();
10213 vikram.rag 50
		//sendTo = new String[]{"vikram.raghav@shop2020.in"};
51
		sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
10049 vikram.rag 52
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
10213 vikram.rag 53
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};
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();
10202 vikram.rag 62
			flipkartPricingUpdateItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
63
			for(MarketPlaceItemPrice flipkartPricingUpdateItem:flipkartPricingUpdateItems){
64
				if(flipkartPricingUpdateItem.getLastUpdatedOn() > flipkartPricingUpdateItem.getLastUpdatedOnMarketplace() && !flipkartPricingUpdateItem.isIsPriceOverride() && flipkartPricingUpdateItem.isIsListedOnSource() && flipkartPricingUpdateItem.getSellingPrice()!=0){
65
					flipkartPricingItems.add(flipkartPricingUpdateItem);
66
				}
67
			}
10199 vikram.rag 68
			List<FlipkartItem> flipkartItems = null;
69
			boolean retry = true;
70
			while(retry){
71
				try{
72
					flipkartItems = catalogServiceClient.getAllFlipkartItems();
73
					retry = false;
74
				}
75
				catch(Exception e){
76
					catalogServiceClient = new CatalogClient().getClient();
77
				}
78
			}
10049 vikram.rag 79
			for(FlipkartItem flipkartItem:flipkartItems){
80
				flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
81
			}
10200 vikram.rag 82
			retry = true;
83
			List<Item> aliveItems = null;
84
			while(retry){
85
				try{
86
					aliveItems = catalogServiceClient.getAllAliveItems();
87
					retry = false;
88
				}
89
				catch(Exception e){
90
					catalogServiceClient = new CatalogClient().getClient();
91
				}
92
			}
10197 vikram.rag 93
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
94
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
95
			}
10049 vikram.rag 96
 
10197 vikram.rag 97
		} catch (TException e) {
98
			e.printStackTrace();
99
		}
100
		if(flipkartPricingItems.size()==0){
101
			String text = "";
102
			try {
103
				mailer.sendSSLMessage(sendTo,"No Change in Flipkart item Prices ", emailFromAddress, password, text);
104
				System.exit(0);
105
			} catch (MessagingException e) {
106
				e.printStackTrace();
107
			}
108
		}
109
 
10049 vikram.rag 110
	}
111
 
112
	public static void main(String[] args) throws ClientProtocolException, TException{
113
		DefaultHttpClient httpclient = new DefaultHttpClient();
114
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
10206 vikram.rag 115
		//httpclient.getCredentialsProvider().setCredentials(
116
		//		new AuthScope("sandbox-api.flipkart.net", 443),
117
		//		new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
10148 vikram.rag 118
		httpclient.getCredentialsProvider().setCredentials(
10206 vikram.rag 119
				new AuthScope("api.flipkart.net", 443),
120
				new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
121
		HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
122
		//HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
10049 vikram.rag 123
		StringBuffer jsonRequest = new StringBuffer();
124
		StringBuffer jsonStart = new StringBuffer();
125
		jsonStart = jsonStart.append("{\"listings\":[");
126
		StringBuffer jsonEnd = new StringBuffer();
127
		jsonEnd.append("]}");
128
		//System.out.println("JSON request  " + jsonRequest);
129
		int i=0;
130
		StringBuffer pricingItems =new StringBuffer();
131
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
132
			+ "<td><b>" + "Product Name" +"</b></td>"
133
			+"<td><b>" + "Item ID" + "</b></td>"
134
			+"<td><b>" + "Selling Price" + "</b></td>"
135
			+"</tr>";
136
		String tableFooter = "</table></html>";
137
		Item item;
138
		StringBuffer feedResponse = null;
139
		for(MarketPlaceItemPrice entry:flipkartPricingItems){
10196 vikram.rag 140
			updatedItems.add(entry.getItem_id());
10049 vikram.rag 141
			feedResponse=  new StringBuffer();
11152 vikram.rag 142
			if(aliveItemsMap.containsKey(entry.getItem_id())){
143
				item = aliveItemsMap.get(entry.getItem_id());	
144
			}
145
			else{
146
				continue;
147
			}
10049 vikram.rag 148
			System.out.println("Item ID  " + entry.getItem_id());
149
			System.out.println("Product Name " + getProductName(item));
150
			System.out.println("Selling Price " + entry.getSellingPrice());
151
			jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
152
					"\","+"\"attributeValues\""+":"+
10205 vikram.rag 153
					"{\"selling_price\""+":"+"\""+ new Double(entry.getSellingPrice()).intValue()+"\""+"}}");
10197 vikram.rag 154
 
10049 vikram.rag 155
			pricingItems.append("<tr>" 
156
					+ "<td>" +getProductName(item)+"</td>"
157
					+"<td>" + item.getId() + "</td>"
158
					+"<td>" + entry.getSellingPrice() + "</td>"
159
					+"</tr>");
10211 vikram.rag 160
			i++;
10212 vikram.rag 161
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size()) ){
162
				if(i%10!=0){
163
					jsonRequest.append(",");
164
				}
165
 
10049 vikram.rag 166
			}
10201 vikram.rag 167
			System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());
10049 vikram.rag 168
			if(i%10==0 || i==flipkartPricingItems.size()){
10201 vikram.rag 169
				System.out.println("Inside send code block");
10049 vikram.rag 170
				StringEntity input = null;
171
				try {
172
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
173
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
174
					jsonRequest = new StringBuffer();
175
				} catch (UnsupportedEncodingException e) {
176
					e.printStackTrace();
177
				}
178
				input.setContentType("application/json");
179
 
180
				httppost.setEntity(input);
181
				HttpResponse response = null;
182
				try {
183
					System.out.println("Before send " );
184
					response = httpclient.execute(httppost);
185
					System.out.println("After send " );
186
				} catch (IOException e) {
187
					e.printStackTrace();
188
				}
189
				BufferedReader rd = null;
190
				try {
191
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
192
				} catch (IllegalStateException e) {
193
					e.printStackTrace();
194
				} catch (IOException e) {
195
					e.printStackTrace();
196
				}
197
				String line;
198
				try {
199
					while ((line = rd.readLine()) != null) {
200
						System.out.println("Response " + line);
201
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
202
						}
203
						else{
204
							feedResponse.append(line);
205
						}
206
 
207
					}
208
				} catch (IOException e) {
209
					e.printStackTrace();
210
				}
211
			}
212
		}
213
		System.out.println("Before sending email");
214
		String text = tableHeader+pricingItems.toString()+tableFooter;
215
		try {
10196 vikram.rag 216
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 217
		} catch (MessagingException e) {
218
			e.printStackTrace();
219
		}
10196 vikram.rag 220
		Client catalogClient = null;
10049 vikram.rag 221
		if(feedResponse.toString().length()>0){
222
			try {
10196 vikram.rag 223
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10204 vikram.rag 224
			} catch (MessagingException e) {
225
				e.printStackTrace();
226
			}
227
		}
228
		else{
229
			try {
230
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10196 vikram.rag 231
				try {
10197 vikram.rag 232
					catalogClient = new CatalogClient().getClient();
233
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 234
				} catch (TException e) {
235
					try {
236
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
237
					} catch (TTransportException e1) {
238
						e1.printStackTrace();
239
					} catch (TException e1) {
240
						e1.printStackTrace();
241
					}
242
					e.printStackTrace();
243
				}
10197 vikram.rag 244
 
10049 vikram.rag 245
			} catch (MessagingException e) {
10196 vikram.rag 246
				try {
10197 vikram.rag 247
					catalogClient = new CatalogClient().getClient();
248
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 249
				} catch (TException ex) {
250
					try {
251
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
252
					} catch (TTransportException e1) {
253
						e1.printStackTrace();
254
					} catch (TException e1) {
255
						e1.printStackTrace();
256
					}
257
					ex.printStackTrace();
258
				}
10049 vikram.rag 259
				e.printStackTrace();
260
			}
261
		}
262
	}
263
 
264
	static String getProductName(Item item){
265
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
266
 
267
	}
268
	static String getName(String name){
269
		if(name==null || name.length()==0){
270
			return "";
271
		}
272
		else{
273
			return name;
274
		}
275
	}
276
}