Subversion Repositories SmartDukaan

Rev

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