| 317 |
ashish |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 3561 |
rajveer |
6 |
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
|
|
|
7 |
import in.shop2020.serving.cache.SnippetCacheWrapper;
|
| 517 |
rajveer |
8 |
import in.shop2020.serving.services.SolrSearchService;
|
| 637 |
rajveer |
9 |
import in.shop2020.serving.utils.Utils;
|
| 2070 |
rajveer |
10 |
import in.shop2020.utils.CategoryManager;
|
| 517 |
rajveer |
11 |
|
| 317 |
ashish |
12 |
import java.io.IOException;
|
| 517 |
rajveer |
13 |
import java.net.URLEncoder;
|
|
|
14 |
import java.util.ArrayList;
|
|
|
15 |
import java.util.Arrays;
|
|
|
16 |
import java.util.HashMap;
|
|
|
17 |
import java.util.LinkedHashMap;
|
|
|
18 |
import java.util.List;
|
|
|
19 |
import java.util.Map;
|
| 2948 |
chandransh |
20 |
import java.util.Map.Entry;
|
| 536 |
rajveer |
21 |
import java.util.StringTokenizer;
|
| 317 |
ashish |
22 |
|
| 517 |
rajveer |
23 |
import org.apache.commons.lang.StringUtils;
|
| 832 |
rajveer |
24 |
import org.apache.log4j.Logger;
|
| 974 |
vikas |
25 |
import org.apache.struts2.convention.annotation.Action;
|
|
|
26 |
import org.apache.struts2.convention.annotation.Actions;
|
|
|
27 |
import org.apache.struts2.convention.annotation.Result;
|
| 317 |
ashish |
28 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
29 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
30 |
|
|
|
31 |
/**
|
| 517 |
rajveer |
32 |
* @author rajveer
|
| 317 |
ashish |
33 |
*
|
|
|
34 |
*/
|
| 974 |
vikas |
35 |
@Result(name = "show", location = "category-show.vm")
|
| 317 |
ashish |
36 |
public class CategoryController extends BaseController {
|
|
|
37 |
|
| 1044 |
chandransh |
38 |
private static final long serialVersionUID = 8477108528765030321L;
|
|
|
39 |
|
| 832 |
rajveer |
40 |
private static Logger log = Logger.getLogger(Class.class);
|
| 517 |
rajveer |
41 |
|
| 790 |
vikas |
42 |
private List<String> results;
|
| 517 |
rajveer |
43 |
private Map<String, String> snippets;
|
|
|
44 |
private Map<String, List<String[]>> facets;
|
| 620 |
rajveer |
45 |
private List<String[]> crumbs;
|
| 517 |
rajveer |
46 |
/**
|
|
|
47 |
*
|
|
|
48 |
*/
|
| 317 |
ashish |
49 |
private String id;
|
|
|
50 |
|
| 536 |
rajveer |
51 |
private String categoryName;
|
| 974 |
vikas |
52 |
private String categoryTitle;
|
| 1259 |
vikas |
53 |
private String facetSelection = "";
|
| 536 |
rajveer |
54 |
|
| 517 |
rajveer |
55 |
private String query;
|
|
|
56 |
|
|
|
57 |
private Double minPrice;
|
|
|
58 |
private Double maxPrice;
|
|
|
59 |
|
| 545 |
rajveer |
60 |
|
|
|
61 |
private long windowSize = 20;
|
|
|
62 |
private long page = 1;
|
|
|
63 |
private long totalResults;
|
|
|
64 |
private long beginIndex = 0;
|
|
|
65 |
private String url;
|
| 1921 |
vikas |
66 |
private String priceUrl = "?";
|
| 545 |
rajveer |
67 |
|
| 317 |
ashish |
68 |
/**
|
| 974 |
vikas |
69 |
* GET /abc/10004
|
| 317 |
ashish |
70 |
*
|
|
|
71 |
*/
|
| 974 |
vikas |
72 |
@Actions({
|
| 1472 |
rajveer |
73 |
@Action("/all-mobile-phones"),
|
|
|
74 |
@Action("/all-mobile-accessories"),
|
| 2506 |
rajveer |
75 |
@Action("/all-tablets"),
|
| 1472 |
rajveer |
76 |
|
| 974 |
vikas |
77 |
@Action("/business-phones"),
|
|
|
78 |
@Action("/high-end-multimedia-phones"),
|
|
|
79 |
@Action("/low-end-multimedia-phones"),
|
|
|
80 |
@Action("/basic-phones"),
|
|
|
81 |
|
|
|
82 |
@Action("/bluetooth-headset"),
|
|
|
83 |
@Action("/memory-card"),
|
|
|
84 |
@Action("/battery"),
|
|
|
85 |
@Action("/headset"),
|
|
|
86 |
@Action("/charger"),
|
|
|
87 |
@Action("/pen-drive"),
|
|
|
88 |
@Action("/carrying-case"),
|
| 1005 |
vikas |
89 |
@Action("/car-charger"),
|
| 1306 |
chandransh |
90 |
@Action("/screen-guard"),
|
| 1005 |
vikas |
91 |
|
| 1534 |
rajveer |
92 |
@Action("/face-plate"),
|
|
|
93 |
@Action("/decal"),
|
|
|
94 |
@Action("/data-cable"),
|
|
|
95 |
@Action("/ear-buds"),
|
|
|
96 |
@Action("/cleaning-kit"),
|
|
|
97 |
@Action("/speaker"),
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
| 1005 |
vikas |
101 |
@Action("/category")
|
| 974 |
vikas |
102 |
})
|
|
|
103 |
|
| 317 |
ashish |
104 |
public HttpHeaders show() throws SecurityException, IOException {
|
|
|
105 |
log.info("id=" + id);
|
| 1472 |
rajveer |
106 |
long categoryId = Long.parseLong(id);
|
|
|
107 |
this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
|
| 974 |
vikas |
108 |
|
| 2505 |
rajveer |
109 |
if( categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
|
| 1472 |
rajveer |
110 |
this.categoryTitle = "Mobile Price List India" + " | " + this.categoryName;
|
|
|
111 |
}else{
|
|
|
112 |
this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
|
| 317 |
ashish |
116 |
|
| 517 |
rajveer |
117 |
String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50006", "F_50007" };
|
|
|
118 |
|
|
|
119 |
String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
|
|
|
120 |
|
|
|
121 |
String[] fqrys = this.request.getParameterValues("fq");
|
| 545 |
rajveer |
122 |
|
|
|
123 |
url = "?";
|
|
|
124 |
|
| 517 |
rajveer |
125 |
query = "*";
|
|
|
126 |
|
| 536 |
rajveer |
127 |
int length = 1;
|
|
|
128 |
|
|
|
129 |
if(fqrys!= null){
|
|
|
130 |
length += fqrys.length;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
String[] newfqrys = new String[length];
|
| 2306 |
vikas |
134 |
if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
|
| 820 |
rajveer |
135 |
newfqrys[0] = "F_50010:"+categoryName;
|
|
|
136 |
}else{
|
|
|
137 |
newfqrys[0] = "F_50011:"+categoryName;
|
|
|
138 |
}
|
|
|
139 |
|
| 545 |
rajveer |
140 |
String urlCrumb = url;
|
| 536 |
rajveer |
141 |
|
| 620 |
rajveer |
142 |
this.crumbs = new ArrayList<String[]>();
|
|
|
143 |
List<String> arrList = Arrays.asList(facetDefIDs);
|
| 536 |
rajveer |
144 |
for(int i=1; i<length; i++) {
|
|
|
145 |
newfqrys[i] = fqrys[i-1];
|
| 545 |
rajveer |
146 |
urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
|
| 1921 |
vikas |
147 |
priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
|
| 2948 |
chandransh |
148 |
StringBuilder filterUrl = new StringBuilder(url);
|
| 620 |
rajveer |
149 |
String facetName = StringUtils.split(fqrys[i-1], ":")[0];
|
|
|
150 |
String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
|
|
|
151 |
String facetLabel = facetLabels[arrList.indexOf(facetName)];
|
| 1257 |
vikas |
152 |
this.categoryTitle += " | " + facetLabel + " " + facetValue;
|
| 1259 |
vikas |
153 |
this.facetSelection += facetValue + " ";
|
| 620 |
rajveer |
154 |
for(int j=1; j<length; j++) {
|
|
|
155 |
if(i==j){
|
|
|
156 |
continue;
|
|
|
157 |
}
|
| 2948 |
chandransh |
158 |
filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8"));
|
| 620 |
rajveer |
159 |
}
|
| 2948 |
chandransh |
160 |
String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
|
| 620 |
rajveer |
161 |
log.info("acrumb=" + Arrays.toString(acrumb));
|
|
|
162 |
this.crumbs.add(acrumb);
|
|
|
163 |
|
| 536 |
rajveer |
164 |
}
|
| 545 |
rajveer |
165 |
url = urlCrumb;
|
|
|
166 |
|
|
|
167 |
if(this.request.getParameter("page") != null){
|
|
|
168 |
this.page = Long.parseLong(this.request.getParameter("page"));
|
|
|
169 |
this.beginIndex = this.windowSize * (this.page-1);
|
|
|
170 |
}
|
| 3173 |
rajveer |
171 |
if(this.request.getParameter("minPrice") != null){
|
|
|
172 |
this.minPrice = (new Double(this.request.getParameter("minPrice")));
|
|
|
173 |
url= url + "&minPrice=" + this.request.getParameter("minPrice");
|
| 545 |
rajveer |
174 |
}
|
| 3173 |
rajveer |
175 |
if(this.request.getParameter("maxPrice") != null){
|
|
|
176 |
this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
|
|
|
177 |
url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
|
| 545 |
rajveer |
178 |
}
|
| 1377 |
rajveer |
179 |
|
| 3561 |
rajveer |
180 |
String sortOrder = "F_50002+asc";
|
| 536 |
rajveer |
181 |
|
| 3561 |
rajveer |
182 |
SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, sortOrder, sourceId);
|
| 517 |
rajveer |
183 |
this.results = search.getResultMap();
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
this.facets = new LinkedHashMap<String, List<String[]>>();
|
|
|
187 |
String qryString = this.request.getQueryString();
|
|
|
188 |
log.info("qryString=" + qryString);
|
|
|
189 |
|
|
|
190 |
for (int i=0; i<facetDefIDs.length; i++) {
|
|
|
191 |
String facetDefID = facetDefIDs[i];
|
|
|
192 |
String facetLabel = facetLabels[i];
|
|
|
193 |
HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
|
|
|
194 |
if(facetDetailMap==null)
|
|
|
195 |
continue;
|
|
|
196 |
List<String[]> values = new ArrayList<String[]>();
|
| 2948 |
chandransh |
197 |
for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
|
|
|
198 |
String facet = facetEntry.getKey();
|
|
|
199 |
String drilldownURL = url + "&fq=" + facetDefID + ":" +
|
| 545 |
rajveer |
200 |
URLEncoder.encode(facet, "UTF-8");
|
| 517 |
rajveer |
201 |
String[] afacet = new String[] { facet,
|
| 2948 |
chandransh |
202 |
facetEntry.getValue().toString(), drilldownURL };
|
| 517 |
rajveer |
203 |
values.add(afacet);
|
| 545 |
rajveer |
204 |
|
| 517 |
rajveer |
205 |
}
|
|
|
206 |
|
|
|
207 |
this.facets.put(facetLabel, values);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
|
| 545 |
rajveer |
211 |
|
| 517 |
rajveer |
212 |
Map<String, Double> priceMap = search.getPriceStatsMap();
|
|
|
213 |
if(priceMap != null){
|
|
|
214 |
this.minPrice = priceMap.get("min");
|
|
|
215 |
this.maxPrice = priceMap.get("max");
|
|
|
216 |
}else{
|
|
|
217 |
this.minPrice = 0.0;
|
|
|
218 |
this.maxPrice = 0.0;
|
|
|
219 |
}
|
| 545 |
rajveer |
220 |
this.totalResults = search.getTotalResults();
|
| 317 |
ashish |
221 |
return new DefaultHttpHeaders("show");
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public void setId(String id) {
|
| 536 |
rajveer |
225 |
StringTokenizer tokenizer = new StringTokenizer(id,"-");
|
|
|
226 |
while(tokenizer.hasMoreTokens()){
|
|
|
227 |
this.id = tokenizer.nextToken();
|
|
|
228 |
}
|
| 317 |
ashish |
229 |
}
|
|
|
230 |
|
|
|
231 |
public String getId() {
|
|
|
232 |
return this.id;
|
|
|
233 |
}
|
| 517 |
rajveer |
234 |
|
| 536 |
rajveer |
235 |
public String getCategoryName() {
|
|
|
236 |
return this.categoryName;
|
| 942 |
chandransh |
237 |
}
|
| 517 |
rajveer |
238 |
|
| 974 |
vikas |
239 |
public String getPageMetaDesc() {
|
| 1966 |
vikas |
240 |
return "Best price "
|
|
|
241 |
+ this.facetSelection.replaceAll("Phones", "")
|
|
|
242 |
+ this.categoryName
|
|
|
243 |
.replaceAll("Mobile Phones", "mobile phones")
|
|
|
244 |
.replaceAll("Phones", "mobile phones")
|
|
|
245 |
+ " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews.";
|
| 974 |
vikas |
246 |
}
|
|
|
247 |
|
|
|
248 |
public String getPageMetaKeywords() {
|
| 2306 |
vikas |
249 |
if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
|
| 974 |
vikas |
250 |
return this.categoryName + ", mobile phone";
|
|
|
251 |
}
|
| 2306 |
vikas |
252 |
if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
|
| 974 |
vikas |
253 |
return this.categoryName + ", phone accessories";
|
|
|
254 |
}
|
|
|
255 |
return "";
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
public String getCategoryTitle() {
|
|
|
259 |
return this.categoryTitle;
|
|
|
260 |
}
|
|
|
261 |
|
| 650 |
rajveer |
262 |
public String getQuery() {
|
| 517 |
rajveer |
263 |
return this.query;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
|
| 790 |
vikas |
267 |
public List<String> getResults() {
|
| 517 |
rajveer |
268 |
return this.results;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public Map<String, List<String[]>> getFacets() {
|
|
|
272 |
return this.facets;
|
|
|
273 |
}
|
|
|
274 |
|
| 545 |
rajveer |
275 |
public long getTotalResults(){
|
|
|
276 |
return totalResults;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
public String getUrl(){
|
|
|
280 |
return this.url;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
public long getBeginIndex(){
|
|
|
284 |
if(totalResults>0)
|
|
|
285 |
return beginIndex+1;
|
|
|
286 |
return beginIndex;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
public long getTotalPages() {
|
| 1376 |
rajveer |
290 |
return 1 + (totalResults-1)/windowSize;
|
| 545 |
rajveer |
291 |
}
|
|
|
292 |
|
|
|
293 |
public long getCurrentPage() {
|
|
|
294 |
return this.page;
|
|
|
295 |
}
|
|
|
296 |
|
| 517 |
rajveer |
297 |
public Double getMinPrice() {
|
|
|
298 |
return this.minPrice;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public Double getMaxPrice() {
|
|
|
302 |
return this.maxPrice;
|
|
|
303 |
}
|
|
|
304 |
|
| 1921 |
vikas |
305 |
public String getPriceUrl() {
|
|
|
306 |
return this.priceUrl;
|
|
|
307 |
}
|
|
|
308 |
|
| 627 |
rajveer |
309 |
public List<String[]> getCrumbs() {
|
|
|
310 |
return this.crumbs;
|
|
|
311 |
}
|
|
|
312 |
|
| 1200 |
chandransh |
313 |
public Map<String, String> getSnippets(){
|
| 3242 |
vikas |
314 |
if(results != null){
|
| 517 |
rajveer |
315 |
snippets = new HashMap<String, String>();
|
| 790 |
vikas |
316 |
for(String docId: results){
|
| 3561 |
rajveer |
317 |
String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
|
| 3242 |
vikas |
318 |
if (snippet != null) {
|
|
|
319 |
snippets.put(docId, snippet);
|
|
|
320 |
}
|
| 517 |
rajveer |
321 |
}
|
|
|
322 |
}
|
|
|
323 |
return snippets;
|
|
|
324 |
}
|
|
|
325 |
|
| 317 |
ashish |
326 |
}
|