Subversion Repositories SmartDukaan

Rev

Rev 5182 | Rev 5489 | 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;
317 ashish 28
import org.apache.struts2.rest.DefaultHttpHeaders;
29
import org.apache.struts2.rest.HttpHeaders;
30
 
31
/**
517 rajveer 32
 * @author rajveer
317 ashish 33
 *
34
 */
974 vikas 35
@Result(name = "show", location = "category-show.vm")
317 ashish 36
public class CategoryController extends BaseController {
37
 
1044 chandransh 38
	private static final long serialVersionUID = 8477108528765030321L;
39
 
832 rajveer 40
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 41
 
790 vikas 42
	private List<String> results;
517 rajveer 43
	private Map<String, String> snippets;
44
	private Map<String, List<String[]>> facets;
620 rajveer 45
	private List<String[]> crumbs;
517 rajveer 46
	/**
47
	 * 
48
	 */
317 ashish 49
	private String id;
50
 
536 rajveer 51
	private String categoryName;
974 vikas 52
	private String categoryTitle;
1259 vikas 53
	private String facetSelection = "";
536 rajveer 54
 
517 rajveer 55
	private String query;
56
 
57
	private Double minPrice;
58
	private Double maxPrice;
3830 chandransh 59
	private long categoryId;
517 rajveer 60
 
545 rajveer 61
	private long windowSize = 20;
62
	private long page = 1;
63
	private long totalResults;
64
	private long beginIndex = 0;
65
	private String url;
1921 vikas 66
	private String priceUrl = "?";
545 rajveer 67
 
317 ashish 68
	/**
974 vikas 69
	 * GET /abc/10004
317 ashish 70
	 * 
71
	 */
974 vikas 72
	@Actions({
1472 rajveer 73
		@Action("/all-mobile-phones"),
74
		@Action("/all-mobile-accessories"),
2506 rajveer 75
		@Action("/all-tablets"),
3656 mandeep.dh 76
		@Action("/all-laptops"),
1472 rajveer 77
 
974 vikas 78
		@Action("/business-phones"),
79
		@Action("/high-end-multimedia-phones"),
80
		@Action("/low-end-multimedia-phones"),
81
		@Action("/basic-phones"),
82
 
83
		@Action("/bluetooth-headset"),
84
		@Action("/memory-card"),
85
		@Action("/battery"),
86
		@Action("/headset"),
87
		@Action("/charger"),
88
		@Action("/pen-drive"),
89
		@Action("/carrying-case"),
1005 vikas 90
		@Action("/car-charger"),
1306 chandransh 91
		@Action("/screen-guard"),
1005 vikas 92
 
1534 rajveer 93
		@Action("/face-plate"),
94
		@Action("/decal"),
95
		@Action("/data-cable"),
96
		@Action("/ear-buds"),
97
		@Action("/cleaning-kit"),
98
		@Action("/speaker"),
5182 varun.gupt 99
		@Action("/hard-disk-drive"),
5426 rajveer 100
		@Action("/headphones"),
1534 rajveer 101
 
1005 vikas 102
		@Action("/category")
974 vikas 103
	})
104
 
317 ashish 105
    public HttpHeaders show() throws SecurityException, IOException {
106
    	log.info("id=" + id);
3830 chandransh 107
    	categoryId = Long.parseLong(id);
1472 rajveer 108
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 109
 
3656 mandeep.dh 110
    	if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
111
    		this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
112
    	}
113
    	else {
1472 rajveer 114
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
115
    	}
317 ashish 116
 
4137 varun.gupt 117
		String[] facetDefIDs = Utils.facetDefIDs;
118
    	String[] facetLabels = Utils.facetLabels;
517 rajveer 119
 
120
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 121
 
122
    	url = "?";
123
 
517 rajveer 124
    	query = "*";
125
 
536 rajveer 126
    	int length = 1;
127
 
128
    	if(fqrys!= null){
129
    		length += fqrys.length;
130
    	}
131
 
132
    	String[] newfqrys = new String[length];
2306 vikas 133
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 134
    		newfqrys[0] = "F_50010:"+categoryName;
135
    	}else{
136
    		newfqrys[0] = "F_50011:"+categoryName;
137
    	}
138
 
545 rajveer 139
    	String urlCrumb = url;
536 rajveer 140
 
620 rajveer 141
    	this.crumbs = new ArrayList<String[]>();
142
    	List<String> arrList = Arrays.asList(facetDefIDs);
536 rajveer 143
    	for(int i=1; i<length; i++) {
144
    		newfqrys[i] = fqrys[i-1];
545 rajveer 145
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
1921 vikas 146
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
2948 chandransh 147
    		StringBuilder filterUrl = new StringBuilder(url);
5064 varun.gupt 148
 
620 rajveer 149
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
150
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
5064 varun.gupt 151
 
152
    		try {
153
        		String facetLabel = facetLabels[arrList.indexOf(facetName)];
154
        		this.categoryTitle += " | " + facetLabel + " " + facetValue;
155
        		this.facetSelection += facetValue + " ";
156
        		for(int j=1; j<length; j++) {
157
        			if(i==j){
158
        				continue;
159
        			}
160
        			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8"));
161
    	    	}
162
        		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
163
        		log.info("acrumb=" + Arrays.toString(acrumb));
164
        		this.crumbs.add(acrumb);
165
 
166
			} catch (ArrayIndexOutOfBoundsException e) {
167
				log.error("CategoryControllerArrayIndexOutOfBoundsException " + arrList + " " + facetName + " " + facetLabels);
168
			}
536 rajveer 169
    	}
545 rajveer 170
		url = urlCrumb;
171
 
172
    	if(this.request.getParameter("page") != null){
173
    		this.page = Long.parseLong(this.request.getParameter("page"));
174
    		this.beginIndex = this.windowSize * (this.page-1);
175
    	}
3173 rajveer 176
    	if(this.request.getParameter("minPrice") != null){
177
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
178
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 179
    	}
3173 rajveer 180
    	if(this.request.getParameter("maxPrice") != null){
181
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
182
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 183
    	}    	
1377 rajveer 184
 
3561 rajveer 185
    	String sortOrder = "F_50002+asc";
536 rajveer 186
 
3561 rajveer 187
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, sortOrder, sourceId);
517 rajveer 188
    	this.results =  search.getResultMap(); 
189
 
190
 
191
    	this.facets = new LinkedHashMap<String, List<String[]>>();
192
    	String qryString = this.request.getQueryString();
193
    	log.info("qryString=" + qryString);
194
 
195
    	for (int i=0; i<facetDefIDs.length; i++) {
196
    		String facetDefID = facetDefIDs[i];
197
    		String facetLabel = facetLabels[i];
198
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
199
    		if(facetDetailMap==null)
200
    			continue;
201
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 202
    		for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
203
    		    String facet = facetEntry.getKey();
204
    			String drilldownURL = url + "&fq=" + facetDefID + ":" + 
545 rajveer 205
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 206
      			String[] afacet = new String[] { facet, 
2948 chandransh 207
      			      facetEntry.getValue().toString(), drilldownURL  };
517 rajveer 208
      			values.add(afacet);    
545 rajveer 209
 
517 rajveer 210
    		}
211
 
212
    		this.facets.put(facetLabel, values);
213
    	}
214
 
215
 
545 rajveer 216
 
517 rajveer 217
    	Map<String, Double> priceMap = search.getPriceStatsMap();
218
    	if(priceMap != null){
219
    		this.minPrice = priceMap.get("min");
220
    		this.maxPrice = priceMap.get("max");
221
    	}else{
222
    		this.minPrice = 0.0;
223
    		this.maxPrice = 0.0;
224
    	}
545 rajveer 225
    	this.totalResults = search.getTotalResults();
317 ashish 226
        return new DefaultHttpHeaders("show");
227
    }
228
 
229
    public void setId(String id) {
536 rajveer 230
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
231
    	while(tokenizer.hasMoreTokens()){
232
    		this.id = tokenizer.nextToken();
233
    	}
317 ashish 234
    }
235
 
236
    public String getId() {
237
    	return this.id;
238
    }
517 rajveer 239
 
536 rajveer 240
    public String getCategoryName() {
241
    	return this.categoryName;
3656 mandeep.dh 242
    }
517 rajveer 243
 
974 vikas 244
    public String getPageMetaDesc() {
1966 vikas 245
        return "Best price "
246
                + this.facetSelection.replaceAll("Phones", "")
247
                + this.categoryName
248
                        .replaceAll("Mobile Phones", "mobile phones")
249
                        .replaceAll("Phones", "mobile phones")
250
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 251
    }
252
 
253
    public String getPageMetaKeywords() {
2306 vikas 254
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 255
        	return this.categoryName + ", mobile phone";
256
    	}
2306 vikas 257
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 258
        	return this.categoryName + ", phone accessories";
259
    	}
260
        return "";
261
    }
262
 
263
    public String getCategoryTitle() {
264
    	return this.categoryTitle;
265
    }	
266
 
650 rajveer 267
	public String getQuery() {
517 rajveer 268
    	return this.query;
269
    }
270
 
271
 
790 vikas 272
    public List<String> getResults() {
517 rajveer 273
    	return this.results;
274
    }
275
 
276
    public Map<String, List<String[]>> getFacets() {
277
    	return this.facets;
278
    }
279
 
545 rajveer 280
    public long getTotalResults(){
281
    	return totalResults;
282
    }
283
 
284
    public String getUrl(){
285
    	return this.url;
286
    }
287
 
288
	public long getBeginIndex(){
289
		if(totalResults>0)
290
			return beginIndex+1;
291
		return beginIndex;
292
	}
293
 
294
	public long getTotalPages() {
1376 rajveer 295
		return 1 + (totalResults-1)/windowSize;
545 rajveer 296
	}
297
 
298
	public long getCurrentPage() {
299
		return this.page;
300
	}
301
 
517 rajveer 302
    public Double getMinPrice() {
303
    	return this.minPrice;
304
    }
305
 
306
    public Double getMaxPrice() {
307
    	return this.maxPrice;
308
    }
309
 
1921 vikas 310
    public String getPriceUrl() {
311
        return this.priceUrl;
312
    }
313
 
627 rajveer 314
    public List<String[]> getCrumbs() {
315
    	return this.crumbs;
316
    }
317
 
1200 chandransh 318
    public Map<String, String> getSnippets(){
3242 vikas 319
        if(results != null){
517 rajveer 320
    		snippets = new HashMap<String, String>();	
790 vikas 321
	    	for(String docId: results){
3561 rajveer 322
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3242 vikas 323
	    	    if (snippet != null) {
324
	    	        snippets.put(docId, snippet);
325
	    	    }
517 rajveer 326
			}
327
    	}
328
		return snippets;
329
    }
4453 varun.gupt 330
 
3830 chandransh 331
    @Override
332
	public String getHeaderSnippet() {
333
		String url = request.getQueryString();
4453 varun.gupt 334
		if (url == null)	{
3830 chandransh 335
			url = "";
4453 varun.gupt 336
		} else	{
3830 chandransh 337
			url = "?" + url;
338
		}
339
		url = request.getRequestURI() + url;
340
 
4453 varun.gupt 341
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , categoryId, true);
3830 chandransh 342
	}
3903 varun.gupt 343
}