Subversion Repositories SmartDukaan

Rev

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