Subversion Repositories SmartDukaan

Rev

Rev 1534 | Rev 1921 | 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
 
317 ashish 70
	/**
974 vikas 71
	 * GET /abc/10004
317 ashish 72
	 * 
73
	 */
974 vikas 74
	@Actions({
1472 rajveer 75
		@Action("/all-mobile-phones"),
76
		@Action("/all-mobile-accessories"),
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"),
99
 
100
 
101
 
1005 vikas 102
		@Action("/category")
974 vikas 103
	})
104
 
317 ashish 105
    public HttpHeaders show() throws SecurityException, IOException {
106
    	log.info("id=" + id);
1472 rajveer 107
    	long categoryId = Long.parseLong(id);
108
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 109
 
1472 rajveer 110
    	if( categoryId == MOBILE_PHONES_CATEGORY || categoryId == MOBILE_ACCESSORIES_CATEGORY ){
111
    		this.categoryTitle = "Mobile Price List India" + " | " + this.categoryName;
112
    	}else{
113
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
114
    	}
115
 
116
 
317 ashish 117
 
517 rajveer 118
		String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
119
 
120
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" 	};
121
 
122
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 123
 
124
    	url = "?";
125
 
517 rajveer 126
    	query = "*";
127
 
536 rajveer 128
    	int length = 1;
129
 
130
    	if(fqrys!= null){
131
    		length += fqrys.length;
132
    	}
133
 
134
    	String[] newfqrys = new String[length];
1376 rajveer 135
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId() == ROOT_CATEGORY){
820 rajveer 136
    		newfqrys[0] = "F_50010:"+categoryName;
137
    	}else{
138
    		newfqrys[0] = "F_50011:"+categoryName;
139
    	}
140
 
545 rajveer 141
    	String urlCrumb = url;
536 rajveer 142
 
620 rajveer 143
    	this.crumbs = new ArrayList<String[]>();
144
    	List<String> arrList = Arrays.asList(facetDefIDs);
536 rajveer 145
    	for(int i=1; i<length; i++) {
146
    		newfqrys[i] = fqrys[i-1];
545 rajveer 147
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
620 rajveer 148
    		String filterUrl = url;
149
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
150
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
151
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
1257 vikas 152
    		this.categoryTitle += " | " + facetLabel + " " + facetValue;
1259 vikas 153
    		this.facetSelection += facetValue + " ";
620 rajveer 154
    		for(int j=1; j<length; j++) {
155
    			if(i==j){
156
    				continue;
157
    			}
158
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
159
	    	}
160
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
161
    		log.info("acrumb=" + Arrays.toString(acrumb));
162
    		this.crumbs.add(acrumb);
163
 
536 rajveer 164
    	}
545 rajveer 165
		url = urlCrumb;
166
 
167
    	if(this.request.getParameter("page") != null){
168
    		this.page = Long.parseLong(this.request.getParameter("page"));
169
    		this.beginIndex = this.windowSize * (this.page-1);
170
    	}
171
    	if(this.request.getParameter("min-price") != null){
172
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
173
    		url= url + "&min-price=" + this.request.getParameter("min-price");
174
    	}
175
    	if(this.request.getParameter("max-price") != null){
176
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
177
    		url= url + "&max-price=" + this.request.getParameter("max-price");
178
    	}    	
1377 rajveer 179
 
180
    	query = query + "&sort=F_50002+asc";
536 rajveer 181
 
1376 rajveer 182
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, ROOT_CATEGORY, null);
517 rajveer 183
    	this.results =  search.getResultMap(); 
184
 
185
 
186
    	this.facets = new LinkedHashMap<String, List<String[]>>();
187
    	String qryString = this.request.getQueryString();
188
    	log.info("qryString=" + qryString);
189
 
190
    	for (int i=0; i<facetDefIDs.length; i++) {
191
    		String facetDefID = facetDefIDs[i];
192
    		String facetLabel = facetLabels[i];
193
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
194
    		if(facetDetailMap==null)
195
    			continue;
196
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 197
    		String drilldownURL = new String();
517 rajveer 198
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 199
 
545 rajveer 200
    			/*
536 rajveer 201
    			if(qryString != null){
202
    				drilldownURL = qryString;
545 rajveer 203
 
536 rajveer 204
    			}
545 rajveer 205
    			*/
206
        		drilldownURL = url;
536 rajveer 207
 
545 rajveer 208
 
209
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
210
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 211
      			String[] afacet = new String[] { facet, 
212
      					facetDetailMap.get(facet).toString(), drilldownURL  };
213
      			values.add(afacet);    
545 rajveer 214
 
517 rajveer 215
    		}
216
 
217
    		this.facets.put(facetLabel, values);
218
    	}
219
 
220
 
545 rajveer 221
 
517 rajveer 222
    	Map<String, Double> priceMap = search.getPriceStatsMap();
223
    	if(priceMap != null){
224
    		this.minPrice = priceMap.get("min");
225
    		this.maxPrice = priceMap.get("max");
226
    	}else{
227
    		this.minPrice = 0.0;
228
    		this.maxPrice = 0.0;
229
    	}
545 rajveer 230
    	this.totalResults = search.getTotalResults();
317 ashish 231
        return new DefaultHttpHeaders("show");
232
    }
233
 
234
    public void setId(String id) {
536 rajveer 235
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
236
    	while(tokenizer.hasMoreTokens()){
237
    		this.id = tokenizer.nextToken();
238
    	}
317 ashish 239
    }
240
 
241
    public String getId() {
242
    	return this.id;
243
    }
517 rajveer 244
 
536 rajveer 245
    public String getCategoryName() {
246
    	return this.categoryName;
942 chandransh 247
    }	
517 rajveer 248
 
974 vikas 249
    public String getPageMetaDesc() {
1259 vikas 250
    	return "Best price " + this.facetSelection + this.categoryName.replaceAll("Phones", "mobile phones") 
980 vikas 251
    	    + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 252
    }
253
 
254
    public String getPageMetaKeywords() {
1376 rajveer 255
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== MOBILE_PHONES_CATEGORY){
974 vikas 256
        	return this.categoryName + ", mobile phone";
257
    	}
1376 rajveer 258
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== MOBILE_ACCESSORIES_CATEGORY){
974 vikas 259
        	return this.categoryName + ", phone accessories";
260
    	}
261
        return "";
262
    }
263
 
264
    public String getCategoryTitle() {
265
    	return this.categoryTitle;
266
    }	
267
 
650 rajveer 268
	public String getQuery() {
517 rajveer 269
    	return this.query;
270
    }
271
 
272
 
790 vikas 273
    public List<String> getResults() {
517 rajveer 274
    	return this.results;
275
    }
276
 
277
    public Map<String, List<String[]>> getFacets() {
278
    	return this.facets;
279
    }
280
 
545 rajveer 281
    public long getTotalResults(){
282
    	return totalResults;
283
    }
284
 
285
    public String getUrl(){
286
    	return this.url;
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
}