Subversion Repositories SmartDukaan

Rev

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