Subversion Repositories SmartDukaan

Rev

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