Subversion Repositories SmartDukaan

Rev

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