Subversion Repositories SmartDukaan

Rev

Rev 12830 | Rev 12836 | 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);
12832 kshitij.so 116
				if(line.equalsIgnoreCase("{\"status\":\"succes\"}")){
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 {
12832 kshitij.so 131
		        updated = fkPricingPannel.updatePrice(this.fksku, String.valueOf(this.price));
12830 kshitij.so 132
                logger.info("Value of updated" +updated);
12827 kshitij.so 133
            } catch (Exception e) {
134
                logger.info("Exception" + e);
135
            }
136
		}
11445 vikram.rag 137
		if(updated){
138
			ArrayList<Long> updateList = new ArrayList<Long>();
139
			try {
140
				Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
10268 vikram.rag 141
 
11445 vikram.rag 142
				updateList.add(this.item.getId());
143
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,8);
144
			} catch (TException e) {
145
				try {
146
					new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
147
				} catch (TTransportException e1) {
148
					e1.printStackTrace();
149
					logger.info("Exception" + e1);
150
				} catch (TException e1) {
151
					e1.printStackTrace();
152
					logger.info("Exception" + e1);
153
				}
154
				logger.info("Exception" + e);
155
			}
156
			try{
157
				mailer.sendSSLMessage(sendTo, "Price updated on Flipkart ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
158
			}
159
			catch(Exception e){
160
				logger.info("Exception"+e);
161
			}
162
		}
163
		else{
164
			try{
165
				mailer.sendSSLMessage(sendTo, "Failed to update Price on Flipkart  ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
166
			}
167
			catch(Exception e){
168
				logger.info("Exception"+e);
169
			}
10268 vikram.rag 170
 
11445 vikram.rag 171
		}
172
		return 1;
173
	}
10268 vikram.rag 174
 
11445 vikram.rag 175
	public void run()
176
	{
177
		try {
10316 kshitij.so 178
			this.updatePricing(this.price,this.fksku,this.item,timestamp);
179
		} catch (JSONException e) {
180
			// TODO Auto-generated catch block
181
			logger.error("Exception while updating prices",e);
182
		}
10268 vikram.rag 183
 
11445 vikram.rag 184
	}
185
	String getProductName(Item item){
186
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10268 vikram.rag 187
 
11445 vikram.rag 188
	}
189
	String getName(String name){
190
		if(name==null || name.length()==0){
191
			return "";
192
		}
193
		else{
194
			return name;
195
		}
196
	}
10268 vikram.rag 197
 
198
 
199
}