Subversion Repositories SmartDukaan

Rev

Details | 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;
317 ashish 23
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
832 rajveer 25
import org.apache.log4j.Logger;
974 vikas 26
import org.apache.struts2.convention.annotation.Action;
27
import org.apache.struts2.convention.annotation.Actions;
28
import org.apache.struts2.convention.annotation.Result;
317 ashish 29
import org.apache.struts2.rest.DefaultHttpHeaders;
30
import org.apache.struts2.rest.HttpHeaders;
31
 
32
/**
517 rajveer 33
 * @author rajveer
317 ashish 34
 *
35
 */
974 vikas 36
@Result(name = "show", location = "category-show.vm")
317 ashish 37
public class CategoryController extends BaseController {
38
 
39
	/**
40
	 * 
41
	 */
832 rajveer 42
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 43
 
317 ashish 44
	/**
45
	 * 
46
	 */
790 vikas 47
	private List<String> results;
517 rajveer 48
	private Map<String, String> snippets;
49
	private Map<String, List<String[]>> facets;
620 rajveer 50
	private List<String[]> crumbs;
517 rajveer 51
	/**
52
	 * 
53
	 */
317 ashish 54
	private String id;
55
 
536 rajveer 56
	private String categoryName;
974 vikas 57
	private String categoryTitle;
536 rajveer 58
 
517 rajveer 59
	private String query;
60
 
61
	private Double minPrice;
62
	private Double maxPrice;
63
 
545 rajveer 64
 
65
	private long windowSize = 20;
66
	private long page = 1;
67
	private long totalResults;
68
	private long beginIndex = 0;
69
	private String url;
70
	private String priceUrl;
71
 
317 ashish 72
	/**
974 vikas 73
	 * GET /abc/10004
317 ashish 74
	 * 
75
	 */
974 vikas 76
	@Actions({
77
		@Action("/business-phones"),
78
		@Action("/high-end-multimedia-phones"),
79
		@Action("/low-end-multimedia-phones"),
80
		@Action("/basic-phones"),
81
 
82
		@Action("/bluetooth-headset"),
83
		@Action("/memory-card"),
84
		@Action("/battery"),
85
		@Action("/headset"),
86
		@Action("/charger"),
87
		@Action("/pen-drive"),
88
		@Action("/carrying-case"),
1005 vikas 89
		@Action("/car-charger"),
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];
820 rajveer 120
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId() == 10000){
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)];
138
    		for(int j=1; j<length; j++) {
139
    			if(i==j){
140
    				continue;
141
    			}
142
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
143
	    	}
144
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
145
    		log.info("acrumb=" + Arrays.toString(acrumb));
146
    		this.crumbs.add(acrumb);
147
 
536 rajveer 148
    	}
545 rajveer 149
		url = urlCrumb;
150
 
151
    	if(this.request.getParameter("page") != null){
152
    		this.page = Long.parseLong(this.request.getParameter("page"));
153
    		this.beginIndex = this.windowSize * (this.page-1);
154
    	}
155
    	if(this.request.getParameter("min-price") != null){
156
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
157
    		url= url + "&min-price=" + this.request.getParameter("min-price");
158
    	}
159
    	if(this.request.getParameter("max-price") != null){
160
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
161
    		url= url + "&max-price=" + this.request.getParameter("max-price");
162
    	}    	
536 rajveer 163
 
569 rajveer 164
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
517 rajveer 165
    	this.results =  search.getResultMap(); 
166
 
167
 
168
    	this.facets = new LinkedHashMap<String, List<String[]>>();
169
    	String qryString = this.request.getQueryString();
170
    	log.info("qryString=" + qryString);
171
 
172
    	for (int i=0; i<facetDefIDs.length; i++) {
173
    		String facetDefID = facetDefIDs[i];
174
    		String facetLabel = facetLabels[i];
175
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
176
    		if(facetDetailMap==null)
177
    			continue;
178
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 179
    		String drilldownURL = new String();
517 rajveer 180
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 181
 
545 rajveer 182
    			/*
536 rajveer 183
    			if(qryString != null){
184
    				drilldownURL = qryString;
545 rajveer 185
 
536 rajveer 186
    			}
545 rajveer 187
    			*/
188
        		drilldownURL = url;
536 rajveer 189
 
545 rajveer 190
 
191
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
192
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 193
      			String[] afacet = new String[] { facet, 
194
      					facetDetailMap.get(facet).toString(), drilldownURL  };
195
      			values.add(afacet);    
545 rajveer 196
 
517 rajveer 197
    		}
198
 
199
    		this.facets.put(facetLabel, values);
200
    	}
201
 
202
 
545 rajveer 203
 
517 rajveer 204
    	Map<String, Double> priceMap = search.getPriceStatsMap();
205
    	if(priceMap != null){
206
    		this.minPrice = priceMap.get("min");
207
    		this.maxPrice = priceMap.get("max");
208
    	}else{
209
    		this.minPrice = 0.0;
210
    		this.maxPrice = 0.0;
211
    	}
545 rajveer 212
    	this.totalResults = search.getTotalResults();
317 ashish 213
        return new DefaultHttpHeaders("show");
214
    }
215
 
216
    public void setId(String id) {
536 rajveer 217
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
218
    	while(tokenizer.hasMoreTokens()){
219
    		this.id = tokenizer.nextToken();
220
    	}
317 ashish 221
    }
222
 
223
    public String getId() {
224
    	return this.id;
225
    }
517 rajveer 226
 
536 rajveer 227
    public String getCategoryName() {
228
    	return this.categoryName;
942 chandransh 229
    }	
517 rajveer 230
 
974 vikas 231
    public String getPageMetaDesc() {
980 vikas 232
    	return "Best price " + this.categoryName.replaceAll("Phones", "mobile phones") 
233
    	    + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 234
    }
235
 
236
    public String getPageMetaKeywords() {
237
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== 10001){
238
        	return this.categoryName + ", mobile phone";
239
    	}
240
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId()== 10011){
241
        	return this.categoryName + ", phone accessories";
242
    	}
243
        return "";
244
    }
245
 
246
    public String getCategoryTitle() {
247
    	return this.categoryTitle;
248
    }	
249
 
650 rajveer 250
	public String getQuery() {
517 rajveer 251
    	return this.query;
252
    }
253
 
254
 
790 vikas 255
    public List<String> getResults() {
517 rajveer 256
    	return this.results;
257
    }
258
 
259
    public Map<String, List<String[]>> getFacets() {
260
    	return this.facets;
261
    }
262
 
545 rajveer 263
    public long getTotalResults(){
264
    	return totalResults;
265
    }
266
 
267
    public String getUrl(){
268
    	return this.url;
269
    }
270
 
271
    public String getPriceUrl(){
272
    	return this.priceUrl;
273
    }
274
 
275
	public long getBeginIndex(){
276
		if(totalResults>0)
277
			return beginIndex+1;
278
		return beginIndex;
279
	}
280
 
281
	public long getTotalPages() {
282
		return 1 + totalResults/windowSize;
283
	}
284
 
285
	public long getCurrentPage() {
286
		return this.page;
287
	}
288
 
517 rajveer 289
    public Double getMinPrice() {
290
    	return this.minPrice;
291
    }
292
 
293
    public Double getMaxPrice() {
294
    	return this.maxPrice;
295
    }
296
 
627 rajveer 297
    public List<String[]> getCrumbs() {
298
    	return this.crumbs;
299
    }
300
 
517 rajveer 301
    public Map<String, String> getSnippets() throws Exception {
302
    	if(results != null){
303
    		snippets = new HashMap<String, String>();	
790 vikas 304
	    	for(String docId: results){
536 rajveer 305
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
517 rajveer 306
			}
307
    	}
308
		return snippets;
309
    }
310
 
317 ashish 311
}