Subversion Repositories SmartDukaan

Rev

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