| 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.json.JSONException;
|
|
|
25 |
import org.json.JSONObject;
|
|
|
26 |
|
|
|
27 |
import com.google.gson.Gson;
|
|
|
28 |
import com.hotspotstore.model.SolrResultPojo;
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
public class SearchController extends BaseController{
|
|
|
32 |
|
|
|
33 |
private static Logger log = Logger.getLogger(SearchController.class);
|
|
|
34 |
|
|
|
35 |
private String id;
|
|
|
36 |
private SolrResultPojo ssr;
|
|
|
37 |
private long windowSize = 24;
|
|
|
38 |
private String q;
|
|
|
39 |
|
|
|
40 |
public String getQ() {
|
|
|
41 |
return q;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public void setQ(String q) {
|
|
|
45 |
this.q = q;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
private Map<String, List<List<String>>> crumbs;
|
|
|
49 |
public static final Map<String, String> FACET_LABEL_MAP = Collections.unmodifiableMap(
|
|
|
50 |
new HashMap<String, String>(){
|
|
|
51 |
/**
|
|
|
52 |
*
|
|
|
53 |
*/
|
|
|
54 |
private static final long serialVersionUID = 1L;
|
|
|
55 |
|
|
|
56 |
{
|
|
|
57 |
put("F_50010","Category");
|
|
|
58 |
put("F_50011","Sub Category");
|
|
|
59 |
put("F_50002","Price");
|
|
|
60 |
put("F_50001","Brand");
|
|
|
61 |
put("F_50006","Data Connectivity");
|
|
|
62 |
put("F_50007","Camera Resolution");
|
|
|
63 |
put("F_50039","");
|
|
|
64 |
put("F_50012","Display");
|
|
|
65 |
put("F_50013","Operating System");
|
|
|
66 |
put("F_50014","RAM");
|
|
|
67 |
put("F_50015","Storage Capacity");
|
|
|
68 |
put("F_50017","Processor");
|
|
|
69 |
put("F_50018","Capacity");
|
|
|
70 |
put("F_50019","Class");
|
|
|
71 |
put("F_50020","Capacity");
|
|
|
72 |
put("F_50021","Capacity");
|
|
|
73 |
put("F_50022","Type");
|
|
|
74 |
put("F_50023","Interface");
|
|
|
75 |
//Resolution for Composite
|
|
|
76 |
put("F_50024","Resolution");
|
|
|
77 |
put("F_50025","Optical Zoom");
|
|
|
78 |
put("F_50026","Display Size");
|
|
|
79 |
//Resolution for DSLR
|
|
|
80 |
put("F_50027","Resolution");
|
|
|
81 |
put("F_50028","Availability");
|
|
|
82 |
put("F_50031","Operating System");
|
|
|
83 |
put("F_50032","Screen Size"); //Screen size for mobiles
|
|
|
84 |
put("F_50033","Screen Size"); //Screen size for laptop
|
|
|
85 |
put("F_50034","Screen Size"); //Screen size for tablets
|
|
|
86 |
put("F_50035","Operating System"); //Screen size for tablets
|
|
|
87 |
put("F_50036","Camera Resolution"); //Screen size for tablets
|
|
|
88 |
put("F_50037","Voice Call Facility"); //Screen size for tablets
|
|
|
89 |
put("F_50038","Mobile Type"); //Mobile Type
|
|
|
90 |
put("F_50028","Availability");
|
|
|
91 |
}
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
public Map<String, List<List<String>>> getCrumbs() {
|
|
|
96 |
return crumbs;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public void setCrumbs(Map<String, List<List<String>>> crumbs) {
|
|
|
100 |
this.crumbs = crumbs;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public long getWindowSize() {
|
|
|
104 |
return windowSize;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public void setWindowSize(long windowSize) {
|
|
|
108 |
this.windowSize = windowSize;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public long getPage() {
|
|
|
112 |
return page;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public void setPage(long page) {
|
|
|
116 |
this.page = page;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public long getBeginIndex(){
|
|
|
120 |
if(totalResults>0)
|
|
|
121 |
return beginIndex+1;
|
|
|
122 |
return beginIndex;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public long getTotalPages() {
|
|
|
126 |
return 1 + (totalResults-1)/windowSize;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public void setBeginIndex(long beginIndex) {
|
|
|
130 |
this.beginIndex = beginIndex;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
private long page = 1;
|
|
|
134 |
private long totalResults;
|
|
|
135 |
private long beginIndex = 0;
|
|
|
136 |
private String url;
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
public String getUrl() {
|
|
|
140 |
return url;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public void setUrl(String url) {
|
|
|
144 |
this.url = url;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public long getTotalResults() {
|
|
|
148 |
return totalResults;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
public void setTotalResults(long totalResults) {
|
|
|
152 |
this.totalResults = totalResults;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public long getCurrentPage() {
|
|
|
156 |
return this.page;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
private static String mobileApiUrl = "http://127.0.0.1:8080/mobileapi";
|
|
|
160 |
|
|
|
161 |
public String index() throws URISyntaxException, IOException, JSONException{
|
|
|
162 |
String[] fqrys = this.request.getParameterValues("fq");
|
|
|
163 |
String query = q;
|
|
|
164 |
int length = 1;
|
|
|
165 |
if(fqrys!= null){
|
|
|
166 |
length += fqrys.length;
|
|
|
167 |
}
|
|
|
168 |
url = "?";
|
|
|
169 |
|
|
|
170 |
String[] newfqrys = new String[length];
|
|
|
171 |
|
|
|
172 |
String urlCrumb = url;
|
|
|
173 |
|
|
|
174 |
this.crumbs = new HashMap<String, List<List<String>>>();
|
|
|
175 |
|
|
|
176 |
if(query != null) {
|
|
|
177 |
urlCrumb = url;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
if(fqrys != null) {
|
|
|
181 |
log.info("fqrys=" + Arrays.toString(fqrys));
|
|
|
182 |
String filterUrl;
|
|
|
183 |
for(int i=0; i<fqrys.length; i++){
|
|
|
184 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
185 |
newfqrys[i+1] = fqrys[i];
|
|
|
186 |
String facetName = StringUtils.split(fqrys[i], ":")[0];
|
|
|
187 |
String facetValue = StringUtils.split(fqrys[i], ":")[1];
|
|
|
188 |
String facetLabel = FACET_LABEL_MAP.get(facetName);
|
|
|
189 |
filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
|
|
|
190 |
List<String> acrumb = Arrays.asList(facetValue, filterUrl);
|
|
|
191 |
if(!crumbs.containsKey(facetLabel)) {
|
|
|
192 |
crumbs.put(facetLabel, new ArrayList<List<String>>());
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
this.crumbs.get(facetLabel).add(acrumb);
|
|
|
196 |
}
|
|
|
197 |
}
|
| 20566 |
kshitij.so |
198 |
newfqrys[0] ="H_00001:true";
|
| 20424 |
kshitij.so |
199 |
|
|
|
200 |
url = urlCrumb;
|
|
|
201 |
|
|
|
202 |
log.info("crumbs "+crumbs);
|
|
|
203 |
|
|
|
204 |
String category_url = mobileApiUrl+"/search/";
|
|
|
205 |
URIBuilder generalSearchUrl = new URIBuilder(category_url);
|
|
|
206 |
generalSearchUrl.addParameter("q", query);
|
|
|
207 |
for (String s : newfqrys){
|
|
|
208 |
if (s==null || s.isEmpty()){
|
|
|
209 |
continue;
|
|
|
210 |
}
|
|
|
211 |
generalSearchUrl.addParameter("fq", s.trim());
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if(this.request.getParameter("page") != null){
|
|
|
215 |
this.page = Long.parseLong(this.request.getParameter("page"));
|
|
|
216 |
this.beginIndex = this.windowSize * (this.page-1);
|
|
|
217 |
}
|
|
|
218 |
generalSearchUrl.addParameter("index", String.valueOf(beginIndex));
|
|
|
219 |
generalSearchUrl.addParameter("offset", String.valueOf(windowSize));
|
|
|
220 |
URL url = generalSearchUrl.build().toURL();
|
|
|
221 |
log.info("Search Url Search Results"+url.toString());
|
|
|
222 |
InputStream is = url.openStream();
|
|
|
223 |
String jsonString;
|
|
|
224 |
try{
|
|
|
225 |
jsonString = IOUtils.toString(is, "UTF-8");
|
|
|
226 |
}
|
|
|
227 |
finally{
|
|
|
228 |
is.close();
|
|
|
229 |
}
|
|
|
230 |
JSONObject jsonObj = new JSONObject(jsonString);
|
|
|
231 |
Gson gson = new Gson();
|
|
|
232 |
ssr = gson.fromJson(jsonObj.getString("result"), SolrResultPojo.class);
|
|
|
233 |
this.totalResults = ssr.getTotalCount();
|
|
|
234 |
log.info("ssr label "+ssr.getLabel());
|
|
|
235 |
return "index";
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
public String getEncodedCompontent(String input) throws UnsupportedEncodingException{
|
|
|
239 |
return URLEncoder.encode(input, "UTF-8");
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
|
|
|
243 |
public String getId() {
|
|
|
244 |
return id;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
public SolrResultPojo getSsr() {
|
|
|
248 |
return ssr;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
public void setSsr(SolrResultPojo ssr) {
|
|
|
252 |
this.ssr = ssr;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public void setId(String id) {
|
|
|
256 |
StringTokenizer tokenizer = new StringTokenizer(id,"-");
|
|
|
257 |
while(tokenizer.hasMoreTokens()){
|
|
|
258 |
this.id = tokenizer.nextToken();
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
}
|