Subversion Repositories SmartDukaan

Rev

Rev 10307 | Go to most recent revision | Details | 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());
74
			 System.out.println("Json input " + jsonRequest.toString());
75
		 } catch (UnsupportedEncodingException e) {
76
			 e.printStackTrace();
77
		 }
78
		 input.setContentType("application/json");
79
		 httppost.setEntity(input);
80
		 HttpResponse response = null;
81
		 try {
82
			 System.out.println("Before send " );
83
			 response = httpclient.execute(httppost);
84
			 System.out.println("After send " );
85
		 } catch (IOException e) {
86
			 e.printStackTrace();
87
		 }
88
		 BufferedReader rd = null;
89
		 try {
90
			 rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
91
		 } catch (IllegalStateException e) {
92
			 e.printStackTrace();
93
		 } catch (IOException e) {
94
			 e.printStackTrace();
95
		 }
96
		 String line;
97
		 boolean updated = false;
98
		 try {
99
			while ((line = rd.readLine()) != null) {
100
				 System.out.println("Response " + line);
101
				 if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
102
					 updated = true;
103
				 }
104
			 }
105
		} catch (IOException e2) {
106
			e2.printStackTrace();
107
		}
108
		 GmailUtils mailer = new GmailUtils();
109
		 String text = "Product : " +getProductName(this.item) +"\n"+ 
110
		 "Item ID : " +this.item.getId() +"\n"+
111
		 "SKU at Flipkart : " +this.fksku +"\n"+
112
		 "Updated Price : " +this.price;
113
		 if(updated){
114
			 ArrayList<Long> updateList = new ArrayList<Long>();
115
			 try {
116
				 Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
117
 
118
				 updateList.add(this.item.getId());
119
				 catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,8);
120
			 } catch (TException e) {
121
				 try {
122
					 new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
123
				 } catch (TTransportException e1) {
124
					 e1.printStackTrace();
125
					 logger.info("Exception" + e1);
126
				 } catch (TException e1) {
127
					 e1.printStackTrace();
128
					 logger.info("Exception" + e1);
129
				 }
130
				 logger.info("Exception" + e);
131
			 }
132
			 try{
133
				 mailer.sendSSLMessage(sendTo, "Price updated on Flipkart ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
134
			 }
135
			 catch(Exception e){
136
				 logger.info("Exception"+e);
137
			 }
138
		 }
139
		 else{
140
			 try{
141
				 mailer.sendSSLMessage(sendTo, "Failed to update Price on Flipkart  ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
142
			 }
143
			 catch(Exception e){
144
				 logger.info("Exception"+e);
145
			 }
146
 
147
		 }
148
		 return 1;
149
	 }
150
 
151
	 public void run()
152
	 {
153
		 this.updatePricing(this.price,this.fksku,this.item,timestamp);
154
 
155
	 }
156
	 String getProductName(Item item){
157
		 return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
158
 
159
	 }
160
	 String getName(String name){
161
		 if(name==null || name.length()==0){
162
			 return "";
163
		 }
164
		 else{
165
			 return name;
166
		 }
167
	 }
168
 
169
 
170
}