Subversion Repositories SmartDukaan

Rev

Rev 5426 | Rev 5525 | 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
 
214
    	for (int i=0; i<facetDefIDs.length; i++) {
215
    		String facetDefID = facetDefIDs[i];
216
    		String facetLabel = facetLabels[i];
217
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
218
    		if(facetDetailMap==null)
219
    			continue;
220
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 221
    		for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
222
    		    String facet = facetEntry.getKey();
223
    			String drilldownURL = url + "&fq=" + facetDefID + ":" + 
545 rajveer 224
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 225
      			String[] afacet = new String[] { facet, 
2948 chandransh 226
      			      facetEntry.getValue().toString(), drilldownURL  };
517 rajveer 227
      			values.add(afacet);    
545 rajveer 228
 
517 rajveer 229
    		}
230
 
231
    		this.facets.put(facetLabel, values);
232
    	}
233
 
234
 
545 rajveer 235
 
517 rajveer 236
    	Map<String, Double> priceMap = search.getPriceStatsMap();
237
    	if(priceMap != null){
238
    		this.minPrice = priceMap.get("min");
239
    		this.maxPrice = priceMap.get("max");
240
    	}else{
241
    		this.minPrice = 0.0;
242
    		this.maxPrice = 0.0;
243
    	}
545 rajveer 244
    	this.totalResults = search.getTotalResults();
317 ashish 245
        return new DefaultHttpHeaders("show");
246
    }
247
 
248
    public void setId(String id) {
536 rajveer 249
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
250
    	while(tokenizer.hasMoreTokens()){
251
    		this.id = tokenizer.nextToken();
252
    	}
317 ashish 253
    }
254
 
255
    public String getId() {
256
    	return this.id;
257
    }
517 rajveer 258
 
536 rajveer 259
    public String getCategoryName() {
260
    	return this.categoryName;
3656 mandeep.dh 261
    }
517 rajveer 262
 
974 vikas 263
    public String getPageMetaDesc() {
1966 vikas 264
        return "Best price "
265
                + this.facetSelection.replaceAll("Phones", "")
266
                + this.categoryName
267
                        .replaceAll("Mobile Phones", "mobile phones")
268
                        .replaceAll("Phones", "mobile phones")
269
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 270
    }
271
 
272
    public String getPageMetaKeywords() {
2306 vikas 273
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 274
        	return this.categoryName + ", mobile phone";
275
    	}
2306 vikas 276
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 277
        	return this.categoryName + ", phone accessories";
278
    	}
279
        return "";
280
    }
281
 
282
    public String getCategoryTitle() {
283
    	return this.categoryTitle;
284
    }	
285
 
650 rajveer 286
	public String getQuery() {
517 rajveer 287
    	return this.query;
288
    }
289
 
290
 
790 vikas 291
    public List<String> getResults() {
517 rajveer 292
    	return this.results;
293
    }
294
 
295
    public Map<String, List<String[]>> getFacets() {
296
    	return this.facets;
297
    }
298
 
545 rajveer 299
    public long getTotalResults(){
300
    	return totalResults;
301
    }
302
 
303
    public String getUrl(){
304
    	return this.url;
305
    }
306
 
307
	public long getBeginIndex(){
308
		if(totalResults>0)
309
			return beginIndex+1;
310
		return beginIndex;
311
	}
312
 
313
	public long getTotalPages() {
1376 rajveer 314
		return 1 + (totalResults-1)/windowSize;
545 rajveer 315
	}
316
 
317
	public long getCurrentPage() {
318
		return this.page;
319
	}
320
 
517 rajveer 321
    public Double getMinPrice() {
322
    	return this.minPrice;
323
    }
324
 
325
    public Double getMaxPrice() {
326
    	return this.maxPrice;
327
    }
328
 
1921 vikas 329
    public String getPriceUrl() {
330
        return this.priceUrl;
331
    }
332
 
627 rajveer 333
    public List<String[]> getCrumbs() {
334
    	return this.crumbs;
335
    }
336
 
1200 chandransh 337
    public Map<String, String> getSnippets(){
3242 vikas 338
        if(results != null){
517 rajveer 339
    		snippets = new HashMap<String, String>();	
790 vikas 340
	    	for(String docId: results){
3561 rajveer 341
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3242 vikas 342
	    	    if (snippet != null) {
343
	    	        snippets.put(docId, snippet);
344
	    	    }
517 rajveer 345
			}
346
    	}
347
		return snippets;
348
    }
4453 varun.gupt 349
 
3830 chandransh 350
    @Override
351
	public String getHeaderSnippet() {
352
		String url = request.getQueryString();
4453 varun.gupt 353
		if (url == null)	{
3830 chandransh 354
			url = "";
4453 varun.gupt 355
		} else	{
3830 chandransh 356
			url = "?" + url;
357
		}
358
		url = request.getRequestURI() + url;
359
 
4453 varun.gupt 360
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , categoryId, true);
3830 chandransh 361
	}
3903 varun.gupt 362
}