Subversion Repositories SmartDukaan

Rev

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