Subversion Repositories SmartDukaan

Rev

Rev 13236 | 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"),
20109 amit.gupta 143
		@Action("/watch-strap"),
144
		@Action("/laptop-stand")
974 vikas 145
	})
146
 
6866 amit.gupta 147
    public HttpHeaders show() throws Exception {
6976 amit.gupta 148
		contextName = ActionContext.getContext().getName();
317 ashish 149
    	log.info("id=" + id);
3830 chandransh 150
    	categoryId = Long.parseLong(id);
8555 amit.gupta 151
    	if (categoryId == 10001l ||categoryId == 10002l ||categoryId == 10003l ||categoryId == 10004l ||categoryId == 10005l){
8554 amit.gupta 152
    		redirectUrl = "/mobile-phone/10006";
153
    		return new DefaultHttpHeaders("redirect");
154
    	}
1472 rajveer 155
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 156
 
3656 mandeep.dh 157
    	if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
158
    		this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
159
    	}
160
    	else {
1472 rajveer 161
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
162
    	}
6975 amit.gupta 163
    	String[] fqrys = this.request.getParameterValues("fq");
164
 
6998 amit.gupta 165
     	url = "?";
545 rajveer 166
 
517 rajveer 167
    	query = "*";
168
 
536 rajveer 169
    	int length = 1;
170
 
171
    	if(fqrys!= null){
172
    		length += fqrys.length;
173
    	}
174
 
175
    	String[] newfqrys = new String[length];
2306 vikas 176
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 177
    		newfqrys[0] = "F_50010:"+categoryName;
178
    	}else{
179
    		newfqrys[0] = "F_50011:"+categoryName;
180
    	}
181
 
545 rajveer 182
    	String urlCrumb = url;
536 rajveer 183
 
6866 amit.gupta 184
    	this.crumbs = new HashMap<String, List<List<String>>>();
5064 varun.gupt 185
 
6866 amit.gupta 186
    	if(query != null) {
187
	    	urlCrumb = url; 
188
	    }else {
5064 varun.gupt 189
 
536 rajveer 190
    	}
6866 amit.gupta 191
 
192
    	if(fqrys != null) {
193
	    	log.info("fqrys=" + Arrays.toString(fqrys));
194
	    	String filterUrl = "";
195
	    	for(int i=0; i<fqrys.length; i++){
196
	    		newfqrys[i+1] = fqrys[i];
197
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
198
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6976 amit.gupta 199
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6866 amit.gupta 200
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
201
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6998 amit.gupta 202
	    		String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
6866 amit.gupta 203
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
204
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
205
	    		if(!crumbs.containsKey(facetLabel)) {
206
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
207
	    		}
208
 
209
	    		this.crumbs.get(facetLabel).add(acrumb);
210
	    	}
211
    	}
545 rajveer 212
		url = urlCrumb;
6976 amit.gupta 213
		if(this.request.getParameter("minPrice") != null){
214
			this.minPrice = (new Double(this.request.getParameter("minPrice")));
215
			url= url + "&minPrice=" + this.request.getParameter("minPrice");
216
		}
217
		if(this.request.getParameter("maxPrice") != null){
218
			this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
219
			url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
220
		}
221
 
222
		sortUrl = url;
545 rajveer 223
 
224
    	if(this.request.getParameter("page") != null){
225
    		this.page = Long.parseLong(this.request.getParameter("page"));
226
    		this.beginIndex = this.windowSize * (this.page-1);
227
    	}
6976 amit.gupta 228
 
229
    	if(request.getParameter("sort")!=null){
230
    		url= url + "&sort=" + this.request.getParameter("sort");
231
    		sortOrder = this.request.getParameter("sort");
545 rajveer 232
    	}
12108 amit.gupta 233
    	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 234
    	this.results =  search.getResultMap(); 
12132 amit.gupta 235
    	this.dealResults =  search.getDealMap(); 
6931 amit.gupta 236
    	this.dynamicSearchMap =  search.getDynamicPriceMap();
517 rajveer 237
 
6866 amit.gupta 238
    	setFacet(Arrays.asList(newfqrys), search);
239
 
517 rajveer 240
 
241
    	Map<String, Double> priceMap = search.getPriceStatsMap();
242
    	if(priceMap != null){
6942 amit.gupta 243
    		if (this.minPrice == null){
244
    			this.minPrice = priceMap.get("min");
245
    		}
246
    		if(this.maxPrice == null) {
247
    			this.maxPrice = priceMap.get("max");
248
    		}
517 rajveer 249
    	}else{
250
    		this.minPrice = 0.0;
251
    		this.maxPrice = 0.0;
252
    	}
545 rajveer 253
    	this.totalResults = search.getTotalResults();
317 ashish 254
        return new DefaultHttpHeaders("show");
255
    }
256
 
257
    public void setId(String id) {
536 rajveer 258
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
259
    	while(tokenizer.hasMoreTokens()){
260
    		this.id = tokenizer.nextToken();
261
    	}
317 ashish 262
    }
263
 
264
    public String getId() {
265
    	return this.id;
266
    }
517 rajveer 267
 
536 rajveer 268
    public String getCategoryName() {
269
    	return this.categoryName;
3656 mandeep.dh 270
    }
517 rajveer 271
 
974 vikas 272
    public String getPageMetaDesc() {
1966 vikas 273
        return "Best price "
274
                + this.facetSelection.replaceAll("Phones", "")
275
                + this.categoryName
276
                        .replaceAll("Mobile Phones", "mobile phones")
277
                        .replaceAll("Phones", "mobile phones")
278
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 279
    }
280
 
281
    public String getPageMetaKeywords() {
2306 vikas 282
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 283
        	return this.categoryName + ", mobile phone";
284
    	}
2306 vikas 285
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 286
        	return this.categoryName + ", phone accessories";
287
    	}
288
        return "";
289
    }
290
 
291
    public String getCategoryTitle() {
292
    	return this.categoryTitle;
293
    }	
294
 
650 rajveer 295
	public String getQuery() {
517 rajveer 296
    	return this.query;
297
    }
298
 
299
 
790 vikas 300
    public List<String> getResults() {
517 rajveer 301
    	return this.results;
302
    }
303
 
304
    public Map<String, List<String[]>> getFacets() {
305
    	return this.facets;
306
    }
307
 
545 rajveer 308
    public long getTotalResults(){
309
    	return totalResults;
310
    }
311
 
312
    public String getUrl(){
313
    	return this.url;
314
    }
315
 
316
	public long getBeginIndex(){
317
		if(totalResults>0)
318
			return beginIndex+1;
319
		return beginIndex;
320
	}
321
 
322
	public long getTotalPages() {
1376 rajveer 323
		return 1 + (totalResults-1)/windowSize;
545 rajveer 324
	}
325
 
326
	public long getCurrentPage() {
327
		return this.page;
328
	}
329
 
517 rajveer 330
    public Double getMinPrice() {
331
    	return this.minPrice;
332
    }
333
 
334
    public Double getMaxPrice() {
335
    	return this.maxPrice;
336
    }
337
 
6976 amit.gupta 338
    public String getContextName() {
339
    	return contextName;
340
    }
341
 
1921 vikas 342
    public String getPriceUrl() {
343
        return this.priceUrl;
344
    }
345
 
6866 amit.gupta 346
    public Map<String, List<List<String>>> getCrumbs() {
627 rajveer 347
    	return this.crumbs;
348
    }
349
 
1200 chandransh 350
    public Map<String, String> getSnippets(){
3242 vikas 351
        if(results != null){
517 rajveer 352
    		snippets = new HashMap<String, String>();	
12141 amit.gupta 353
    		if(userinfo.isPrivateDealUser()) {
354
    			for(String docId: results){
12132 amit.gupta 355
	    			if(dealResults.contains(docId)) {
356
	    				String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.PRIVATE_DEAL_SNIPPET_KEY, docId, sourceId);
12138 amit.gupta 357
	    				if (snippet != null) {
358
	    					snippets.put(docId, snippet);
359
	    					continue;
360
	    				}
12132 amit.gupta 361
	    			}
12141 amit.gupta 362
	    			String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
363
	    			if (snippet != null) {
364
	    				snippets.put(docId, snippet);
365
	    			}
366
	    		} 
367
			} else {
368
				for(String docId: results){
369
					String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
370
					if (snippet != null) {
371
						snippets.put(docId, snippet);
372
					}
373
				} 
374
 
517 rajveer 375
			}
376
    	}
377
		return snippets;
378
    }
4453 varun.gupt 379
 
3830 chandransh 380
    @Override
381
	public String getHeaderSnippet() {
382
		String url = request.getQueryString();
4453 varun.gupt 383
		if (url == null)	{
3830 chandransh 384
			url = "";
4453 varun.gupt 385
		} else	{
3830 chandransh 386
			url = "?" + url;
387
		}
388
		url = request.getRequestURI() + url;
389
 
11829 amit.gupta 390
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , categoryId, true, userinfo.isPrivateDealUser());
3830 chandransh 391
	}
6708 kshitij.so 392
    @Override
393
	public String getCartWidgetSnippet() {
9155 kshitij.so 394
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),categoryId,activeBanners.get("side-banner"),sideBannersMap);
6708 kshitij.so 395
	}
6866 amit.gupta 396
 
397
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
6998 amit.gupta 398
    	List<String> filtrableFacets = search.getFilterableFacets();
6866 amit.gupta 399
    	this.facets = new LinkedHashMap<String, List<String[]>>();
6998 amit.gupta 400
    	for (String filtrableFacet : filtrableFacets) {
6866 amit.gupta 401
 
6998 amit.gupta 402
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
6866 amit.gupta 403
    		if(facetDetailMap==null)
404
    			continue;
405
    		List<String[]> values = new ArrayList<String[]>();
406
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
407
    			String selected = "";
6998 amit.gupta 408
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
6866 amit.gupta 409
    				selected = "checked=\"checked\"";
410
    			}
411
    		    String facet = facetEntry.getKey();
412
      			String drilldownURL = "&fq=" +  
6998 amit.gupta 413
    				URLEncoder.encode(filtrableFacet + ":" + facet, "UTF-8"); 
6866 amit.gupta 414
      			String[] afacet = new String[] { facet, 
415
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
416
      			values.add(afacet);    
417
    		}
418
 
6998 amit.gupta 419
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
6866 amit.gupta 420
    	}
421
	}
422
 
423
    public List<String> getChildren(String categoryLabel) {
424
    	return SearchController.getCategoriesChildren().get(categoryLabel);
425
    }
6931 amit.gupta 426
 
427
	public String getDynamicSearchMap(){
428
    	if (this.dynamicSearchMap == null) {
429
    		return "{}";
430
    	} else {
431
    		return new Gson().toJson(this.dynamicSearchMap);
432
    	}
433
    }
6976 amit.gupta 434
 
435
	public String getSortOrder(){
436
		return this.sortOrder;
437
	}
438
 
439
	public String getSortUrl(){
440
		return this.sortUrl;
441
	}
442
 
3903 varun.gupt 443
}