Subversion Repositories SmartDukaan

Rev

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