Subversion Repositories SmartDukaan

Rev

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