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