Subversion Repositories SmartDukaan

Rev

Rev 20566 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
20424 kshitij.so 1
package com.hotspotstore.controllers;
2
 
3
 
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.UnsupportedEncodingException;
7
import java.net.MalformedURLException;
8
import java.net.URISyntaxException;
9
import java.net.URL;
10
import java.net.URLEncoder;
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17
 
18
import java.util.StringTokenizer;
19
 
20
import org.apache.commons.io.IOUtils;
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.http.client.utils.URIBuilder;
23
import org.apache.log4j.Logger;
24
import org.apache.struts2.convention.annotation.Action;
25
import org.apache.struts2.convention.annotation.Actions;
26
import org.apache.struts2.convention.annotation.Result;
27
import org.apache.struts2.convention.annotation.Results;
28
import org.json.JSONException;
29
import org.json.JSONObject;
30
 
31
import com.google.gson.Gson;
32
import com.hotspotstore.model.SolrResultPojo;
33
 
34
@Results({
35
	@Result(name = "show", location = "category-show.vm"),
36
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
37
})
38
 
39
public class CategoryController extends BaseController{
40
 
41
	private static Logger log = Logger.getLogger(CategoryController.class);
42
 
43
	private String id;
44
	private SolrResultPojo ssr;
45
	private long windowSize = 24;
46
	private Map<String, List<List<String>>> crumbs;
47
	public static final Map<String, String> FACET_LABEL_MAP = Collections.unmodifiableMap(
48
			new HashMap<String, String>(){
49
				/**
50
				 * 
51
				 */
52
				private static final long serialVersionUID = 1L;
53
 
54
				{
55
					put("F_50010","Category");
56
					put("F_50011","Sub Category");
57
					put("F_50002","Price");
58
					put("F_50001","Brand");
59
					put("F_50006","Data Connectivity");
60
					put("F_50007","Camera Resolution");
61
					put("F_50039","");
62
					put("F_50012","Display");
63
					put("F_50013","Operating System");
64
					put("F_50014","RAM");
65
					put("F_50015","Storage Capacity");
66
					put("F_50017","Processor");
67
					put("F_50018","Capacity");
68
					put("F_50019","Class");
69
					put("F_50020","Capacity");
70
					put("F_50021","Capacity");
71
					put("F_50022","Type");
72
					put("F_50023","Interface");
73
					//Resolution for Composite
74
					put("F_50024","Resolution");
75
					put("F_50025","Optical Zoom");
76
					put("F_50026","Display Size");
77
					//Resolution for DSLR
78
					put("F_50027","Resolution");
79
					put("F_50028","Availability");
80
					put("F_50031","Operating System");
81
					put("F_50032","Screen Size");	//Screen size for mobiles
82
					put("F_50033","Screen Size");	//Screen size for laptop
83
					put("F_50034","Screen Size"); 	//Screen size for tablets
84
					put("F_50035","Operating System"); 	//Screen size for tablets
85
					put("F_50036","Camera Resolution"); 	//Screen size for tablets
86
					put("F_50037","Voice Call Facility"); 	//Screen size for tablets
87
					put("F_50038","Mobile Type"); 	//Mobile Type
88
					put("F_50028","Availability");
89
				}
90
		});
91
 
92
 
93
	public Map<String, List<List<String>>> getCrumbs() {
94
		return crumbs;
95
	}
96
 
97
	public void setCrumbs(Map<String, List<List<String>>> crumbs) {
98
		this.crumbs = crumbs;
99
	}
100
 
101
	public long getWindowSize() {
102
		return windowSize;
103
	}
104
 
105
	public void setWindowSize(long windowSize) {
106
		this.windowSize = windowSize;
107
	}
108
 
109
	public long getPage() {
110
		return page;
111
	}
112
 
113
	public void setPage(long page) {
114
		this.page = page;
115
	}
116
 
117
	public long getBeginIndex(){
118
		if(totalResults>0)
119
			return beginIndex+1;
120
		return beginIndex;
121
	}
122
 
123
	public long getTotalPages() {
124
		return 1 + (totalResults-1)/windowSize;
125
	}
126
 
127
	public void setBeginIndex(long beginIndex) {
128
		this.beginIndex = beginIndex;
129
	}
130
 
131
	private long page = 1;
132
	private long totalResults;
133
	private long beginIndex = 0;
134
	private String url;
135
 
136
 
137
	public String getUrl() {
138
		return url;
139
	}
140
 
141
	public void setUrl(String url) {
142
		this.url = url;
143
	}
144
 
145
	public long getTotalResults() {
146
		return totalResults;
147
	}
148
 
149
	public void setTotalResults(long totalResults) {
150
		this.totalResults = totalResults;
151
	}
152
 
153
	public long getCurrentPage() {
154
		return this.page;
155
	}
156
 
157
	private static String mobileApiUrl = "http://127.0.0.1:8080/mobileapi";
158
 
159
	@Actions({
160
		@Action("/all-mobile-phones"),
161
		@Action("/all-mobile-accessories"),
162
		@Action("/all-laptop-accessories"),
163
		@Action("/all-tablets"),
164
		@Action("/all-laptops"),
165
		@Action("/all-cameras"),
166
		@Action("/business-phones"),
167
		@Action("/mobile-phone"),
168
		@Action("/high-end-multimedia-phones"),
169
		@Action("/low-end-multimedia-phones"),
170
		@Action("/basic-phones"),
171
		@Action("/dslr-cameras"),
172
		@Action("/compact-cameras"),
173
 
174
		@Action("/bluetooth-headset"),
175
		@Action("/memory-card"),
176
		@Action("/battery"),
177
		@Action("/headset"),
178
		@Action("/charger"),
179
		@Action("/pen-drive"),
180
		@Action("/carrying-case"),
181
		@Action("/car-charger"),
182
		@Action("/screen-guard"),
183
 
184
		@Action("/face-plate"),
185
		@Action("/decal"),
186
		@Action("/data-cable"),
187
		@Action("/ear-buds"),
188
		@Action("/cleaning-kit"),
189
		@Action("/speaker"),
190
		@Action("/hard-disk-drive"),
191
		@Action("/external-hard-disks"),
192
		@Action("/headphones"),
193
		@Action("/portable-music-players"),
194
		@Action("/data-cards"),
195
		@Action("/printer-cartridges"),
196
		@Action("/power-banks"),
197
		@Action("/mouse-pads"),
198
		@Action("/mouse"),
199
		@Action("/laptop-bags"),
200
		@Action("/laptop-batteries"),
201
		@Action("/laptop-chargers"),
202
		@Action("/all-smart-watches"),
203
		@Action("/wi-fi-routers"),
204
		@Action("/aux-cable"),
205
		@Action("/category"),
206
		@Action("/vehicle-mounts"),
207
		@Action("/projectors"),
208
		@Action("/watch-strap"),
209
		@Action("/laptop-stand")
210
	})
211
 
212
	public String show() throws URISyntaxException, IOException, JSONException{
213
		long category_id = Long.parseLong(id);
214
		String[] fqrys = this.request.getParameterValues("fq");
215
		String query = "*";
216
		int length = 1;
217
		if(fqrys!= null){
218
			length += fqrys.length;
219
		}
220
		url = "?";
221
 
222
		String[] newfqrys = new String[length];
223
 
224
		String urlCrumb = url;
225
 
226
		this.crumbs = new HashMap<String, List<List<String>>>();
227
 
228
		if(query != null) {
229
			urlCrumb = url; 
230
		}
231
 
232
		if(fqrys != null) {
233
			log.info("fqrys=" + Arrays.toString(fqrys));
234
			String filterUrl;
235
			for(int i=0; i<fqrys.length; i++){
236
				urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
237
				newfqrys[i+1] = fqrys[i];
238
				String facetName = StringUtils.split(fqrys[i], ":")[0];
239
				String facetValue = StringUtils.split(fqrys[i], ":")[1];
240
				String facetLabel = FACET_LABEL_MAP.get(facetName);
241
				filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
242
				List<String> acrumb = Arrays.asList(facetValue, filterUrl);
243
				if(!crumbs.containsKey(facetLabel)) {
244
					crumbs.put(facetLabel, new ArrayList<List<String>>());
245
				}
246
 
247
				this.crumbs.get(facetLabel).add(acrumb);
248
			}
249
		}
20855 amit.gupta 250
		newfqrys[0] ="P_120537:Yes";
20424 kshitij.so 251
 
252
		url = urlCrumb;
253
 
254
		log.info("crumbs "+crumbs);
255
 
256
		String category_url = mobileApiUrl+"/category/"+category_id;
257
		URIBuilder generalSearchUrl = new URIBuilder(category_url);
258
		generalSearchUrl.addParameter("q", query);
259
		for (String s : newfqrys){
260
			if (s==null || s.isEmpty()){
261
				continue;
262
			}
263
			generalSearchUrl.addParameter("fq", s.trim());  
264
		}
265
 
266
		if(this.request.getParameter("page") != null){
267
			this.page = Long.parseLong(this.request.getParameter("page"));
268
			this.beginIndex = this.windowSize * (this.page-1);
269
		}
270
		generalSearchUrl.addParameter("index", String.valueOf(beginIndex));
271
		generalSearchUrl.addParameter("offset", String.valueOf(windowSize));
272
		URL url = generalSearchUrl.build().toURL();
273
		log.info("Search Url Search Results"+url.toString());
274
		InputStream is = url.openStream();
275
		String jsonString;
276
		try{
277
			jsonString = IOUtils.toString(is, "UTF-8");
278
		}
279
		finally{
280
			is.close();
281
		}
282
		JSONObject jsonObj = new JSONObject(jsonString);
283
		Gson gson = new Gson();
284
		ssr = gson.fromJson(jsonObj.getString("result"), SolrResultPojo.class);
285
		this.totalResults = ssr.getTotalCount();
286
		return "show";
287
	}
288
 
289
	public String getEncodedCompontent(String input) throws UnsupportedEncodingException{
290
		return URLEncoder.encode(input, "UTF-8");
291
	}
292
 
293
	public String getId() {
294
		return id;
295
	}
296
 
297
	public SolrResultPojo getSsr() {
298
		return ssr;
299
	}
300
 
301
	public void setSsr(SolrResultPojo ssr) {
302
		this.ssr = ssr;
303
	}
304
 
305
	public void setId(String id) {
306
		StringTokenizer tokenizer = new StringTokenizer(id,"-");
307
		while(tokenizer.hasMoreTokens()){
308
			this.id = tokenizer.nextToken();
309
		}
310
	}
311
 
312
}