Subversion Repositories SmartDukaan

Rev

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