Subversion Repositories SmartDukaan

Rev

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