Subversion Repositories SmartDukaan

Rev

Rev 1376 | Rev 1472 | 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
    	}    	
1377 rajveer 165
 
166
    	query = query + "&sort=F_50002+asc";
536 rajveer 167
 
1376 rajveer 168
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, ROOT_CATEGORY, null);
517 rajveer 169
    	this.results =  search.getResultMap(); 
170
 
171
 
172
    	this.facets = new LinkedHashMap<String, List<String[]>>();
173
    	String qryString = this.request.getQueryString();
174
    	log.info("qryString=" + qryString);
175
 
176
    	for (int i=0; i<facetDefIDs.length; i++) {
177
    		String facetDefID = facetDefIDs[i];
178
    		String facetLabel = facetLabels[i];
179
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
180
    		if(facetDetailMap==null)
181
    			continue;
182
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 183
    		String drilldownURL = new String();
517 rajveer 184
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 185
 
545 rajveer 186
    			/*
536 rajveer 187
    			if(qryString != null){
188
    				drilldownURL = qryString;
545 rajveer 189
 
536 rajveer 190
    			}
545 rajveer 191
    			*/
192
        		drilldownURL = url;
536 rajveer 193
 
545 rajveer 194
 
195
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
196
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 197
      			String[] afacet = new String[] { facet, 
198
      					facetDetailMap.get(facet).toString(), drilldownURL  };
199
      			values.add(afacet);    
545 rajveer 200
 
517 rajveer 201
    		}
202
 
203
    		this.facets.put(facetLabel, values);
204
    	}
205
 
206
 
545 rajveer 207
 
517 rajveer 208
    	Map<String, Double> priceMap = search.getPriceStatsMap();
209
    	if(priceMap != null){
210
    		this.minPrice = priceMap.get("min");
211
    		this.maxPrice = priceMap.get("max");
212
    	}else{
213
    		this.minPrice = 0.0;
214
    		this.maxPrice = 0.0;
215
    	}
545 rajveer 216
    	this.totalResults = search.getTotalResults();
317 ashish 217
        return new DefaultHttpHeaders("show");
218
    }
219
 
220
    public void setId(String id) {
536 rajveer 221
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
222
    	while(tokenizer.hasMoreTokens()){
223
    		this.id = tokenizer.nextToken();
224
    	}
317 ashish 225
    }
226
 
227
    public String getId() {
228
    	return this.id;
229
    }
517 rajveer 230
 
536 rajveer 231
    public String getCategoryName() {
232
    	return this.categoryName;
942 chandransh 233
    }	
517 rajveer 234
 
974 vikas 235
    public String getPageMetaDesc() {
1259 vikas 236
    	return "Best price " + this.facetSelection + this.categoryName.replaceAll("Phones", "mobile phones") 
980 vikas 237
    	    + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 238
    }
239
 
240
    public String getPageMetaKeywords() {
1376 rajveer 241
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== MOBILE_PHONES_CATEGORY){
974 vikas 242
        	return this.categoryName + ", mobile phone";
243
    	}
1376 rajveer 244
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== MOBILE_ACCESSORIES_CATEGORY){
974 vikas 245
        	return this.categoryName + ", phone accessories";
246
    	}
247
        return "";
248
    }
249
 
250
    public String getCategoryTitle() {
251
    	return this.categoryTitle;
252
    }	
253
 
650 rajveer 254
	public String getQuery() {
517 rajveer 255
    	return this.query;
256
    }
257
 
258
 
790 vikas 259
    public List<String> getResults() {
517 rajveer 260
    	return this.results;
261
    }
262
 
263
    public Map<String, List<String[]>> getFacets() {
264
    	return this.facets;
265
    }
266
 
545 rajveer 267
    public long getTotalResults(){
268
    	return totalResults;
269
    }
270
 
271
    public String getUrl(){
272
    	return this.url;
273
    }
274
 
275
    public String getPriceUrl(){
276
    	return this.priceUrl;
277
    }
278
 
279
	public long getBeginIndex(){
280
		if(totalResults>0)
281
			return beginIndex+1;
282
		return beginIndex;
283
	}
284
 
285
	public long getTotalPages() {
1376 rajveer 286
		return 1 + (totalResults-1)/windowSize;
545 rajveer 287
	}
288
 
289
	public long getCurrentPage() {
290
		return this.page;
291
	}
292
 
517 rajveer 293
    public Double getMinPrice() {
294
    	return this.minPrice;
295
    }
296
 
297
    public Double getMaxPrice() {
298
    	return this.maxPrice;
299
    }
300
 
627 rajveer 301
    public List<String[]> getCrumbs() {
302
    	return this.crumbs;
303
    }
304
 
1200 chandransh 305
    public Map<String, String> getSnippets(){
517 rajveer 306
    	if(results != null){
307
    		snippets = new HashMap<String, String>();	
790 vikas 308
	    	for(String docId: results){
1200 chandransh 309
				try {
310
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
311
				} catch (IOException e) {
312
					log.error(e);
313
				}
517 rajveer 314
			}
315
    	}
316
		return snippets;
317
    }
318
 
317 ashish 319
}