Subversion Repositories SmartDukaan

Rev

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