Subversion Repositories SmartDukaan

Rev

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