Subversion Repositories SmartDukaan

Rev

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