Subversion Repositories SmartDukaan

Rev

Rev 9977 | Rev 9984 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9977 Rev 9983
Line 17... Line 17...
17
import in.shop2020.serving.model.OrderItems;
17
import in.shop2020.serving.model.OrderItems;
18
import in.shop2020.thrift.clients.CatalogClient;
18
import in.shop2020.thrift.clients.CatalogClient;
19
import in.shop2020.thrift.clients.InventoryClient;
19
import in.shop2020.thrift.clients.InventoryClient;
20
import in.shop2020.thrift.clients.LogisticsClient;
20
import in.shop2020.thrift.clients.LogisticsClient;
21
import in.shop2020.thrift.clients.TransactionClient;
21
import in.shop2020.thrift.clients.TransactionClient;
-
 
22
import in.shop2020.utils.GmailUtils;
22
import inventory.FlipkartInventoryItem;
23
import inventory.FlipkartInventoryItem;
23
 
24
 
24
import java.io.BufferedReader;
25
import java.io.BufferedReader;
-
 
26
import java.io.File;
25
import java.io.IOException;
27
import java.io.IOException;
26
import java.io.InputStreamReader;
28
import java.io.InputStreamReader;
27
import java.io.UnsupportedEncodingException;
29
import java.io.UnsupportedEncodingException;
28
import java.util.ArrayList;
30
import java.util.ArrayList;
29
import java.util.HashMap;
31
import java.util.HashMap;
30
import java.util.List;
32
import java.util.List;
31
import java.util.Map;
33
import java.util.Map;
32
import java.util.Map.Entry;
34
import java.util.Map.Entry;
33
 
35
 
-
 
36
import javax.mail.MessagingException;
-
 
37
 
34
import org.apache.http.HttpResponse;
38
import org.apache.http.HttpResponse;
35
import org.apache.http.NameValuePair;
39
import org.apache.http.NameValuePair;
36
import org.apache.http.auth.AuthScope;
40
import org.apache.http.auth.AuthScope;
37
import org.apache.http.auth.UsernamePasswordCredentials;
41
import org.apache.http.auth.UsernamePasswordCredentials;
38
import org.apache.http.client.ClientProtocolException;
42
import org.apache.http.client.ClientProtocolException;
Line 53... Line 57...
53
public class CreateSubmitFlipkartInventoryFeed {
57
public class CreateSubmitFlipkartInventoryFeed {
54
	private static Map<Long, FlipkartInventoryItem> flipkartItemsInventoryMap = new HashMap<Long, FlipkartInventoryItem>();
58
	private static Map<Long, FlipkartInventoryItem> flipkartItemsInventoryMap = new HashMap<Long, FlipkartInventoryItem>();
55
	private static List<FlipkartItem> flipkartItems;
59
	private static List<FlipkartItem> flipkartItems;
56
	private static List<FlipkartItem> flipkartInventoryItems = new ArrayList<FlipkartItem>();
60
	private static List<FlipkartItem> flipkartInventoryItems = new ArrayList<FlipkartItem>();
57
	private static Map<Long,Long> itemIdOrdersMap = new HashMap<Long,Long>();
61
	private static Map<Long,Long> itemIdOrdersMap = new HashMap<Long,Long>();
-
 
62
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
-
 
63
	static java.text.SimpleDateFormat sdf;
-
 
64
	static String emailFromAddress;
-
 
65
	static String password;
-
 
66
	static GmailUtils mailer;
-
 
67
	static String sendTo[];
58
	static
68
	static
59
	{
69
	{
-
 
70
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
-
 
71
		emailFromAddress = "build@shop2020.in";
-
 
72
		password = "cafe@nes";
-
 
73
		mailer = new GmailUtils();
-
 
74
		sendTo = new String[]{"vikram.raghav@shop2020.in"};
-
 
75
		/*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
-
 
76
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
-
 
77
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
60
		List<String> flipkartorderids = null;
78
		 */List<String> flipkartorderids = null;
61
		Client catalogServiceClient = null;
79
		 Client catalogServiceClient = null;
62
		try {
80
		 try {
63
			catalogServiceClient = new CatalogClient().getClient();
81
			 catalogServiceClient = new CatalogClient().getClient();
64
		} catch (TTransportException e) {
82
		 } catch (TTransportException e) {
65
			e.printStackTrace();
83
			 e.printStackTrace();
66
		}
84
		 }
67
		try {
85
		 try {
68
			flipkartItems = catalogServiceClient.getAllFlipkartItems();
86
			 flipkartItems = catalogServiceClient.getAllFlipkartItems();
69
		} catch (TException e) {
87
		 } catch (TException e) {
70
			e.printStackTrace();
88
			 e.printStackTrace();
71
		}
89
		 }
72
		for(FlipkartItem flipkartItem:flipkartItems){
90
		 for(FlipkartItem flipkartItem:flipkartItems){
73
			if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed())
91
			 if(flipkartItem.isIsListedOnFlipkart() && !flipkartItem.isSuppressInventoryFeed())
74
				flipkartInventoryItems.add(flipkartItem);
92
				 flipkartInventoryItems.add(flipkartItem);
75
		}
93
		 }
-
 
94
		 if(flipkartInventoryItems.size()==0){
-
 
95
			 String text = "";
-
 
96
				try {
-
 
97
					mailer.sendSSLMessage(sendTo,"No items to send inventory ", emailFromAddress, password, text);
-
 
98
					System.exit(0);
-
 
99
				} catch (MessagingException e) {
-
 
100
					e.printStackTrace();
-
 
101
				}
-
 
102
		 }
76
		in.shop2020.model.v1.order.TransactionService.Client transactionClient;
103
		 in.shop2020.model.v1.order.TransactionService.Client transactionClient;
77
		try {
104
		 try {
78
			flipkartorderids = FetchOrders();
105
			 flipkartorderids = FetchOrders();
79
		} catch (ClientProtocolException e1) {
106
		 } catch (ClientProtocolException e1) {
80
			e1.printStackTrace();
107
			 e1.printStackTrace();
81
		} catch (IOException e1) {
108
		 } catch (IOException e1) {
82
			e1.printStackTrace();
109
			 e1.printStackTrace();
83
		}
110
		 }
84
		try {
111
		 try {
85
			transactionClient = new TransactionClient("support_transaction_service_server_host","transaction_service_server_port").getClient();
112
			 transactionClient = new TransactionClient("support_transaction_service_server_host","transaction_service_server_port").getClient();
86
			if(flipkartorderids!=null && flipkartorderids.size()>0){
113
			 if(flipkartorderids!=null && flipkartorderids.size()>0){
87
				itemIdOrdersMap = transactionClient.getCreatedOrdersForFlipkart(flipkartorderids);
114
				 itemIdOrdersMap = transactionClient.getCreatedOrdersForFlipkart(flipkartorderids);
88
			}
115
			 }
89
		} catch (TTransportException e) {
116
		 } catch (TTransportException e) {
90
			e.printStackTrace();
117
			 e.printStackTrace();
91
		}catch (TException e) {
118
		 }catch (TException e) {
92
			// TODO Auto-generated catch block
119
			 // TODO Auto-generated catch block
93
			e.printStackTrace();
120
			 e.printStackTrace();
94
		}
121
		 }
95
 
122
 
96
	}
123
	}
97
 
124
 
98
	public static void main(String[] args) throws ClientProtocolException, TException{
125
	public static void main(String[] args) throws ClientProtocolException, TException{
99
		calculateinventory();
126
		calculateinventory();
100
		DefaultHttpClient httpclient = new DefaultHttpClient();
127
		DefaultHttpClient httpclient = new DefaultHttpClient();
101
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
128
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
102
		//httpclient.getCredentialsProvider().setCredentials(
-
 
103
		//		new AuthScope("sandbox-api.flipkart.net", 443),
-
 
104
		//		new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
-
 
105
		httpclient.getCredentialsProvider().setCredentials(
129
		httpclient.getCredentialsProvider().setCredentials(
-
 
130
				new AuthScope("sandbox-api.flipkart.net", 443),
-
 
131
				new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
-
 
132
		/*httpclient.getCredentialsProvider().setCredentials(
106
				new AuthScope("api.flipkart.net", 443),
133
				new AuthScope("api.flipkart.net", 443),
107
				new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
134
				new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
-
 
135
		*/
108
		HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk");
136
		//HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
-
 
137
		HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
109
		StringBuffer jsonRequest = new StringBuffer();
138
		StringBuffer jsonRequest = new StringBuffer();
-
 
139
		StringBuffer jsonStart = new StringBuffer();
110
		jsonRequest.append("{\"listings\":[");
140
		jsonStart = jsonStart.append("{\"listings\":[");
-
 
141
		StringBuffer jsonEnd = new StringBuffer();
-
 
142
		jsonEnd.append("]}");
111
		//System.out.println("JSON request  " + jsonRequest);
143
		//System.out.println("JSON request  " + jsonRequest);
112
		LogisticsClient logisticsServiceClient;
144
		LogisticsClient logisticsServiceClient;
113
		in.shop2020.logistics.LogisticsService.Client logisticsClient=null;
145
		in.shop2020.logistics.LogisticsService.Client logisticsClient=null;
114
		LogisticsInfo logisticinfo = null;
146
		LogisticsInfo logisticinfo = null;
115
		try {
147
		try {
Line 120... Line 152...
120
			// TODO Auto-generated catch block
152
			// TODO Auto-generated catch block
121
			e.printStackTrace();
153
			e.printStackTrace();
122
		}
154
		}
123
		long stock=0;
155
		long stock=0;
124
		int i=0;
156
		int i=0;
125
		long available,reserve,heldforsource,totalheld,allocable,holdinventory,defaultinventory;
157
		long available,reserve,heldforsource,totalheld,allocable,holdinventory,defaultinventory,actualstock=0,orders=0;
-
 
158
		StringBuffer inventoryItems =new StringBuffer();
-
 
159
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
-
 
160
			+ "<td><b>" + "Product Name" +"</b></td>"
-
 
161
			+"<td><b>" + "Item ID" + "</b></td>"
-
 
162
			+"<td><b>" + "Inventory" + "</b></td>"
-
 
163
			+"<td><b>" + "Orders Added to Inventory" + "</b></td>"
-
 
164
			+"<td><b>" + "Sent Inventory" + "</b></td>"
-
 
165
			+"</tr>";
-
 
166
		String tableFooter = "</table></html>";
-
 
167
		Item item;
-
 
168
		StringBuffer feedResponse = null;
126
		for(Entry<Long, FlipkartInventoryItem> entry:flipkartItemsInventoryMap.entrySet()){
169
		for(Entry<Long, FlipkartInventoryItem> entry:flipkartItemsInventoryMap.entrySet()){
-
 
170
			feedResponse=  new StringBuffer();
127
			available = entry.getValue().getAvailability();
171
			available = entry.getValue().getAvailability();
128
			reserve = entry.getValue().getReserve();
172
			reserve = entry.getValue().getReserve();
129
			heldforsource = entry.getValue().getHeldForSource();
173
			heldforsource = entry.getValue().getHeldForSource();
130
			totalheld = entry.getValue().getTotalHeldInventory();
174
			totalheld = entry.getValue().getTotalHeldInventory();
131
			holdinventory = entry.getValue().getHoldInventory();
175
			holdinventory = entry.getValue().getHoldInventory();
Line 156... Line 200...
156
						stock = defaultinventory;
200
						stock = defaultinventory;
157
					}
201
					}
158
				}
202
				}
159
				System.out.println("Item ID " + entry.getKey()+" Stock "+stock +" Orders " + itemIdOrdersMap.get(entry.getKey()));
203
				System.out.println("Item ID " + entry.getKey()+" Stock "+stock +" Orders " + itemIdOrdersMap.get(entry.getKey()));
160
				if(itemIdOrdersMap!=null && itemIdOrdersMap.containsKey(entry.getKey())){
204
				if(itemIdOrdersMap!=null && itemIdOrdersMap.containsKey(entry.getKey())){
-
 
205
					actualstock = stock;
161
					stock = stock + itemIdOrdersMap.get(entry.getKey());  
206
					stock = stock + itemIdOrdersMap.get(entry.getKey());  
-
 
207
					orders = itemIdOrdersMap.get(entry.getKey());
162
				}
208
				}
163
				if(stock<0){
209
				if(stock<0){
164
					stock =0;
210
					stock =0;
165
				}
211
				}
166
				else{
212
				else{
Line 181... Line 227...
181
			}
227
			}
182
			jsonRequest.append("{\"skuId\":"+"\""+entry.getValue().getSkuAtflipkart()+
228
			jsonRequest.append("{\"skuId\":"+"\""+entry.getValue().getSkuAtflipkart()+
183
					"\","+"\"attributeValues\""+":"+
229
					"\","+"\"attributeValues\""+":"+
184
					"{\"procurement_sla\""+":"+"\""+logisticinfo.getShippingTime()+
230
					"{\"procurement_sla\""+":"+"\""+logisticinfo.getShippingTime()+
185
					"\","+"\"stock_count\""+":"+"\""+stock+"\""+"}}");
231
					"\","+"\"stock_count\""+":"+"\""+stock+"\""+"}}");
-
 
232
			item = aliveItemsMap.get(entry.getValue().getId());
-
 
233
			inventoryItems.append("<tr>" 
-
 
234
					+ "<td>" +getProductName(item)+"</td>"
-
 
235
					+"<td>" + item.getId() + "</td>"
-
 
236
					+"<td>" + actualstock + "</td>"
-
 
237
					+"<td>" + orders + "</td>"
-
 
238
					+"<td>" + stock + "</td>"
-
 
239
					+"</tr>");
186
			if(flipkartItemsInventoryMap.entrySet().size()!=1 && (i!=flipkartItemsInventoryMap.entrySet().size() -1 ) ){
240
			if(flipkartItemsInventoryMap.entrySet().size()!=1 && (i!=flipkartItemsInventoryMap.entrySet().size() -1 ) ){
187
				jsonRequest.append(",");	
241
				jsonRequest.append(",");	
188
			}
242
			}
189
			i++;
243
			i++;
190
			if(i%10==0 || i==flipkartItemsInventoryMap.entrySet().size()){
244
			if(i%10==0 || i==flipkartItemsInventoryMap.entrySet().size()){
191
				jsonRequest.append("]}");
-
 
192
				StringEntity input = null;
245
				StringEntity input = null;
193
				try {
246
				try {
194
					input = new StringEntity(jsonRequest.toString());
247
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
-
 
248
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
-
 
249
					jsonRequest = new StringBuffer();
195
				} catch (UnsupportedEncodingException e) {
250
				} catch (UnsupportedEncodingException e) {
196
					// TODO Auto-generated catch block
251
					// TODO Auto-generated catch block
197
					e.printStackTrace();
252
					e.printStackTrace();
198
				}
253
				}
199
				input.setContentType("application/json");
254
				input.setContentType("application/json");
200
				System.out.println("Json input " + jsonRequest.toString());
-
 
-
 
255
				
201
				httppost.setEntity(input);
256
				httppost.setEntity(input);
202
				HttpResponse response = null;
257
				HttpResponse response = null;
203
				try {
258
				try {
204
					System.out.println("Before send " );
259
					System.out.println("Before send " );
205
					response = httpclient.execute(httppost);
260
					response = httpclient.execute(httppost);
Line 220... Line 275...
220
				}
275
				}
221
				String line;
276
				String line;
222
				try {
277
				try {
223
					while ((line = rd.readLine()) != null) {
278
					while ((line = rd.readLine()) != null) {
224
						System.out.println("Response " + line);
279
						System.out.println("Response " + line);
-
 
280
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
-
 
281
						}
-
 
282
						else{
-
 
283
							feedResponse.append(line);
-
 
284
						}
-
 
285
						
225
					}
286
					}
226
				} catch (IOException e) {
287
				} catch (IOException e) {
227
					// TODO Auto-generated catch block
288
					// TODO Auto-generated catch block
228
					e.printStackTrace();
289
					e.printStackTrace();
229
				}
290
				}
230
			}
291
			}
231
		}
292
		}
-
 
293
		System.out.println("Before sending email");
-
 
294
		String text = tableHeader+inventoryItems.toString()+tableFooter;
-
 
295
		try {
-
 
296
			mailer.sendSSLMessage(sendTo,"Flipkart Inventory Sent Details ", emailFromAddress, password, text);
-
 
297
		} catch (MessagingException e) {
-
 
298
			e.printStackTrace();
-
 
299
		}
-
 
300
		if(feedResponse.toString().length()>0){
-
 
301
			try {
-
 
302
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
-
 
303
			} catch (MessagingException e) {
-
 
304
				e.printStackTrace();
-
 
305
			}
-
 
306
		}
-
 
307
		else{
-
 
308
			try {
-
 
309
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
-
 
310
			} catch (MessagingException e) {
-
 
311
				e.printStackTrace();
-
 
312
			}
-
 
313
		}
232
	}
314
	}
-
 
315
 
233
	private static void calculateinventory(){
316
	private static void calculateinventory(){
234
		CatalogClient catalogServiceClient = null;
317
		CatalogClient catalogServiceClient = null;
235
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
318
		Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
236
		try {
319
		try {
237
			catalogServiceClient = new CatalogClient();
320
			catalogServiceClient = new CatalogClient();
Line 240... Line 323...
240
			e.printStackTrace();
323
			e.printStackTrace();
241
		}
324
		}
242
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
325
		in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
243
		try {
326
		try {
244
			List<Item> aliveItems = catalogClient.getAllAliveItems();
327
			List<Item> aliveItems = catalogClient.getAllAliveItems();
245
			Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>(); 
-
 
246
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
328
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
247
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
329
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
248
 
330
 
249
			}
331
			}
250
			InventoryClient inventoryServiceClient = new InventoryClient();
332
			InventoryClient inventoryServiceClient = new InventoryClient();
Line 527... Line 609...
527
					+ order.getStatusLabel() + " " + order.getPickup_by_date() + " "+ order.getTrackingId());
609
					+ order.getStatusLabel() + " " + order.getPickup_by_date() + " "+ order.getTrackingId());
528
			orderIdList.add(order.getExternalId());
610
			orderIdList.add(order.getExternalId());
529
		}
611
		}
530
		return orderIdList; 
612
		return orderIdList; 
531
	}
613
	}
-
 
614
	static String getProductName(Item item){
-
 
615
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
-
 
616
 
-
 
617
	}
-
 
618
	static String getName(String name){
-
 
619
		if(name==null || name.length()==0){
-
 
620
			return "";
-
 
621
		}
-
 
622
		else{
-
 
623
			return name;
-
 
624
		}
-
 
625
	}
532
}
626
}