Subversion Repositories SmartDukaan

Rev

Rev 6942 | Rev 6976 | 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
 
6975 amit.gupta 23
import org.apache.commons.lang.ArrayUtils;
517 rajveer 24
import org.apache.commons.lang.StringUtils;
832 rajveer 25
import org.apache.log4j.Logger;
974 vikas 26
import org.apache.struts2.convention.annotation.Action;
27
import org.apache.struts2.convention.annotation.Actions;
28
import org.apache.struts2.convention.annotation.Result;
5489 amit.gupta 29
import org.apache.struts2.convention.annotation.Results;
317 ashish 30
import org.apache.struts2.rest.DefaultHttpHeaders;
31
import org.apache.struts2.rest.HttpHeaders;
32
 
6931 amit.gupta 33
import com.google.gson.Gson;
5489 amit.gupta 34
import com.opensymphony.xwork2.ActionContext;
35
 
317 ashish 36
/**
517 rajveer 37
 * @author rajveer
317 ashish 38
 *
39
 */
5489 amit.gupta 40
@Results({
41
	@Result(name = "show", location = "category-show.vm"),
42
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
43
})
317 ashish 44
public class CategoryController extends BaseController {
45
 
1044 chandransh 46
	private static final long serialVersionUID = 8477108528765030321L;
47
 
832 rajveer 48
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 49
 
790 vikas 50
	private List<String> results;
517 rajveer 51
	private Map<String, String> snippets;
52
	private Map<String, List<String[]>> facets;
6866 amit.gupta 53
	private Map<String, List<List<String>>> crumbs;
6931 amit.gupta 54
	private Map<String, Double> dynamicSearchMap;
517 rajveer 55
	/**
56
	 * 
57
	 */
317 ashish 58
	private String id;
59
 
536 rajveer 60
	private String categoryName;
5489 amit.gupta 61
	private String redirectUrl;
62
 
63
	public String getRedirectUrl() {
64
		return redirectUrl;
65
	}
66
 
67
 
974 vikas 68
	private String categoryTitle;
1259 vikas 69
	private String facetSelection = "";
536 rajveer 70
 
517 rajveer 71
	private String query;
72
 
73
	private Double minPrice;
74
	private Double maxPrice;
3830 chandransh 75
	private long categoryId;
517 rajveer 76
 
545 rajveer 77
	private long windowSize = 20;
78
	private long page = 1;
79
	private long totalResults;
80
	private long beginIndex = 0;
81
	private String url;
1921 vikas 82
	private String priceUrl = "?";
545 rajveer 83
 
317 ashish 84
	/**
974 vikas 85
	 * GET /abc/10004
6866 amit.gupta 86
	 * @throws Exception 
317 ashish 87
	 * 
88
	 */
974 vikas 89
	@Actions({
1472 rajveer 90
		@Action("/all-mobile-phones"),
91
		@Action("/all-mobile-accessories"),
2506 rajveer 92
		@Action("/all-tablets"),
3656 mandeep.dh 93
		@Action("/all-laptops"),
5873 amit.gupta 94
		@Action("/all-cameras"),
974 vikas 95
		@Action("/business-phones"),
96
		@Action("/high-end-multimedia-phones"),
97
		@Action("/low-end-multimedia-phones"),
98
		@Action("/basic-phones"),
5873 amit.gupta 99
		@Action("/dslr-cameras"),
100
		@Action("/compact-cameras"),
974 vikas 101
 
102
		@Action("/bluetooth-headset"),
103
		@Action("/memory-card"),
104
		@Action("/battery"),
105
		@Action("/headset"),
106
		@Action("/charger"),
107
		@Action("/pen-drive"),
108
		@Action("/carrying-case"),
1005 vikas 109
		@Action("/car-charger"),
1306 chandransh 110
		@Action("/screen-guard"),
1005 vikas 111
 
1534 rajveer 112
		@Action("/face-plate"),
113
		@Action("/decal"),
114
		@Action("/data-cable"),
115
		@Action("/ear-buds"),
116
		@Action("/cleaning-kit"),
117
		@Action("/speaker"),
5182 varun.gupt 118
		@Action("/hard-disk-drive"),
5489 amit.gupta 119
		@Action("/external-hard-disks"),
5426 rajveer 120
		@Action("/headphones"),
6593 amit.gupta 121
		@Action("/portable-music-players"),
1534 rajveer 122
 
1005 vikas 123
		@Action("/category")
974 vikas 124
	})
125
 
6866 amit.gupta 126
    public HttpHeaders show() throws Exception {
5489 amit.gupta 127
		if(ActionContext.getContext().getName().equals("hard-disk-drive")) {
128
			redirectUrl = "/external-hard-disks/10073";
129
	        log.info("301 Redirection to " + redirectUrl);
130
	        return new DefaultHttpHeaders("redirect");
131
		}
317 ashish 132
    	log.info("id=" + id);
3830 chandransh 133
    	categoryId = Long.parseLong(id);
1472 rajveer 134
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 135
 
3656 mandeep.dh 136
    	if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
137
    		this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
138
    	}
139
    	else {
1472 rajveer 140
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
141
    	}
6975 amit.gupta 142
    	String[] fqrys = this.request.getParameterValues("fq");
143
 
144
    	String[] facetDefIDs = Utils.rootfacetDefIDs;
145
    	String[] facetLabels = Utils.rootfacetLabels;
146
    	if((getChildren(this.categoryName) == null || getChildren(this.categoryName).size() == 1) || (this.request.getQueryString() != null && this.request.getQueryString().contains("F_50011"))) {
147
    		facetDefIDs = Utils.facetDefIDs;
148
    		facetLabels = Utils.facetLabels;
149
    	}
317 ashish 150
 
517 rajveer 151
 
545 rajveer 152
    	url = "?";
153
 
517 rajveer 154
    	query = "*";
155
 
536 rajveer 156
    	int length = 1;
157
 
158
    	if(fqrys!= null){
159
    		length += fqrys.length;
160
    	}
161
 
162
    	String[] newfqrys = new String[length];
2306 vikas 163
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 164
    		newfqrys[0] = "F_50010:"+categoryName;
165
    	}else{
166
    		newfqrys[0] = "F_50011:"+categoryName;
167
    	}
168
 
545 rajveer 169
    	String urlCrumb = url;
536 rajveer 170
 
6866 amit.gupta 171
    	this.crumbs = new HashMap<String, List<List<String>>>();
5064 varun.gupt 172
 
6866 amit.gupta 173
    	if(query != null) {
174
	    	urlCrumb = url; 
175
	    }else {
5064 varun.gupt 176
 
536 rajveer 177
    	}
6866 amit.gupta 178
 
179
    	if(fqrys != null) {
180
	    	log.info("fqrys=" + Arrays.toString(fqrys));
181
	    	List<String> arrList = Arrays.asList(facetDefIDs);
182
	    	String filterUrl = "";
183
	    	for(int i=0; i<fqrys.length; i++){
184
	    		newfqrys[i+1] = fqrys[i];
185
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
186
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
187
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
188
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
189
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
190
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
191
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
192
	    		if(!crumbs.containsKey(facetLabel)) {
193
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
194
	    		}
195
 
196
	    		this.crumbs.get(facetLabel).add(acrumb);
197
	    	}
198
    	}
545 rajveer 199
		url = urlCrumb;
200
 
201
    	if(this.request.getParameter("page") != null){
202
    		this.page = Long.parseLong(this.request.getParameter("page"));
203
    		this.beginIndex = this.windowSize * (this.page-1);
204
    	}
3173 rajveer 205
    	if(this.request.getParameter("minPrice") != null){
206
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
207
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 208
    	}
3173 rajveer 209
    	if(this.request.getParameter("maxPrice") != null){
210
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
211
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 212
    	}    	
1377 rajveer 213
 
6881 amit.gupta 214
    	String sortOrder = "F_50028+asc,F_50030+desc";
536 rajveer 215
 
3561 rajveer 216
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, 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
 
1921 vikas 320
    public String getPriceUrl() {
321
        return this.priceUrl;
322
    }
323
 
6866 amit.gupta 324
    public Map<String, List<List<String>>> getCrumbs() {
627 rajveer 325
    	return this.crumbs;
326
    }
327
 
1200 chandransh 328
    public Map<String, String> getSnippets(){
3242 vikas 329
        if(results != null){
517 rajveer 330
    		snippets = new HashMap<String, String>();	
790 vikas 331
	    	for(String docId: results){
3561 rajveer 332
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3242 vikas 333
	    	    if (snippet != null) {
334
	    	        snippets.put(docId, snippet);
335
	    	    }
517 rajveer 336
			}
337
    	}
338
		return snippets;
339
    }
4453 varun.gupt 340
 
3830 chandransh 341
    @Override
342
	public String getHeaderSnippet() {
343
		String url = request.getQueryString();
4453 varun.gupt 344
		if (url == null)	{
3830 chandransh 345
			url = "";
4453 varun.gupt 346
		} else	{
3830 chandransh 347
			url = "?" + url;
348
		}
349
		url = request.getRequestURI() + url;
350
 
6152 amit.gupta 351
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , categoryId, true);
3830 chandransh 352
	}
6708 kshitij.so 353
    @Override
354
	public String getCartWidgetSnippet() {
355
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),categoryId);
356
	}
6866 amit.gupta 357
 
358
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
359
    	String[] toshowfacetDefIDs;
360
    	String[] toshowfacetLabels;
361
 
362
		toshowfacetDefIDs = Utils.facetDefIDs;
363
		toshowfacetLabels = Utils.facetLabels;
364
 
365
    	this.facets = new LinkedHashMap<String, List<String[]>>();
366
    	for (int i=0; i<toshowfacetDefIDs.length; i++) {
367
    		String facetDefID = toshowfacetDefIDs[i];
368
    		String facetLabel = toshowfacetLabels[i];
369
 
370
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
371
    		if(facetDetailMap==null)
372
    			continue;
373
    		List<String[]> values = new ArrayList<String[]>();
374
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
375
    			String selected = "";
376
    			if(fqs.contains(facetDefID + ":" + facetEntry.getKey())) {
377
    				selected = "checked=\"checked\"";
378
    			}
379
    		    String facet = facetEntry.getKey();
380
      			String drilldownURL = "&fq=" +  
381
    				URLEncoder.encode( facetDefID + ":" + facet, "UTF-8"); 
382
      			String[] afacet = new String[] { facet, 
383
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
384
      			values.add(afacet);    
385
    		}
386
 
387
    		this.facets.put(facetLabel, values);
388
    	}
389
	}
390
 
391
    public List<String> getChildren(String categoryLabel) {
392
    	return SearchController.getCategoriesChildren().get(categoryLabel);
393
    }
6931 amit.gupta 394
 
395
	public String getDynamicSearchMap(){
396
    	if (this.dynamicSearchMap == null) {
397
    		return "{}";
398
    	} else {
399
    		return new Gson().toJson(this.dynamicSearchMap);
400
    	}
401
    }
3903 varun.gupt 402
}