Subversion Repositories SmartDukaan

Rev

Rev 15237 | Rev 15527 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15014 kshitij.so 1
package in.shop2020.dtrapi.controllers;
2
 
3
import java.io.IOException;
4
import java.net.URI;
5
import java.net.URISyntaxException;
15027 kshitij.so 6
import java.util.HashMap;
15014 kshitij.so 7
import java.util.List;
8
 
15027 kshitij.so 9
import org.apache.axis.encoding.Base64;
15014 kshitij.so 10
import org.apache.http.NameValuePair;
11
import org.apache.http.client.utils.URLEncodedUtils;
12
import org.apache.log4j.Logger;
13
import org.json.JSONArray;
14
import org.json.JSONException;
15
import org.jsoup.Jsoup;
16
import org.jsoup.nodes.Document;
17
 
15027 kshitij.so 18
import com.whalin.MemCached.MemCachedClient;
19
import in.shop2020.dtrapi.services.MemCache;
15183 kshitij.so 20
import in.shop2020.dtrapi.services.UserMessagePojo;
15027 kshitij.so 21
 
15018 kshitij.so 22
public class SnapdealProductPageParserController extends BaseController{
15014 kshitij.so 23
 
15237 kshitij.so 24
	private static Logger log = Logger.getLogger(Class.class);
25
	private static final long serialVersionUID = 1L;
15014 kshitij.so 26
 
15237 kshitij.so 27
	private String url;
28
	private String supc = "";
29
	private String productUrl = "";
30
	private String color = "";
31
	private static final String INDEX = "index";
32
	private static final String KEY = "snapdealSupcToColorMapping";
33
	private static MemCache memCache = new MemCache();
34
	private static MemCachedClient memcachedClient = memCache.getClient();
15014 kshitij.so 35
 
15237 kshitij.so 36
 
37
 
38
	public UserMessagePojo getColorMessage() throws IOException, JSONException, URISyntaxException{
39
		try{
40
			parseUrl();
41
			if (!checkCache()){
42
				Document doc = Jsoup.connect(productUrl).get();
43
				doc.outputSettings().charset("UTF-8");
44
				JSONArray jsonArray = new JSONArray(doc.getElementById("productAttributesJson").attr("value"));
45
				for (int element=0; element<jsonArray.length();element++){
46
					if (!color.isEmpty()){
47
						break;
48
					}
49
					try{
50
						JSONArray subAttributeArray = jsonArray.getJSONObject(element).getJSONArray("subAttributes");
51
						for (int innerElement=0; innerElement< subAttributeArray.length(); innerElement++){
52
							if (supc.equalsIgnoreCase(subAttributeArray.getJSONObject(innerElement).getString("supc"))){
53
								color = jsonArray.getJSONObject(element).getString("value");
54
							}
55
						}
56
					}
57
					catch(Exception e1){
58
						e1.printStackTrace();
59
					}
60
					if (supc.equalsIgnoreCase(jsonArray.getJSONObject(element).getString("supc"))){
61
						color = jsonArray.getJSONObject(element).getString("value");
62
					}
63
				}
64
			}
65
		}
66
		catch (Exception e){
67
			log.error("Error while getting product details " +e);
68
		}
69
		return getMsg();
70
	}
71
 
72
	private UserMessagePojo getMsg(){
73
		UserMessagePojo ump = new UserMessagePojo();
74
		if (color.isEmpty()){
75
			ump.setResult(false);
76
			ump.setMessage("");
77
		}
78
		else{
79
			ump.setResult(true);
15272 kshitij.so 80
			ump.setMessage("Please select "+color+" color to get this price.");
15237 kshitij.so 81
		}
15183 kshitij.so 82
		return ump;
15014 kshitij.so 83
 
15237 kshitij.so 84
	}
15014 kshitij.so 85
 
15237 kshitij.so 86
	private String getJavaScriptCode(){
87
		String jsCode = "javascript:try{var ele=document.getElementById('attribute-select-0');ele.value ="+"'"+color+"'"+";ele.onchange();}catch(error){Android.onError(error.message);}";
88
		return jsCode;
89
	}
15027 kshitij.so 90
 
15237 kshitij.so 91
	private void parseUrl() throws URISyntaxException{
92
		List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
93
		for (NameValuePair param : params){
94
			if (param.getName().equalsIgnoreCase("supc")){
95
				supc = param.getValue();
96
			}
97
		}
98
		productUrl = new URI(url).getHost()+new URI(url).getPath();
99
		if (!productUrl.startsWith("http")){
100
			productUrl = "http://"+productUrl;
101
		}
102
	}
15014 kshitij.so 103
 
15237 kshitij.so 104
	private void populateCache(String supc, String color){
105
		System.out.println("populating cache");
106
		Object cacheValue = memcachedClient.get(KEY);
107
		HashMap<String, String> supcMap = new HashMap<String, String>();
108
		if (cacheValue != null) {
109
			supcMap=(HashMap<String, String>)cacheValue;
110
		}
111
		supcMap.put(supc, color);
112
		memcachedClient.set(KEY, supcMap);
113
	}
15014 kshitij.so 114
 
15237 kshitij.so 115
	private boolean checkCache(){
116
		if (supc!=null){
117
			Object cacheValue = memcachedClient.get(KEY);
118
			if (cacheValue != null) {
119
				System.out.println("cache value is not null");
120
				HashMap<String, String> supcMap = new HashMap<String, String>();
121
				supcMap=(HashMap<String, String>)cacheValue;
122
				System.out.println("supc map "+supcMap);
123
				color = supcMap.get(supc);
124
				System.out.println("color "+color);
125
				if (!(color==null || color.isEmpty())){
126
					return true;
127
				}
128
			}
129
		}
130
		return false;
131
	}
15014 kshitij.so 132
 
15237 kshitij.so 133
	public void setUrl(String url) {
134
		byte[] decoded = Base64.decode(url);
135
		this.url = new String(decoded);
136
	}
137
 
138
	public String getUrl() {
139
		return url;
140
	}
141
 
142
	public static void main(String[] args) throws URISyntaxException, IOException, JSONException{
143
		SnapdealProductPageParserController s = new SnapdealProductPageParserController();
144
		//String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS01MjYtZy82ODE1MDk5MjQyNDg/c3VwYz1TREw1MjU0MDI1OTYmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjEwODk3MA==";
145
		String url = "aHR0cDovL20uc25hcGRlYWwuY29tL3Byb2R1Y3QvaHRjLWRlc2lyZS02MjYtZy82NDAwMzA4NDA3MTU/c3VwYz1TREw5MzU5MzI1NzgmdXRtX3NvdXJjZT1hZmZfcHJvZyZ1dG1fY2FtcGFpZ249YWZ0cyZvZmZlcl9pZD0xNyZhZmZfaWQ9MzM1NTAmYWZmX3N1Yj1TSEEzMTQzMjExMzkxNQ==";
146
		byte[] decoded = Base64.decode(url);
147
		System.out.println(new String(decoded));
148
		s.setUrl(url);
149
		System.out.println(s.getColorMessage().getMessage());
150
	}
151
 
15014 kshitij.so 152
}