Subversion Repositories SmartDukaan

Rev

Rev 6931 | Rev 6975 | 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
 
317 ashish 12
import java.io.IOException;
517 rajveer 13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.HashMap;
17
import java.util.LinkedHashMap;
18
import java.util.List;
19
import java.util.Map;
2948 chandransh 20
import java.util.Map.Entry;
536 rajveer 21
import java.util.StringTokenizer;
317 ashish 22
 
517 rajveer 23
import org.apache.commons.lang.StringUtils;
832 rajveer 24
import org.apache.log4j.Logger;
974 vikas 25
import org.apache.struts2.convention.annotation.Action;
26
import org.apache.struts2.convention.annotation.Actions;
27
import org.apache.struts2.convention.annotation.Result;
5489 amit.gupta 28
import org.apache.struts2.convention.annotation.Results;
317 ashish 29
import org.apache.struts2.rest.DefaultHttpHeaders;
30
import org.apache.struts2.rest.HttpHeaders;
31
 
6931 amit.gupta 32
import com.google.gson.Gson;
5489 amit.gupta 33
import com.opensymphony.xwork2.ActionContext;
34
 
317 ashish 35
/**
517 rajveer 36
 * @author rajveer
317 ashish 37
 *
38
 */
5489 amit.gupta 39
@Results({
40
	@Result(name = "show", location = "category-show.vm"),
41
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
42
})
317 ashish 43
public class CategoryController extends BaseController {
44
 
1044 chandransh 45
	private static final long serialVersionUID = 8477108528765030321L;
46
 
832 rajveer 47
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 48
 
790 vikas 49
	private List<String> results;
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;
61
 
62
	public String getRedirectUrl() {
63
		return redirectUrl;
64
	}
65
 
66
 
974 vikas 67
	private String categoryTitle;
1259 vikas 68
	private String facetSelection = "";
536 rajveer 69
 
517 rajveer 70
	private String query;
71
 
72
	private Double minPrice;
73
	private Double maxPrice;
3830 chandransh 74
	private long categoryId;
517 rajveer 75
 
545 rajveer 76
	private long windowSize = 20;
77
	private long page = 1;
78
	private long totalResults;
79
	private long beginIndex = 0;
80
	private String url;
1921 vikas 81
	private String priceUrl = "?";
545 rajveer 82
 
317 ashish 83
	/**
974 vikas 84
	 * GET /abc/10004
6866 amit.gupta 85
	 * @throws Exception 
317 ashish 86
	 * 
87
	 */
974 vikas 88
	@Actions({
1472 rajveer 89
		@Action("/all-mobile-phones"),
90
		@Action("/all-mobile-accessories"),
2506 rajveer 91
		@Action("/all-tablets"),
3656 mandeep.dh 92
		@Action("/all-laptops"),
5873 amit.gupta 93
		@Action("/all-cameras"),
974 vikas 94
		@Action("/business-phones"),
95
		@Action("/high-end-multimedia-phones"),
96
		@Action("/low-end-multimedia-phones"),
97
		@Action("/basic-phones"),
5873 amit.gupta 98
		@Action("/dslr-cameras"),
99
		@Action("/compact-cameras"),
974 vikas 100
 
101
		@Action("/bluetooth-headset"),
102
		@Action("/memory-card"),
103
		@Action("/battery"),
104
		@Action("/headset"),
105
		@Action("/charger"),
106
		@Action("/pen-drive"),
107
		@Action("/carrying-case"),
1005 vikas 108
		@Action("/car-charger"),
1306 chandransh 109
		@Action("/screen-guard"),
1005 vikas 110
 
1534 rajveer 111
		@Action("/face-plate"),
112
		@Action("/decal"),
113
		@Action("/data-cable"),
114
		@Action("/ear-buds"),
115
		@Action("/cleaning-kit"),
116
		@Action("/speaker"),
5182 varun.gupt 117
		@Action("/hard-disk-drive"),
5489 amit.gupta 118
		@Action("/external-hard-disks"),
5426 rajveer 119
		@Action("/headphones"),
6593 amit.gupta 120
		@Action("/portable-music-players"),
1534 rajveer 121
 
1005 vikas 122
		@Action("/category")
974 vikas 123
	})
124
 
6866 amit.gupta 125
    public HttpHeaders show() throws Exception {
5489 amit.gupta 126
		if(ActionContext.getContext().getName().equals("hard-disk-drive")) {
127
			redirectUrl = "/external-hard-disks/10073";
128
	        log.info("301 Redirection to " + redirectUrl);
129
	        return new DefaultHttpHeaders("redirect");
130
		}
317 ashish 131
    	log.info("id=" + id);
3830 chandransh 132
    	categoryId = Long.parseLong(id);
1472 rajveer 133
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 134
 
3656 mandeep.dh 135
    	if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
136
    		this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
137
    	}
138
    	else {
1472 rajveer 139
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
140
    	}
317 ashish 141
 
4137 varun.gupt 142
		String[] facetDefIDs = Utils.facetDefIDs;
143
    	String[] facetLabels = Utils.facetLabels;
517 rajveer 144
 
145
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 146
 
147
    	url = "?";
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
	    	List<String> arrList = Arrays.asList(facetDefIDs);
177
	    	String filterUrl = "";
178
	    	for(int i=0; i<fqrys.length; i++){
179
	    		newfqrys[i+1] = fqrys[i];
180
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
181
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
182
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
183
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
184
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
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;
195
 
196
    	if(this.request.getParameter("page") != null){
197
    		this.page = Long.parseLong(this.request.getParameter("page"));
198
    		this.beginIndex = this.windowSize * (this.page-1);
199
    	}
3173 rajveer 200
    	if(this.request.getParameter("minPrice") != null){
201
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
202
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 203
    	}
3173 rajveer 204
    	if(this.request.getParameter("maxPrice") != null){
205
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
206
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 207
    	}    	
1377 rajveer 208
 
6881 amit.gupta 209
    	String sortOrder = "F_50028+asc,F_50030+desc";
536 rajveer 210
 
3561 rajveer 211
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, sortOrder, sourceId);
517 rajveer 212
    	this.results =  search.getResultMap(); 
6931 amit.gupta 213
    	this.dynamicSearchMap =  search.getDynamicPriceMap();
517 rajveer 214
 
6866 amit.gupta 215
    	setFacet(Arrays.asList(newfqrys), search);
216
 
517 rajveer 217
 
218
    	Map<String, Double> priceMap = search.getPriceStatsMap();
219
    	if(priceMap != null){
6942 amit.gupta 220
    		if (this.minPrice == null){
221
    			this.minPrice = priceMap.get("min");
222
    		}
223
    		if(this.maxPrice == null) {
224
    			this.maxPrice = priceMap.get("max");
225
    		}
517 rajveer 226
    	}else{
227
    		this.minPrice = 0.0;
228
    		this.maxPrice = 0.0;
229
    	}
545 rajveer 230
    	this.totalResults = search.getTotalResults();
317 ashish 231
        return new DefaultHttpHeaders("show");
232
    }
233
 
234
    public void setId(String id) {
536 rajveer 235
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
236
    	while(tokenizer.hasMoreTokens()){
237
    		this.id = tokenizer.nextToken();
238
    	}
317 ashish 239
    }
240
 
241
    public String getId() {
242
    	return this.id;
243
    }
517 rajveer 244
 
536 rajveer 245
    public String getCategoryName() {
246
    	return this.categoryName;
3656 mandeep.dh 247
    }
517 rajveer 248
 
974 vikas 249
    public String getPageMetaDesc() {
1966 vikas 250
        return "Best price "
251
                + this.facetSelection.replaceAll("Phones", "")
252
                + this.categoryName
253
                        .replaceAll("Mobile Phones", "mobile phones")
254
                        .replaceAll("Phones", "mobile phones")
255
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 256
    }
257
 
258
    public String getPageMetaKeywords() {
2306 vikas 259
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 260
        	return this.categoryName + ", mobile phone";
261
    	}
2306 vikas 262
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 263
        	return this.categoryName + ", phone accessories";
264
    	}
265
        return "";
266
    }
267
 
268
    public String getCategoryTitle() {
269
    	return this.categoryTitle;
270
    }	
271
 
650 rajveer 272
	public String getQuery() {
517 rajveer 273
    	return this.query;
274
    }
275
 
276
 
790 vikas 277
    public List<String> getResults() {
517 rajveer 278
    	return this.results;
279
    }
280
 
281
    public Map<String, List<String[]>> getFacets() {
282
    	return this.facets;
283
    }
284
 
545 rajveer 285
    public long getTotalResults(){
286
    	return totalResults;
287
    }
288
 
289
    public String getUrl(){
290
    	return this.url;
291
    }
292
 
293
	public long getBeginIndex(){
294
		if(totalResults>0)
295
			return beginIndex+1;
296
		return beginIndex;
297
	}
298
 
299
	public long getTotalPages() {
1376 rajveer 300
		return 1 + (totalResults-1)/windowSize;
545 rajveer 301
	}
302
 
303
	public long getCurrentPage() {
304
		return this.page;
305
	}
306
 
517 rajveer 307
    public Double getMinPrice() {
308
    	return this.minPrice;
309
    }
310
 
311
    public Double getMaxPrice() {
312
    	return this.maxPrice;
313
    }
314
 
1921 vikas 315
    public String getPriceUrl() {
316
        return this.priceUrl;
317
    }
318
 
6866 amit.gupta 319
    public Map<String, List<List<String>>> getCrumbs() {
627 rajveer 320
    	return this.crumbs;
321
    }
322
 
1200 chandransh 323
    public Map<String, String> getSnippets(){
3242 vikas 324
        if(results != null){
517 rajveer 325
    		snippets = new HashMap<String, String>();	
790 vikas 326
	    	for(String docId: results){
3561 rajveer 327
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3242 vikas 328
	    	    if (snippet != null) {
329
	    	        snippets.put(docId, snippet);
330
	    	    }
517 rajveer 331
			}
332
    	}
333
		return snippets;
334
    }
4453 varun.gupt 335
 
3830 chandransh 336
    @Override
337
	public String getHeaderSnippet() {
338
		String url = request.getQueryString();
4453 varun.gupt 339
		if (url == null)	{
3830 chandransh 340
			url = "";
4453 varun.gupt 341
		} else	{
3830 chandransh 342
			url = "?" + url;
343
		}
344
		url = request.getRequestURI() + url;
345
 
6152 amit.gupta 346
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , categoryId, true);
3830 chandransh 347
	}
6708 kshitij.so 348
    @Override
349
	public String getCartWidgetSnippet() {
350
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),categoryId);
351
	}
6866 amit.gupta 352
 
353
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
354
    	String[] toshowfacetDefIDs;
355
    	String[] toshowfacetLabels;
356
 
357
		toshowfacetDefIDs = Utils.facetDefIDs;
358
		toshowfacetLabels = Utils.facetLabels;
359
 
360
    	this.facets = new LinkedHashMap<String, List<String[]>>();
361
    	for (int i=0; i<toshowfacetDefIDs.length; i++) {
362
    		String facetDefID = toshowfacetDefIDs[i];
363
    		String facetLabel = toshowfacetLabels[i];
364
 
365
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
366
    		if(facetDetailMap==null)
367
    			continue;
368
    		List<String[]> values = new ArrayList<String[]>();
369
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
370
    			String selected = "";
371
    			if(fqs.contains(facetDefID + ":" + facetEntry.getKey())) {
372
    				selected = "checked=\"checked\"";
373
    			}
374
    		    String facet = facetEntry.getKey();
375
      			String drilldownURL = "&fq=" +  
376
    				URLEncoder.encode( facetDefID + ":" + facet, "UTF-8"); 
377
      			String[] afacet = new String[] { facet, 
378
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
379
      			values.add(afacet);    
380
    		}
381
 
382
    		this.facets.put(facetLabel, values);
383
    	}
384
	}
385
 
386
    public List<String> getChildren(String categoryLabel) {
387
    	return SearchController.getCategoriesChildren().get(categoryLabel);
388
    }
6931 amit.gupta 389
 
390
	public String getDynamicSearchMap(){
391
    	if (this.dynamicSearchMap == null) {
392
    		return "{}";
393
    	} else {
394
    		return new Gson().toJson(this.dynamicSearchMap);
395
    	}
396
    }
3903 varun.gupt 397
}