Subversion Repositories SmartDukaan

Rev

Rev 10049 | Rev 10196 | 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.logistics.DeliveryType;
4
import in.shop2020.logistics.LogisticsInfo;
5
import in.shop2020.logistics.LogisticsServiceException;
6
import in.shop2020.model.v1.catalog.CatalogService.Client;
7
import in.shop2020.model.v1.catalog.FlipkartItem;
8
import in.shop2020.model.v1.catalog.Item;
9
import in.shop2020.model.v1.catalog.MarketPlaceItemPrice;
10
import in.shop2020.model.v1.catalog.status;
11
import in.shop2020.model.v1.inventory.InventoryType;
12
import in.shop2020.model.v1.inventory.ItemInventory;
13
import in.shop2020.model.v1.inventory.Warehouse;
14
import in.shop2020.model.v1.inventory.WarehouseLocation;
15
import in.shop2020.model.v1.inventory.WarehouseType;
16
import in.shop2020.model.v1.order.OrderSource;
17
import in.shop2020.serving.model.Order;
18
import in.shop2020.serving.model.OrderItems;
19
import in.shop2020.thrift.clients.CatalogClient;
20
import in.shop2020.thrift.clients.InventoryClient;
21
import in.shop2020.thrift.clients.LogisticsClient;
22
import in.shop2020.thrift.clients.TransactionClient;
23
import in.shop2020.utils.GmailUtils;
24
import inventory.FlipkartInventoryItem;
25
 
26
import java.io.BufferedReader;
27
import java.io.File;
28
import java.io.IOException;
29
import java.io.InputStreamReader;
30
import java.io.UnsupportedEncodingException;
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Map.Entry;
36
 
37
import javax.mail.MessagingException;
38
 
39
import org.apache.http.HttpResponse;
40
import org.apache.http.NameValuePair;
41
import org.apache.http.auth.AuthScope;
42
import org.apache.http.auth.UsernamePasswordCredentials;
43
import org.apache.http.client.ClientProtocolException;
44
import org.apache.http.client.HttpClient;
45
import org.apache.http.client.entity.UrlEncodedFormEntity;
46
import org.apache.http.client.methods.HttpGet;
47
import org.apache.http.client.methods.HttpPost;
48
import org.apache.http.entity.StringEntity;
49
import org.apache.http.impl.client.DefaultHttpClient;
50
import org.apache.http.message.BasicNameValuePair;
51
import org.apache.thrift.TException;
52
import org.apache.thrift.transport.TTransportException;
53
 
54
import com.google.gson.Gson;
55
 
56
 
57
 
58
public class CreateSubmitFlipkartPricingFeed {
59
	private static List<MarketPlaceItemPrice> flipkartPricingItems;
60
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
61
	private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
62
	static java.text.SimpleDateFormat sdf;
63
	static String emailFromAddress;
64
	static String password;
65
	static GmailUtils mailer;
66
	static String sendTo[];
67
	static
68
	{
69
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
70
		emailFromAddress = "build@shop2020.in";
71
		password = "cafe@nes";
72
		mailer = new GmailUtils();
73
		sendTo = new String[]{"vikram.raghav@shop2020.in"};
74
		/*sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
75
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
76
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
77
		 */
78
		 Client catalogServiceClient = null;
79
		 try {
80
			 catalogServiceClient = new CatalogClient().getClient();
81
		 } catch (TTransportException e) {
82
			 e.printStackTrace();
83
		 }
84
		 try {
85
			 flipkartPricingItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
86
			 List<Item> aliveItems = catalogServiceClient.getAllAliveItems();
87
				for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
88
					aliveItemsMap.put(thriftItem.getId(), thriftItem);
89
				}
90
			List<FlipkartItem> flipkartItems = catalogServiceClient.getAllFlipkartItems();
91
			for(FlipkartItem flipkartItem:flipkartItems){
92
				flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
93
			}
94
 
95
		 } catch (TException e) {
96
			 e.printStackTrace();
97
		 }
98
		 if(flipkartPricingItems.size()==0){
99
			 String text = "";
100
			 try {
101
				 mailer.sendSSLMessage(sendTo,"No items to send prices ", emailFromAddress, password, text);
102
				 System.exit(0);
103
			 } catch (MessagingException e) {
104
				 e.printStackTrace();
105
			 }
106
		 }
107
 
108
	}
109
 
110
	public static void main(String[] args) throws ClientProtocolException, TException{
111
		DefaultHttpClient httpclient = new DefaultHttpClient();
112
		httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
10148 vikram.rag 113
		httpclient.getCredentialsProvider().setCredentials(
10049 vikram.rag 114
				new AuthScope("sandbox-api.flipkart.net", 443),
115
				new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
10148 vikram.rag 116
		//httpclient.getCredentialsProvider().setCredentials(
117
		//		new AuthScope("api.flipkart.net", 443),
118
		//		new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
119
		//HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
120
		HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
10049 vikram.rag 121
		StringBuffer jsonRequest = new StringBuffer();
122
		StringBuffer jsonStart = new StringBuffer();
123
		jsonStart = jsonStart.append("{\"listings\":[");
124
		StringBuffer jsonEnd = new StringBuffer();
125
		jsonEnd.append("]}");
126
		//System.out.println("JSON request  " + jsonRequest);
127
		int i=0;
128
		StringBuffer pricingItems =new StringBuffer();
129
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
130
			+ "<td><b>" + "Product Name" +"</b></td>"
131
			+"<td><b>" + "Item ID" + "</b></td>"
132
			+"<td><b>" + "Selling Price" + "</b></td>"
133
			+"</tr>";
134
		String tableFooter = "</table></html>";
135
		Item item;
136
		StringBuffer feedResponse = null;
137
		for(MarketPlaceItemPrice entry:flipkartPricingItems){
138
			feedResponse=  new StringBuffer();
139
			item = aliveItemsMap.get(entry.getItem_id());
140
			System.out.println("Item ID  " + entry.getItem_id());
141
			System.out.println("Product Name " + getProductName(item));
142
			System.out.println("Selling Price " + entry.getSellingPrice());
143
			jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
144
					"\","+"\"attributeValues\""+":"+
145
					"{\"selling_price\""+":"+"\""+entry.getSellingPrice()+"}}");
146
 
147
			pricingItems.append("<tr>" 
148
					+ "<td>" +getProductName(item)+"</td>"
149
					+"<td>" + item.getId() + "</td>"
150
					+"<td>" + entry.getSellingPrice() + "</td>"
151
					+"</tr>");
152
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size() -1 ) ){
153
				jsonRequest.append(",");	
154
			}
155
			i++;
156
			if(i%10==0 || i==flipkartPricingItems.size()){
157
				StringEntity input = null;
158
				try {
159
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
160
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
161
					jsonRequest = new StringBuffer();
162
				} catch (UnsupportedEncodingException e) {
163
					// TODO Auto-generated catch block
164
					e.printStackTrace();
165
				}
166
				input.setContentType("application/json");
167
 
168
				httppost.setEntity(input);
169
				HttpResponse response = null;
170
				try {
171
					System.out.println("Before send " );
172
					response = httpclient.execute(httppost);
173
					System.out.println("After send " );
174
				} catch (IOException e) {
175
					// TODO Auto-generated catch block
176
					e.printStackTrace();
177
				}
178
				BufferedReader rd = null;
179
				try {
180
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
181
				} catch (IllegalStateException e) {
182
					// TODO Auto-generated catch block
183
					e.printStackTrace();
184
				} catch (IOException e) {
185
					// TODO Auto-generated catch block
186
					e.printStackTrace();
187
				}
188
				String line;
189
				try {
190
					while ((line = rd.readLine()) != null) {
191
						System.out.println("Response " + line);
192
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
193
						}
194
						else{
195
							feedResponse.append(line);
196
						}
197
 
198
					}
199
				} catch (IOException e) {
200
					// TODO Auto-generated catch block
201
					e.printStackTrace();
202
				}
203
			}
204
		}
205
		System.out.println("Before sending email");
206
		String text = tableHeader+pricingItems.toString()+tableFooter;
207
		try {
208
			mailer.sendSSLMessage(sendTo,"Flipkart Inventory Sent Details ", emailFromAddress, password, text);
209
		} catch (MessagingException e) {
210
			e.printStackTrace();
211
		}
212
		if(feedResponse.toString().length()>0){
213
			try {
214
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
215
			} catch (MessagingException e) {
216
				e.printStackTrace();
217
			}
218
		}
219
		else{
220
			try {
221
				mailer.sendSSLMessage(sendTo, "Flipkart Inventory updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
222
			} catch (MessagingException e) {
223
				e.printStackTrace();
224
			}
225
		}
226
	}
227
 
228
	static String getProductName(Item item){
229
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
230
 
231
	}
232
	static String getName(String name){
233
		if(name==null || name.length()==0){
234
			return "";
235
		}
236
		else{
237
			return name;
238
		}
239
	}
240
}