Subversion Repositories SmartDukaan

Rev

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