Subversion Repositories SmartDukaan

Rev

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