Subversion Repositories SmartDukaan

Rev

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