Subversion Repositories SmartDukaan

Rev

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