| 317 |
ashish |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 517 |
rajveer |
6 |
import in.shop2020.model.v1.widgets.WidgetService.Client;
|
|
|
7 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
8 |
import in.shop2020.serving.pages.PageEnum;
|
| 536 |
rajveer |
9 |
import in.shop2020.serving.services.CategoryManager;
|
| 517 |
rajveer |
10 |
import in.shop2020.serving.services.PageLoaderHandler;
|
|
|
11 |
import in.shop2020.serving.services.SolrSearchService;
|
|
|
12 |
import in.shop2020.serving.utils.FileUtils;
|
|
|
13 |
import in.shop2020.thrift.clients.WidgetServiceClient;
|
|
|
14 |
import in.shop2020.util.Utils;
|
|
|
15 |
|
|
|
16 |
import java.io.File;
|
| 317 |
ashish |
17 |
import java.io.IOException;
|
| 517 |
rajveer |
18 |
import java.net.URLEncoder;
|
|
|
19 |
import java.util.ArrayList;
|
|
|
20 |
import java.util.Arrays;
|
|
|
21 |
import java.util.HashMap;
|
|
|
22 |
import java.util.LinkedHashMap;
|
|
|
23 |
import java.util.List;
|
|
|
24 |
import java.util.Map;
|
| 536 |
rajveer |
25 |
import java.util.StringTokenizer;
|
| 317 |
ashish |
26 |
|
| 517 |
rajveer |
27 |
import org.apache.commons.lang.StringUtils;
|
| 317 |
ashish |
28 |
import org.apache.juli.logging.Log;
|
|
|
29 |
import org.apache.juli.logging.LogFactory;
|
|
|
30 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
31 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
32 |
|
|
|
33 |
/**
|
| 517 |
rajveer |
34 |
* @author rajveer
|
| 317 |
ashish |
35 |
*
|
|
|
36 |
*/
|
|
|
37 |
public class CategoryController extends BaseController {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
private static Log log = LogFactory.getLog(CategoryController.class);
|
| 517 |
rajveer |
43 |
|
| 317 |
ashish |
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
*/
|
| 517 |
rajveer |
47 |
private Map<String,String> htmlSnippets;
|
|
|
48 |
private Map<String, String[]> results;
|
|
|
49 |
private Map<String, String> snippets;
|
|
|
50 |
private Map<String, List<String[]>> facets;
|
| 620 |
rajveer |
51 |
private List<String[]> crumbs;
|
| 517 |
rajveer |
52 |
/**
|
|
|
53 |
*
|
|
|
54 |
*/
|
| 317 |
ashish |
55 |
private String id;
|
|
|
56 |
|
| 536 |
rajveer |
57 |
private String categoryName;
|
|
|
58 |
|
| 517 |
rajveer |
59 |
private String query;
|
|
|
60 |
|
|
|
61 |
private Double minPrice;
|
|
|
62 |
private Double maxPrice;
|
|
|
63 |
|
| 545 |
rajveer |
64 |
|
|
|
65 |
private long windowSize = 20;
|
|
|
66 |
private long page = 1;
|
|
|
67 |
private long totalResults;
|
|
|
68 |
private long beginIndex = 0;
|
|
|
69 |
private String url;
|
|
|
70 |
private String priceUrl;
|
|
|
71 |
|
| 317 |
ashish |
72 |
/**
|
|
|
73 |
* GET /category/10004
|
|
|
74 |
*
|
|
|
75 |
*/
|
|
|
76 |
public HttpHeaders show() throws SecurityException, IOException {
|
|
|
77 |
log.info("id=" + id);
|
| 536 |
rajveer |
78 |
this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(Long.parseLong(id));
|
|
|
79 |
|
|
|
80 |
|
| 517 |
rajveer |
81 |
htmlSnippets = new HashMap<String, String>();
|
|
|
82 |
PageLoaderHandler pageLoader = new PageLoaderHandler();
|
|
|
83 |
|
| 555 |
chandransh |
84 |
htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
|
| 517 |
rajveer |
85 |
htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
|
|
|
86 |
htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
|
|
|
87 |
htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
|
|
|
88 |
htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
|
| 569 |
rajveer |
89 |
htmlSnippets.put("BROWSE_HISTORY", pageLoader.getBrowseHistoryHtml(userinfo.getUserId(), userinfo.isSessionId()));
|
| 517 |
rajveer |
90 |
htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
|
|
|
91 |
|
|
|
92 |
htmlSnippets.put("CATEGORY_HEADER", pageLoader.getCategoryHeaderSnippet());
|
|
|
93 |
htmlSnippets.put("MAIN_BANNER", "");
|
|
|
94 |
htmlSnippets.put("SIDE_BANNER", "");
|
|
|
95 |
htmlSnippets.put("TAB_BUTTONS", "");
|
| 545 |
rajveer |
96 |
|
| 317 |
ashish |
97 |
|
| 517 |
rajveer |
98 |
|
|
|
99 |
|
|
|
100 |
String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50006", "F_50007" };
|
|
|
101 |
|
|
|
102 |
String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
|
|
|
103 |
|
|
|
104 |
String[] fqrys = this.request.getParameterValues("fq");
|
| 545 |
rajveer |
105 |
|
|
|
106 |
url = "?";
|
| 536 |
rajveer |
107 |
|
| 545 |
rajveer |
108 |
|
|
|
109 |
|
| 517 |
rajveer |
110 |
query = "*";
|
|
|
111 |
|
| 536 |
rajveer |
112 |
int length = 1;
|
|
|
113 |
|
|
|
114 |
if(fqrys!= null){
|
|
|
115 |
length += fqrys.length;
|
|
|
116 |
}
|
|
|
117 |
|
| 620 |
rajveer |
118 |
|
| 536 |
rajveer |
119 |
String[] newfqrys = new String[length];
|
| 545 |
rajveer |
120 |
newfqrys[0] = "Category:"+categoryName;
|
|
|
121 |
String urlCrumb = url;
|
| 536 |
rajveer |
122 |
|
| 620 |
rajveer |
123 |
this.crumbs = new ArrayList<String[]>();
|
|
|
124 |
List<String> arrList = Arrays.asList(facetDefIDs);
|
| 536 |
rajveer |
125 |
for(int i=1; i<length; i++) {
|
|
|
126 |
newfqrys[i] = fqrys[i-1];
|
| 545 |
rajveer |
127 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
|
|
|
128 |
priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
|
| 620 |
rajveer |
129 |
String filterUrl = url;
|
|
|
130 |
String facetName = StringUtils.split(fqrys[i-1], ":")[0];
|
|
|
131 |
String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
|
|
|
132 |
String facetLabel = facetLabels[arrList.indexOf(facetName)];
|
|
|
133 |
for(int j=1; j<length; j++) {
|
|
|
134 |
if(i==j){
|
|
|
135 |
continue;
|
|
|
136 |
}
|
|
|
137 |
filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
|
|
|
138 |
}
|
|
|
139 |
String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
|
|
|
140 |
log.info("acrumb=" + Arrays.toString(acrumb));
|
|
|
141 |
this.crumbs.add(acrumb);
|
|
|
142 |
|
| 536 |
rajveer |
143 |
}
|
| 545 |
rajveer |
144 |
url = urlCrumb;
|
|
|
145 |
|
|
|
146 |
if(this.request.getParameter("page") != null){
|
|
|
147 |
this.page = Long.parseLong(this.request.getParameter("page"));
|
|
|
148 |
this.beginIndex = this.windowSize * (this.page-1);
|
|
|
149 |
}
|
|
|
150 |
if(this.request.getParameter("min-price") != null){
|
|
|
151 |
this.minPrice = (new Double(this.request.getParameter("min-price")));
|
|
|
152 |
url= url + "&min-price=" + this.request.getParameter("min-price");
|
|
|
153 |
}
|
|
|
154 |
if(this.request.getParameter("max-price") != null){
|
|
|
155 |
this.maxPrice = (new Double(this.request.getParameter("max-price")));
|
|
|
156 |
url= url + "&max-price=" + this.request.getParameter("max-price");
|
|
|
157 |
}
|
| 536 |
rajveer |
158 |
|
| 569 |
rajveer |
159 |
SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
|
| 517 |
rajveer |
160 |
this.results = new LinkedHashMap<String, String[]>();
|
|
|
161 |
this.results = search.getResultMap();
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
this.facets = new LinkedHashMap<String, List<String[]>>();
|
|
|
165 |
String qryString = this.request.getQueryString();
|
|
|
166 |
log.info("qryString=" + qryString);
|
|
|
167 |
|
|
|
168 |
for (int i=0; i<facetDefIDs.length; i++) {
|
|
|
169 |
String facetDefID = facetDefIDs[i];
|
|
|
170 |
String facetLabel = facetLabels[i];
|
|
|
171 |
HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
|
|
|
172 |
if(facetDetailMap==null)
|
|
|
173 |
continue;
|
|
|
174 |
List<String[]> values = new ArrayList<String[]>();
|
| 545 |
rajveer |
175 |
String drilldownURL = new String();
|
| 517 |
rajveer |
176 |
for(String facet: facetDetailMap.keySet()){
|
| 536 |
rajveer |
177 |
|
| 545 |
rajveer |
178 |
/*
|
| 536 |
rajveer |
179 |
if(qryString != null){
|
|
|
180 |
drilldownURL = qryString;
|
| 545 |
rajveer |
181 |
|
| 536 |
rajveer |
182 |
}
|
| 545 |
rajveer |
183 |
*/
|
|
|
184 |
drilldownURL = url;
|
| 536 |
rajveer |
185 |
|
| 545 |
rajveer |
186 |
|
|
|
187 |
drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" +
|
|
|
188 |
URLEncoder.encode(facet, "UTF-8");
|
| 517 |
rajveer |
189 |
String[] afacet = new String[] { facet,
|
|
|
190 |
facetDetailMap.get(facet).toString(), drilldownURL };
|
|
|
191 |
values.add(afacet);
|
| 545 |
rajveer |
192 |
|
| 517 |
rajveer |
193 |
}
|
|
|
194 |
|
|
|
195 |
this.facets.put(facetLabel, values);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
|
| 545 |
rajveer |
199 |
|
| 517 |
rajveer |
200 |
Map<String, Double> priceMap = search.getPriceStatsMap();
|
|
|
201 |
if(priceMap != null){
|
|
|
202 |
this.minPrice = priceMap.get("min");
|
|
|
203 |
this.maxPrice = priceMap.get("max");
|
|
|
204 |
}else{
|
|
|
205 |
this.minPrice = 0.0;
|
|
|
206 |
this.maxPrice = 0.0;
|
|
|
207 |
}
|
| 545 |
rajveer |
208 |
this.totalResults = search.getTotalResults();
|
| 317 |
ashish |
209 |
return new DefaultHttpHeaders("show");
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
*
|
|
|
214 |
* @param id
|
|
|
215 |
*/
|
|
|
216 |
public void setId(String id) {
|
| 536 |
rajveer |
217 |
StringTokenizer tokenizer = new StringTokenizer(id,"-");
|
|
|
218 |
while(tokenizer.hasMoreTokens()){
|
|
|
219 |
this.id = tokenizer.nextToken();
|
|
|
220 |
}
|
| 317 |
ashish |
221 |
}
|
|
|
222 |
|
|
|
223 |
/**
|
|
|
224 |
*
|
|
|
225 |
*/
|
|
|
226 |
public String getId() {
|
|
|
227 |
return this.id;
|
|
|
228 |
}
|
| 517 |
rajveer |
229 |
|
| 536 |
rajveer |
230 |
public String getCategoryName() {
|
|
|
231 |
return this.categoryName;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
|
| 517 |
rajveer |
235 |
public String getHeaderSnippet(){
|
|
|
236 |
return htmlSnippets.get("HEADER");
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
public String getMainMenuSnippet(){
|
|
|
240 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
public String getSearchBarSnippet(){
|
|
|
244 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
public String getCustomerServiceSnippet(){
|
|
|
248 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
public String getSearchHeaderSnippet(){
|
|
|
252 |
return htmlSnippets.get("SEARCH_HEADER");
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public String getSearchDetailsSnippet(){
|
|
|
256 |
return htmlSnippets.get("SEARCH_DETAILS");
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
public String getMyResearchSnippet(){
|
|
|
260 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
261 |
}
|
| 569 |
rajveer |
262 |
|
|
|
263 |
public String getBrowseHistorySnippet(){
|
|
|
264 |
return htmlSnippets.get("BROWSE_HISTORY");
|
|
|
265 |
}
|
|
|
266 |
|
| 517 |
rajveer |
267 |
public String getFooterSnippet(){
|
|
|
268 |
return htmlSnippets.get("FOOTER");
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public String getJsFileSnippet(){
|
|
|
272 |
return htmlSnippets.get("JS_FILES");
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
public String getCssFileSnippet(){
|
|
|
276 |
return htmlSnippets.get("CSS_FILES");
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
*
|
|
|
281 |
* @return
|
|
|
282 |
*/
|
|
|
283 |
public String getQuery() {
|
|
|
284 |
return this.query;
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
*
|
|
|
290 |
* @return
|
|
|
291 |
*/
|
|
|
292 |
public Map<String, String[]> getResults() {
|
|
|
293 |
return this.results;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
*
|
|
|
298 |
* @return
|
|
|
299 |
*/
|
|
|
300 |
public Map<String, List<String[]>> getFacets() {
|
|
|
301 |
return this.facets;
|
|
|
302 |
}
|
|
|
303 |
|
| 545 |
rajveer |
304 |
|
|
|
305 |
public long getTotalResults(){
|
|
|
306 |
return totalResults;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
public String getUrl(){
|
|
|
310 |
return this.url;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
public String getPriceUrl(){
|
|
|
314 |
return this.priceUrl;
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
public long getBeginIndex(){
|
|
|
318 |
if(totalResults>0)
|
|
|
319 |
return beginIndex+1;
|
|
|
320 |
return beginIndex;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
public long getTotalPages() {
|
|
|
324 |
return 1 + totalResults/windowSize;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
public long getCurrentPage() {
|
|
|
328 |
return this.page;
|
|
|
329 |
}
|
|
|
330 |
|
| 517 |
rajveer |
331 |
public Double getMinPrice() {
|
|
|
332 |
return this.minPrice;
|
|
|
333 |
}
|
|
|
334 |
|
|
|
335 |
public Double getMaxPrice() {
|
|
|
336 |
return this.maxPrice;
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
public Map<String, String> getSnippets() throws Exception {
|
|
|
340 |
if(results != null){
|
|
|
341 |
snippets = new HashMap<String, String>();
|
|
|
342 |
for(String docId: results.keySet()){
|
| 536 |
rajveer |
343 |
snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
|
| 517 |
rajveer |
344 |
}
|
|
|
345 |
}
|
|
|
346 |
return snippets;
|
|
|
347 |
}
|
|
|
348 |
|
| 317 |
ashish |
349 |
}
|