Subversion Repositories SmartDukaan

Rev

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