Subversion Repositories SmartDukaan

Rev

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