Subversion Repositories SmartDukaan

Rev

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