Subversion Repositories SmartDukaan

Rev

Rev 10319 | Rev 11445 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10268 vikram.rag 1
package in.shop2020.support.utils;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.thrift.clients.CatalogClient;
7
import in.shop2020.utils.GmailUtils;
8
 
9
import java.io.BufferedReader;
10
import java.io.File;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
import java.io.UnsupportedEncodingException;
14
import java.util.ArrayList;
15
import org.apache.http.HttpResponse;
16
import org.apache.http.auth.AuthScope;
17
import org.apache.http.auth.UsernamePasswordCredentials;
18
import org.apache.http.client.ClientProtocolException;
19
import org.apache.http.client.methods.HttpPost;
20
import org.apache.http.conn.ClientConnectionManager;
21
import org.apache.http.entity.StringEntity;
22
import org.apache.http.impl.client.DefaultHttpClient;
23
import org.apache.http.impl.conn.PoolingClientConnectionManager;
24
import org.apache.thrift.TException;
25
import org.apache.thrift.transport.TTransportException;
10316 kshitij.so 26
import org.json.JSONException;
27
import org.json.JSONObject;
10268 vikram.rag 28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30
 
31
public class UpdateFlipkartPricing extends Thread{
32
	private Float price;
33
	private String fksku;
34
	private Item item;
35
	private Long timestamp;
10708 kshitij.so 36
	//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
10268 vikram.rag 37
	private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
10708 kshitij.so 38
	public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
10268 vikram.rag 39
			"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
40
			"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};
10708 kshitij.so 41
	 public String emailFromAddress = "build@shop2020.in";
10268 vikram.rag 42
	 public String password = "cafe@nes";
43
	 public UpdateFlipkartPricing(Float price,String fksku,Item item,Long timestamp){
44
		 logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " SKU at flipkart :"+fksku +" Item ID:" +item.getId());
45
		 this.price = price;
46
		 this.fksku = fksku;
47
		 this.item = item;
48
		 this.timestamp = timestamp; 
49
	 }
50
	 public UpdateFlipkartPricing() {
51
	 }
52
	 public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
53
		 Item item = new CatalogClient().getClient().getItem(2231);
54
		 UpdateFlipkartPricing updateFlipkartPricing = new UpdateFlipkartPricing(22200f,"1108903",item,System.currentTimeMillis());
55
		 logger.info("Calling Thread to update price at snapdeal");
56
		 updateFlipkartPricing.start();	
57
 
58
	 }
10316 kshitij.so 59
	 int updatePricing(Float price,String fksku,Item item,Long timestamp) throws JSONException{
10268 vikram.rag 60
		 logger.info("Calling Update Flipkart Price Method --" + " Price :" +price + " Supc :"+fksku +" Item ID:" +item.getId());
61
		 ClientConnectionManager connManager = new PoolingClientConnectionManager();
62
		 DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
63
		 httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
64
		 httpclient.getCredentialsProvider().setCredentials(
65
				 new AuthScope("api.flipkart.net", 443),
66
				 new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4"));
67
		 HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/"+fksku+"/listings");
10319 kshitij.so 68
		 //StringBuffer jsonRequest = new StringBuffer();
10316 kshitij.so 69
		 JSONObject outer = new JSONObject();
70
    	 JSONObject inner = new JSONObject();
71
    	 outer.put("skuId",fksku);
10319 kshitij.so 72
    	 inner.put("selling_price", String.valueOf(new Double(price).intValue()));
10316 kshitij.so 73
    	 outer.put("attributeValues", inner);
74
		 /*jsonRequest.append("{\"skuId\":"+"\""+fksku+
10268 vikram.rag 75
				 "\","+"\"attributeValues\""+":"+
76
				 "{\"selling_price\""+":"+"\""+price+
10316 kshitij.so 77
		 "\"}");*/
10268 vikram.rag 78
		 StringEntity input = null;
79
		 try {
10316 kshitij.so 80
			 input = new StringEntity(outer.toString());
10318 kshitij.so 81
			 logger.info("Json input " + outer.toString());
10268 vikram.rag 82
		 } catch (UnsupportedEncodingException e) {
10307 kshitij.so 83
			 logger.error("Unable to create request",e);
10268 vikram.rag 84
		 }
85
		 input.setContentType("application/json");
86
		 httppost.setEntity(input);
87
		 HttpResponse response = null;
88
		 try {
10307 kshitij.so 89
			 logger.info("Trying to post");
10268 vikram.rag 90
			 response = httpclient.execute(httppost);
91
		 } catch (IOException e) {
10307 kshitij.so 92
			 logger.error("Unable to post request",e);
10268 vikram.rag 93
		 }
94
		 BufferedReader rd = null;
95
		 try {
96
			 rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
97
		 } catch (IllegalStateException e) {
98
			 e.printStackTrace();
99
		 } catch (IOException e) {
100
			 e.printStackTrace();
101
		 }
102
		 String line;
103
		 boolean updated = false;
104
		 try {
105
			while ((line = rd.readLine()) != null) {
10314 kshitij.so 106
				 logger.info("Response " + line);
10268 vikram.rag 107
				 if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
108
					 updated = true;
109
				 }
110
			 }
111
		} catch (IOException e2) {
112
			e2.printStackTrace();
113
		}
114
		 GmailUtils mailer = new GmailUtils();
115
		 String text = "Product : " +getProductName(this.item) +"\n"+ 
116
		 "Item ID : " +this.item.getId() +"\n"+
117
		 "SKU at Flipkart : " +this.fksku +"\n"+
118
		 "Updated Price : " +this.price;
119
		 if(updated){
120
			 ArrayList<Long> updateList = new ArrayList<Long>();
121
			 try {
122
				 Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
123
 
124
				 updateList.add(this.item.getId());
125
				 catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,8);
126
			 } catch (TException e) {
127
				 try {
128
					 new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
129
				 } catch (TTransportException e1) {
130
					 e1.printStackTrace();
131
					 logger.info("Exception" + e1);
132
				 } catch (TException e1) {
133
					 e1.printStackTrace();
134
					 logger.info("Exception" + e1);
135
				 }
136
				 logger.info("Exception" + e);
137
			 }
138
			 try{
139
				 mailer.sendSSLMessage(sendTo, "Price updated on Flipkart ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
140
			 }
141
			 catch(Exception e){
142
				 logger.info("Exception"+e);
143
			 }
144
		 }
145
		 else{
146
			 try{
147
				 mailer.sendSSLMessage(sendTo, "Failed to update Price on Flipkart  ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
148
			 }
149
			 catch(Exception e){
150
				 logger.info("Exception"+e);
151
			 }
152
 
153
		 }
154
		 return 1;
155
	 }
156
 
157
	 public void run()
158
	 {
10316 kshitij.so 159
		 try {
160
			this.updatePricing(this.price,this.fksku,this.item,timestamp);
161
		} catch (JSONException e) {
162
			// TODO Auto-generated catch block
163
			logger.error("Exception while updating prices",e);
164
		}
10268 vikram.rag 165
 
166
	 }
167
	 String getProductName(Item item){
168
		 return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
169
 
170
	 }
171
	 String getName(String name){
172
		 if(name==null || name.length()==0){
173
			 return "";
174
		 }
175
		 else{
176
			 return name;
177
		 }
178
	 }
179
 
180
 
181
}