Subversion Repositories SmartDukaan

Rev

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