Subversion Repositories SmartDukaan

Rev

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