Subversion Repositories SmartDukaan

Rev

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