Subversion Repositories SmartDukaan

Rev

Rev 10200 | Rev 10202 | 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++;
10201 vikram.rag 157
			System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());
10049 vikram.rag 158
			if(i%10==0 || i==flipkartPricingItems.size()){
10201 vikram.rag 159
				System.out.println("Inside send code block");
10049 vikram.rag 160
				StringEntity input = null;
161
				try {
162
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
163
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
164
					jsonRequest = new StringBuffer();
165
				} catch (UnsupportedEncodingException e) {
166
					e.printStackTrace();
167
				}
168
				input.setContentType("application/json");
169
 
170
				httppost.setEntity(input);
171
				HttpResponse response = null;
172
				try {
173
					System.out.println("Before send " );
174
					response = httpclient.execute(httppost);
175
					System.out.println("After send " );
176
				} catch (IOException e) {
177
					e.printStackTrace();
178
				}
179
				BufferedReader rd = null;
180
				try {
181
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
182
				} catch (IllegalStateException e) {
183
					e.printStackTrace();
184
				} catch (IOException e) {
185
					e.printStackTrace();
186
				}
187
				String line;
188
				try {
189
					while ((line = rd.readLine()) != null) {
190
						System.out.println("Response " + line);
191
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
192
						}
193
						else{
194
							feedResponse.append(line);
195
						}
196
 
197
					}
198
				} catch (IOException e) {
199
					e.printStackTrace();
200
				}
201
			}
202
		}
203
		System.out.println("Before sending email");
204
		String text = tableHeader+pricingItems.toString()+tableFooter;
205
		try {
10196 vikram.rag 206
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 207
		} catch (MessagingException e) {
208
			e.printStackTrace();
209
		}
10196 vikram.rag 210
		Client catalogClient = null;
10049 vikram.rag 211
		if(feedResponse.toString().length()>0){
212
			try {
10196 vikram.rag 213
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
214
				try {
10197 vikram.rag 215
					catalogClient = new CatalogClient().getClient();
216
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 217
				} catch (TException e) {
218
					try {
219
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
220
					} catch (TTransportException e1) {
221
						e1.printStackTrace();
222
					} catch (TException e1) {
223
						e1.printStackTrace();
224
					}
225
					e.printStackTrace();
226
				}
10197 vikram.rag 227
 
10049 vikram.rag 228
			} catch (MessagingException e) {
10196 vikram.rag 229
				try {
10197 vikram.rag 230
					catalogClient = new CatalogClient().getClient();
231
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 232
				} catch (TException ex) {
233
					try {
234
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
235
					} catch (TTransportException e1) {
236
						e1.printStackTrace();
237
					} catch (TException e1) {
238
						e1.printStackTrace();
239
					}
240
					ex.printStackTrace();
241
				}
10049 vikram.rag 242
				e.printStackTrace();
243
			}
244
		}
245
		else{
246
			try {
10196 vikram.rag 247
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10049 vikram.rag 248
			} catch (MessagingException e) {
249
				e.printStackTrace();
250
			}
251
		}
252
	}
253
 
254
	static String getProductName(Item item){
255
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
256
 
257
	}
258
	static String getName(String name){
259
		if(name==null || name.length()==0){
260
			return "";
261
		}
262
		else{
263
			return name;
264
		}
265
	}
266
}