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;
317 ashish 26
import org.apache.struts2.rest.DefaultHttpHeaders;
27
import org.apache.struts2.rest.HttpHeaders;
28
 
29
/**
517 rajveer 30
 * @author rajveer
317 ashish 31
 *
32
 */
33
public class CategoryController extends BaseController {
34
 
35
	/**
36
	 * 
37
	 */
832 rajveer 38
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 39
 
317 ashish 40
	/**
41
	 * 
42
	 */
790 vikas 43
	private List<String> results;
517 rajveer 44
	private Map<String, String> snippets;
45
	private Map<String, List<String[]>> facets;
620 rajveer 46
	private List<String[]> crumbs;
517 rajveer 47
	/**
48
	 * 
49
	 */
317 ashish 50
	private String id;
51
 
536 rajveer 52
	private String categoryName;
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
	/**
68
	 * GET /category/10004
69
	 * 
70
	 */
71
    public HttpHeaders show() throws SecurityException, IOException {
72
    	log.info("id=" + id);
536 rajveer 73
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(Long.parseLong(id));
317 ashish 74
 
517 rajveer 75
		String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
76
 
77
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" 	};
78
 
79
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 80
 
81
    	url = "?";
536 rajveer 82
 
545 rajveer 83
 
84
 
517 rajveer 85
    	query = "*";
86
 
536 rajveer 87
    	int length = 1;
88
 
89
    	if(fqrys!= null){
90
    		length += fqrys.length;
91
    	}
92
 
620 rajveer 93
 
536 rajveer 94
    	String[] newfqrys = new String[length];
820 rajveer 95
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParentCategoryId() == 10000){
96
    		newfqrys[0] = "F_50010:"+categoryName;
97
    	}else{
98
    		newfqrys[0] = "F_50011:"+categoryName;
99
    	}
100
 
545 rajveer 101
    	String urlCrumb = url;
536 rajveer 102
 
620 rajveer 103
    	this.crumbs = new ArrayList<String[]>();
104
    	List<String> arrList = Arrays.asList(facetDefIDs);
536 rajveer 105
    	for(int i=1; i<length; i++) {
106
    		newfqrys[i] = fqrys[i-1];
545 rajveer 107
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
108
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
620 rajveer 109
    		String filterUrl = url;
110
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
111
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
112
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
113
    		for(int j=1; j<length; j++) {
114
    			if(i==j){
115
    				continue;
116
    			}
117
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
118
	    	}
119
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
120
    		log.info("acrumb=" + Arrays.toString(acrumb));
121
    		this.crumbs.add(acrumb);
122
 
536 rajveer 123
    	}
545 rajveer 124
		url = urlCrumb;
125
 
126
    	if(this.request.getParameter("page") != null){
127
    		this.page = Long.parseLong(this.request.getParameter("page"));
128
    		this.beginIndex = this.windowSize * (this.page-1);
129
    	}
130
    	if(this.request.getParameter("min-price") != null){
131
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
132
    		url= url + "&min-price=" + this.request.getParameter("min-price");
133
    	}
134
    	if(this.request.getParameter("max-price") != null){
135
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
136
    		url= url + "&max-price=" + this.request.getParameter("max-price");
137
    	}    	
536 rajveer 138
 
569 rajveer 139
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
517 rajveer 140
    	this.results =  search.getResultMap(); 
141
 
142
 
143
    	this.facets = new LinkedHashMap<String, List<String[]>>();
144
    	String qryString = this.request.getQueryString();
145
    	log.info("qryString=" + qryString);
146
 
147
    	for (int i=0; i<facetDefIDs.length; i++) {
148
    		String facetDefID = facetDefIDs[i];
149
    		String facetLabel = facetLabels[i];
150
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
151
    		if(facetDetailMap==null)
152
    			continue;
153
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 154
    		String drilldownURL = new String();
517 rajveer 155
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 156
 
545 rajveer 157
    			/*
536 rajveer 158
    			if(qryString != null){
159
    				drilldownURL = qryString;
545 rajveer 160
 
536 rajveer 161
    			}
545 rajveer 162
    			*/
163
        		drilldownURL = url;
536 rajveer 164
 
545 rajveer 165
 
166
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
167
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 168
      			String[] afacet = new String[] { facet, 
169
      					facetDetailMap.get(facet).toString(), drilldownURL  };
170
      			values.add(afacet);    
545 rajveer 171
 
517 rajveer 172
    		}
173
 
174
    		this.facets.put(facetLabel, values);
175
    	}
176
 
177
 
545 rajveer 178
 
517 rajveer 179
    	Map<String, Double> priceMap = search.getPriceStatsMap();
180
    	if(priceMap != null){
181
    		this.minPrice = priceMap.get("min");
182
    		this.maxPrice = priceMap.get("max");
183
    	}else{
184
    		this.minPrice = 0.0;
185
    		this.maxPrice = 0.0;
186
    	}
545 rajveer 187
    	this.totalResults = search.getTotalResults();
317 ashish 188
        return new DefaultHttpHeaders("show");
189
    }
190
 
191
    public void setId(String id) {
536 rajveer 192
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
193
    	while(tokenizer.hasMoreTokens()){
194
    		this.id = tokenizer.nextToken();
195
    	}
317 ashish 196
    }
197
 
198
    public String getId() {
199
    	return this.id;
200
    }
517 rajveer 201
 
536 rajveer 202
    public String getCategoryName() {
203
    	return this.categoryName;
942 chandransh 204
    }	
517 rajveer 205
 
650 rajveer 206
	public String getQuery() {
517 rajveer 207
    	return this.query;
208
    }
209
 
210
 
790 vikas 211
    public List<String> getResults() {
517 rajveer 212
    	return this.results;
213
    }
214
 
215
    public Map<String, List<String[]>> getFacets() {
216
    	return this.facets;
217
    }
218
 
545 rajveer 219
    public long getTotalResults(){
220
    	return totalResults;
221
    }
222
 
223
    public String getUrl(){
224
    	return this.url;
225
    }
226
 
227
    public String getPriceUrl(){
228
    	return this.priceUrl;
229
    }
230
 
231
	public long getBeginIndex(){
232
		if(totalResults>0)
233
			return beginIndex+1;
234
		return beginIndex;
235
	}
236
 
237
	public long getTotalPages() {
238
		return 1 + totalResults/windowSize;
239
	}
240
 
241
	public long getCurrentPage() {
242
		return this.page;
243
	}
244
 
517 rajveer 245
    public Double getMinPrice() {
246
    	return this.minPrice;
247
    }
248
 
249
    public Double getMaxPrice() {
250
    	return this.maxPrice;
251
    }
252
 
627 rajveer 253
    public List<String[]> getCrumbs() {
254
    	return this.crumbs;
255
    }
256
 
517 rajveer 257
    public Map<String, String> getSnippets() throws Exception {
258
    	if(results != null){
259
    		snippets = new HashMap<String, String>();	
790 vikas 260
	    	for(String docId: results){
536 rajveer 261
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
517 rajveer 262
			}
263
    	}
264
		return snippets;
265
    }
266
 
317 ashish 267
}