Subversion Repositories SmartDukaan

Rev

Rev 10708 | Rev 11446 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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