Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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
 
12
import java.net.URLEncoder;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashMap;
16
import java.util.LinkedHashMap;
17
import java.util.List;
18
import java.util.Map;
2948 chandransh 19
import java.util.Map.Entry;
536 rajveer 20
import java.util.StringTokenizer;
317 ashish 21
 
517 rajveer 22
import org.apache.commons.lang.StringUtils;
832 rajveer 23
import org.apache.log4j.Logger;
974 vikas 24
import org.apache.struts2.convention.annotation.Action;
25
import org.apache.struts2.convention.annotation.Actions;
26
import org.apache.struts2.convention.annotation.Result;
5489 amit.gupta 27
import org.apache.struts2.convention.annotation.Results;
317 ashish 28
import org.apache.struts2.rest.DefaultHttpHeaders;
29
import org.apache.struts2.rest.HttpHeaders;
30
 
6931 amit.gupta 31
import com.google.gson.Gson;
5489 amit.gupta 32
import com.opensymphony.xwork2.ActionContext;
33
 
317 ashish 34
/**
517 rajveer 35
 * @author rajveer
317 ashish 36
 *
37
 */
5489 amit.gupta 38
@Results({
39
	@Result(name = "show", location = "category-show.vm"),
40
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
41
})
317 ashish 42
public class CategoryController extends BaseController {
43
 
1044 chandransh 44
	private static final long serialVersionUID = 8477108528765030321L;
45
 
9624 amit.gupta 46
	private static Logger log = Logger.getLogger(CategoryController.class);
517 rajveer 47
 
790 vikas 48
	private List<String> results;
517 rajveer 49
	private Map<String, String> snippets;
50
	private Map<String, List<String[]>> facets;
6866 amit.gupta 51
	private Map<String, List<List<String>>> crumbs;
6931 amit.gupta 52
	private Map<String, Double> dynamicSearchMap;
517 rajveer 53
	/**
54
	 * 
55
	 */
317 ashish 56
	private String id;
57
 
536 rajveer 58
	private String categoryName;
5489 amit.gupta 59
	private String redirectUrl;
6976 amit.gupta 60
 
61
	private String sortOrder = "F_50030+desc";
62
	private String sortUrl = "?";
5489 amit.gupta 63
 
64
	public String getRedirectUrl() {
65
		return redirectUrl;
66
	}
67
 
68
 
974 vikas 69
	private String categoryTitle;
1259 vikas 70
	private String facetSelection = "";
536 rajveer 71
 
517 rajveer 72
	private String query;
73
 
74
	private Double minPrice;
75
	private Double maxPrice;
3830 chandransh 76
	private long categoryId;
517 rajveer 77
 
545 rajveer 78
	private long windowSize = 20;
79
	private long page = 1;
80
	private long totalResults;
81
	private long beginIndex = 0;
82
	private String url;
1921 vikas 83
	private String priceUrl = "?";
545 rajveer 84
 
6976 amit.gupta 85
	private String contextName;
86
 
87
 
317 ashish 88
	/**
974 vikas 89
	 * GET /abc/10004
6866 amit.gupta 90
	 * @throws Exception 
317 ashish 91
	 * 
92
	 */
974 vikas 93
	@Actions({
1472 rajveer 94
		@Action("/all-mobile-phones"),
95
		@Action("/all-mobile-accessories"),
8561 amit.gupta 96
		@Action("/all-laptop-accessories"),
2506 rajveer 97
		@Action("/all-tablets"),
3656 mandeep.dh 98
		@Action("/all-laptops"),
5873 amit.gupta 99
		@Action("/all-cameras"),
974 vikas 100
		@Action("/business-phones"),
8179 amit.gupta 101
		@Action("/mobile-phone"),
974 vikas 102
		@Action("/high-end-multimedia-phones"),
103
		@Action("/low-end-multimedia-phones"),
104
		@Action("/basic-phones"),
5873 amit.gupta 105
		@Action("/dslr-cameras"),
106
		@Action("/compact-cameras"),
974 vikas 107
 
108
		@Action("/bluetooth-headset"),
109
		@Action("/memory-card"),
110
		@Action("/battery"),
111
		@Action("/headset"),
112
		@Action("/charger"),
113
		@Action("/pen-drive"),
114
		@Action("/carrying-case"),
1005 vikas 115
		@Action("/car-charger"),
1306 chandransh 116
		@Action("/screen-guard"),
1005 vikas 117
 
1534 rajveer 118
		@Action("/face-plate"),
119
		@Action("/decal"),
120
		@Action("/data-cable"),
121
		@Action("/ear-buds"),
122
		@Action("/cleaning-kit"),
123
		@Action("/speaker"),
5182 varun.gupt 124
		@Action("/hard-disk-drive"),
5489 amit.gupta 125
		@Action("/external-hard-disks"),
5426 rajveer 126
		@Action("/headphones"),
6593 amit.gupta 127
		@Action("/portable-music-players"),
6998 amit.gupta 128
		@Action("/data-cards"),
10040 amit.gupta 129
		@Action("/printer-cartridges"),
7418 amit.gupta 130
		@Action("/power-banks"),
8132 amit.gupta 131
		@Action("/mouse-pads"),
8561 amit.gupta 132
		@Action("/mouse"),
8132 amit.gupta 133
		@Action("/laptop-bags"),
8561 amit.gupta 134
		@Action("/laptop-batteries"),
8180 amit.gupta 135
		@Action("/laptop-chargers"),
8315 amit.gupta 136
		@Action("/all-smart-watches"),
9624 amit.gupta 137
		@Action("/smart-watches"),
138
		@Action("/wi-fi-routers"),
139
		@Action("/aux-cable"),
140
		@Action("/category"),
141
		@Action("/vehicle-mounts")
974 vikas 142
	})
143
 
6866 amit.gupta 144
    public HttpHeaders show() throws Exception {
6976 amit.gupta 145
		contextName = ActionContext.getContext().getName();
317 ashish 146
    	log.info("id=" + id);
3830 chandransh 147
    	categoryId = Long.parseLong(id);
8555 amit.gupta 148
    	if (categoryId == 10001l ||categoryId == 10002l ||categoryId == 10003l ||categoryId == 10004l ||categoryId == 10005l){
8554 amit.gupta 149
    		redirectUrl = "/mobile-phone/10006";
150
    		return new DefaultHttpHeaders("redirect");
151
    	}
1472 rajveer 152
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 153
 
3656 mandeep.dh 154
    	if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
155
    		this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
156
    	}
157
    	else {
1472 rajveer 158
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
159
    	}
6975 amit.gupta 160
    	String[] fqrys = this.request.getParameterValues("fq");
161
 
6998 amit.gupta 162
     	url = "?";
545 rajveer 163
 
517 rajveer 164
    	query = "*";
165
 
536 rajveer 166
    	int length = 1;
167
 
168
    	if(fqrys!= null){
169
    		length += fqrys.length;
170
    	}
171
 
172
    	String[] newfqrys = new String[length];
2306 vikas 173
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 174
    		newfqrys[0] = "F_50010:"+categoryName;
175
    	}else{
176
    		newfqrys[0] = "F_50011:"+categoryName;
177
    	}
178
 
545 rajveer 179
    	String urlCrumb = url;
536 rajveer 180
 
6866 amit.gupta 181
    	this.crumbs = new HashMap<String, List<List<String>>>();
5064 varun.gupt 182
 
6866 amit.gupta 183
    	if(query != null) {
184
	    	urlCrumb = url; 
185
	    }else {
5064 varun.gupt 186
 
536 rajveer 187
    	}
6866 amit.gupta 188
 
189
    	if(fqrys != null) {
190
	    	log.info("fqrys=" + Arrays.toString(fqrys));
191
	    	String filterUrl = "";
192
	    	for(int i=0; i<fqrys.length; i++){
193
	    		newfqrys[i+1] = fqrys[i];
194
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
195
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6976 amit.gupta 196
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6866 amit.gupta 197
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
198
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6998 amit.gupta 199
	    		String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
6866 amit.gupta 200
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
201
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
202
	    		if(!crumbs.containsKey(facetLabel)) {
203
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
204
	    		}
205
 
206
	    		this.crumbs.get(facetLabel).add(acrumb);
207
	    	}
208
    	}
545 rajveer 209
		url = urlCrumb;
6976 amit.gupta 210
		if(this.request.getParameter("minPrice") != null){
211
			this.minPrice = (new Double(this.request.getParameter("minPrice")));
212
			url= url + "&minPrice=" + this.request.getParameter("minPrice");
213
		}
214
		if(this.request.getParameter("maxPrice") != null){
215
			this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
216
			url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
217
		}
218
 
219
		sortUrl = url;
545 rajveer 220
 
221
    	if(this.request.getParameter("page") != null){
222
    		this.page = Long.parseLong(this.request.getParameter("page"));
223
    		this.beginIndex = this.windowSize * (this.page-1);
224
    	}
6976 amit.gupta 225
 
226
    	if(request.getParameter("sort")!=null){
227
    		url= url + "&sort=" + this.request.getParameter("sort");
228
    		sortOrder = this.request.getParameter("sort");
545 rajveer 229
    	}
8354 amit.gupta 230
    	SolrSearchService search = new SolrSearchService(URLEncoder.encode(query, "UTF-8"), newfqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, "F_50028+asc," + sortOrder, sourceId);
517 rajveer 231
    	this.results =  search.getResultMap(); 
6931 amit.gupta 232
    	this.dynamicSearchMap =  search.getDynamicPriceMap();
517 rajveer 233
 
6866 amit.gupta 234
    	setFacet(Arrays.asList(newfqrys), search);
235
 
517 rajveer 236
 
237
    	Map<String, Double> priceMap = search.getPriceStatsMap();
238
    	if(priceMap != null){
6942 amit.gupta 239
    		if (this.minPrice == null){
240
    			this.minPrice = priceMap.get("min");
241
    		}
242
    		if(this.maxPrice == null) {
243
    			this.maxPrice = priceMap.get("max");
244
    		}
517 rajveer 245
    	}else{
246
    		this.minPrice = 0.0;
247
    		this.maxPrice = 0.0;
248
    	}
545 rajveer 249
    	this.totalResults = search.getTotalResults();
317 ashish 250
        return new DefaultHttpHeaders("show");
251
    }
252
 
253
    public void setId(String id) {
536 rajveer 254
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
255
    	while(tokenizer.hasMoreTokens()){
256
    		this.id = tokenizer.nextToken();
257
    	}
317 ashish 258
    }
259
 
260
    public String getId() {
261
    	return this.id;
262
    }
517 rajveer 263
 
536 rajveer 264
    public String getCategoryName() {
265
    	return this.categoryName;
3656 mandeep.dh 266
    }
517 rajveer 267
 
974 vikas 268
    public String getPageMetaDesc() {
1966 vikas 269
        return "Best price "
270
                + this.facetSelection.replaceAll("Phones", "")
271
                + this.categoryName
272
                        .replaceAll("Mobile Phones", "mobile phones")
273
                        .replaceAll("Phones", "mobile phones")
274
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 275
    }
276
 
277
    public String getPageMetaKeywords() {
2306 vikas 278
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 279
        	return this.categoryName + ", mobile phone";
280
    	}
2306 vikas 281
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 282
        	return this.categoryName + ", phone accessories";
283
    	}
284
        return "";
285
    }
286
 
287
    public String getCategoryTitle() {
288
    	return this.categoryTitle;
289
    }	
290
 
650 rajveer 291
	public String getQuery() {
517 rajveer 292
    	return this.query;
293
    }
294
 
295
 
790 vikas 296
    public List<String> getResults() {
517 rajveer 297
    	return this.results;
298
    }
299
 
300
    public Map<String, List<String[]>> getFacets() {
301
    	return this.facets;
302
    }
303
 
545 rajveer 304
    public long getTotalResults(){
305
    	return totalResults;
306
    }
307
 
308
    public String getUrl(){
309
    	return this.url;
310
    }
311
 
312
	public long getBeginIndex(){
313
		if(totalResults>0)
314
			return beginIndex+1;
315
		return beginIndex;
316
	}
317
 
318
	public long getTotalPages() {
1376 rajveer 319
		return 1 + (totalResults-1)/windowSize;
545 rajveer 320
	}
321
 
322
	public long getCurrentPage() {
323
		return this.page;
324
	}
325
 
517 rajveer 326
    public Double getMinPrice() {
327
    	return this.minPrice;
328
    }
329
 
330
    public Double getMaxPrice() {
331
    	return this.maxPrice;
332
    }
333
 
6976 amit.gupta 334
    public String getContextName() {
335
    	return contextName;
336
    }
337
 
1921 vikas 338
    public String getPriceUrl() {
339
        return this.priceUrl;
340
    }
341
 
6866 amit.gupta 342
    public Map<String, List<List<String>>> getCrumbs() {
627 rajveer 343
    	return this.crumbs;
344
    }
345
 
1200 chandransh 346
    public Map<String, String> getSnippets(){
3242 vikas 347
        if(results != null){
517 rajveer 348
    		snippets = new HashMap<String, String>();	
790 vikas 349
	    	for(String docId: results){
3561 rajveer 350
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3242 vikas 351
	    	    if (snippet != null) {
352
	    	        snippets.put(docId, snippet);
353
	    	    }
517 rajveer 354
			}
355
    	}
356
		return snippets;
357
    }
4453 varun.gupt 358
 
3830 chandransh 359
    @Override
360
	public String getHeaderSnippet() {
361
		String url = request.getQueryString();
4453 varun.gupt 362
		if (url == null)	{
3830 chandransh 363
			url = "";
4453 varun.gupt 364
		} else	{
3830 chandransh 365
			url = "?" + url;
366
		}
367
		url = request.getRequestURI() + url;
368
 
11829 amit.gupta 369
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , categoryId, true, userinfo.isPrivateDealUser());
3830 chandransh 370
	}
6708 kshitij.so 371
    @Override
372
	public String getCartWidgetSnippet() {
9155 kshitij.so 373
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),categoryId,activeBanners.get("side-banner"),sideBannersMap);
6708 kshitij.so 374
	}
6866 amit.gupta 375
 
376
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
6998 amit.gupta 377
    	List<String> filtrableFacets = search.getFilterableFacets();
6866 amit.gupta 378
    	this.facets = new LinkedHashMap<String, List<String[]>>();
6998 amit.gupta 379
    	for (String filtrableFacet : filtrableFacets) {
6866 amit.gupta 380
 
6998 amit.gupta 381
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
6866 amit.gupta 382
    		if(facetDetailMap==null)
383
    			continue;
384
    		List<String[]> values = new ArrayList<String[]>();
385
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
386
    			String selected = "";
6998 amit.gupta 387
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
6866 amit.gupta 388
    				selected = "checked=\"checked\"";
389
    			}
390
    		    String facet = facetEntry.getKey();
391
      			String drilldownURL = "&fq=" +  
6998 amit.gupta 392
    				URLEncoder.encode(filtrableFacet + ":" + facet, "UTF-8"); 
6866 amit.gupta 393
      			String[] afacet = new String[] { facet, 
394
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
395
      			values.add(afacet);    
396
    		}
397
 
6998 amit.gupta 398
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
6866 amit.gupta 399
    	}
400
	}
401
 
402
    public List<String> getChildren(String categoryLabel) {
403
    	return SearchController.getCategoriesChildren().get(categoryLabel);
404
    }
6931 amit.gupta 405
 
406
	public String getDynamicSearchMap(){
407
    	if (this.dynamicSearchMap == null) {
408
    		return "{}";
409
    	} else {
410
    		return new Gson().toJson(this.dynamicSearchMap);
411
    	}
412
    }
6976 amit.gupta 413
 
414
	public String getSortOrder(){
415
		return this.sortOrder;
416
	}
417
 
418
	public String getSortUrl(){
419
		return this.sortUrl;
420
	}
421
 
3903 varun.gupt 422
}