Subversion Repositories SmartDukaan

Rev

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