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