Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
339 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
517 rajveer 6
import in.shop2020.serving.pages.PageContentKeys;
7
import in.shop2020.serving.pages.PageEnum;
8
import in.shop2020.serving.pages.PageManager;
9
import in.shop2020.serving.services.PageLoaderHandler;
339 rajveer 10
import in.shop2020.serving.services.SolrSearchService;
517 rajveer 11
import in.shop2020.serving.utils.FileUtils;
637 rajveer 12
import in.shop2020.serving.utils.Utils;
339 rajveer 13
 
517 rajveer 14
import java.io.File;
339 rajveer 15
import java.io.UnsupportedEncodingException;
16
import java.net.URLEncoder;
17
import java.util.ArrayList;
18
import java.util.Arrays;
354 rajveer 19
import java.util.HashMap;
339 rajveer 20
import java.util.LinkedHashMap;
21
import java.util.List;
22
import java.util.Map;
536 rajveer 23
import java.util.StringTokenizer;
339 rajveer 24
 
25
import javax.servlet.http.HttpServletRequest;
26
 
27
import org.apache.commons.lang.StringUtils;
28
import org.apache.juli.logging.Log;
29
import org.apache.juli.logging.LogFactory;
30
import org.apache.struts2.interceptor.ServletRequestAware;
31
import org.apache.struts2.rest.DefaultHttpHeaders;
32
import org.apache.struts2.rest.HttpHeaders;
33
 
34
/**
545 rajveer 35
 * @author rajveer
339 rajveer 36
 *
37
 */
517 rajveer 38
public class SearchController extends BaseController {
339 rajveer 39
 
40
	/**
41
	 * 
42
	 */
43
	private static Log log = LogFactory.getLog(SearchController.class);
44
 
517 rajveer 45
	private Map<String,String> htmlSnippets = new HashMap<String, String>();
339 rajveer 46
	/**
47
	 * 
48
	 */
517 rajveer 49
	private Map<String, String[]> results;
50
 
339 rajveer 51
	/**
52
	 * 
53
	 */
517 rajveer 54
	private Map<String, String> snippets;
339 rajveer 55
 
56
	/**
57
	 * 
58
	 */
545 rajveer 59
	private Map<String, List<String[]>> facets = null;
339 rajveer 60
 
61
	/**
62
	 * 
63
	 */
64
	private List<String[]> crumbs;
65
 
517 rajveer 66
	private String  query;
569 rajveer 67
	private String sortOrder = null;
545 rajveer 68
	private Double minPrice = null;
69
	private Double maxPrice = null;
354 rajveer 70
 
545 rajveer 71
	private long windowSize = 10;
72
	private long page = 1;
73
	private long totalResults;
74
	private long beginIndex = 0;
75
	private String url;
76
	private String priceUrl;
569 rajveer 77
	private long categoryId=10000;
78
	private String sortUrl;
339 rajveer 79
	/**
80
	 * 
81
	 * @return
82
	 * @throws UnsupportedEncodingException
83
	 */
84
    // GET /query
85
    public HttpHeaders index() throws UnsupportedEncodingException {
517 rajveer 86
 
545 rajveer 87
    	// Something we need to figure out
637 rajveer 88
 
555 chandransh 89
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser()));
517 rajveer 90
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
91
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
92
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
637 rajveer 93
		htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isLoggedIn()));
94
		htmlSnippets.put("BROWSE_HISTORY", pageLoader.getBrowseHistoryHtml(userinfo.getUserId(), userinfo.isLoggedIn()));
517 rajveer 95
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
96
		htmlSnippets.put("SEARCH_HEADER","");
97
		htmlSnippets.put("SEARCH_DETAILS","");
98
 
545 rajveer 99
 
100
		log.info("this.request=" + this.request);
339 rajveer 101
 
545 rajveer 102
    	//url = this.request.getRequestURL();
103
		query = this.request.getParameter("q");
104
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 105
 
106
 
107
		if(this.request.getParameter("category") != null){
108
    		this.categoryId = Long.parseLong(this.request.getParameter("category"));
109
    		url= url + "&category=" + this.request.getParameter("category");
110
    	}
111
		sortUrl = url;
112
		if(this.request.getParameter("sort") != null){
113
    		url= url + "&sort=" + this.request.getParameter("sort");
114
    		sortOrder = this.request.getParameter("sort");
115
    	}
545 rajveer 116
		priceUrl = url;
117
    	if(this.request.getParameter("page") != null){
118
    		this.page = Long.parseLong(this.request.getParameter("page"));
119
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 120
    	}
545 rajveer 121
    	if(this.request.getParameter("min-price") != null){
122
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
123
    		url= url + "&min-price=" + this.request.getParameter("min-price");
124
    	}
125
    	if(this.request.getParameter("max-price") != null){
126
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
127
    		url= url + "&max-price=" + this.request.getParameter("max-price");
128
    	}    	
339 rajveer 129
 
569 rajveer 130
 
339 rajveer 131
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 132
 
339 rajveer 133
    	this.crumbs = new ArrayList<String[]>();
134
 
569 rajveer 135
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
583 rajveer 136
    	String[] facetLabels = new String[] {"Category","Sub Category","Price",
569 rajveer 137
    	    	"Brand", "Data Connectivity", "Camera Resolution"
138
        	};
139
 
140
    	String urlCrumb = "";
141
    	if(query != null) {
142
	    	urlCrumb = url; 
143
	    }else {
144
 
145
    	}
146
 
339 rajveer 147
    	if(fqrys != null) {
148
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 149
	    	List<String> arrList = Arrays.asList(facetDefIDs);
150
	    	for(int i=0; i<fqrys.length; i++){
151
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
152
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
153
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
154
	    		String filterUrl = url;
545 rajveer 155
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 156
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 157
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
158
	    		for(int j=0; j<fqrys.length; j++) {
159
	    			if(i==j){
160
	    				continue;
161
	    			}
162
	    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
163
		    	}
164
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
165
	    		log.info("acrumb=" + Arrays.toString(acrumb));
166
	    		this.crumbs.add(acrumb);
167
	    	}
168
    	}
169
    	url = urlCrumb;
517 rajveer 170
 
171
 
339 rajveer 172
 
569 rajveer 173
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
354 rajveer 174
    	this.results = new LinkedHashMap<String, String[]>();
175
    	this.results =  search.getResultMap(); 
339 rajveer 176
 
545 rajveer 177
    	// Facets
339 rajveer 178
 
179
    	String qryString = this.request.getQueryString();
180
    	log.info("qryString=" + qryString);
181
 
545 rajveer 182
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 183
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 184
    		String facetDefID = facetDefIDs[i];
185
    		String facetLabel = facetLabels[i];
186
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
187
    		if(facetDetailMap==null)
188
    			continue;
189
    		List<String[]> values = new ArrayList<String[]>();
190
    		for(String facet: facetDetailMap.keySet()){
545 rajveer 191
    			String drilldownURL = url;  
192
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 193
    				URLEncoder.encode(facet, "UTF-8"); 
194
      			String[] afacet = new String[] { facet, 
195
      					facetDetailMap.get(facet).toString(), drilldownURL  };
196
      			values.add(afacet);    
197
 
198
    		}
199
 
200
    		this.facets.put(facetLabel, values);
201
    	}
202
 
203
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 204
    	if(priceMap != null){
205
    		this.minPrice = priceMap.get("min");
206
    		this.maxPrice = priceMap.get("max");
207
    	}else{
208
    		this.minPrice = 0.0;
209
    		this.maxPrice = 0.0;
210
    	}
354 rajveer 211
 
545 rajveer 212
    	this.totalResults = search.getTotalResults();
517 rajveer 213
    	return new DefaultHttpHeaders("index")
339 rajveer 214
            .disableCaching();
215
    }
517 rajveer 216
 
217
 
218
    public Map<String, String> getSnippets() throws Exception {
219
    	if(results != null){
220
    		snippets = new HashMap<String, String>();	
221
	    	for(String docId: results.keySet()){
222
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"SearchSnippet.html"));
223
			}
224
    	}
225
		return snippets;
226
    }
227
 
228
    /**
229
     * 
230
     * @return
231
     */
232
    public String getQuery() {
233
    	return this.query;
234
    }
339 rajveer 235
 
517 rajveer 236
 
339 rajveer 237
    /**
238
     * 
239
     * @return
240
     */
241
    public Map<String, String[]> getResults() {
242
    	return this.results;
243
    }
244
 
245
    /**
246
     * 
247
     * @return
248
     */
249
    public Map<String, List<String[]>> getFacets() {
250
    	return this.facets;
251
    }
252
 
545 rajveer 253
    public long getTotalResults(){
254
    	return totalResults;
255
    }
256
 
257
    public String getUrl(){
258
    	return this.url;
259
    }
260
 
261
    public String getPriceUrl(){
262
    	return this.priceUrl;
263
    }
264
 
569 rajveer 265
    public String getSortUrl(){
266
    	return this.sortUrl;
267
    }
268
 
545 rajveer 269
	public long getBeginIndex(){
270
		if(totalResults>0)
271
			return beginIndex+1;
272
		return beginIndex;
273
	}
274
 
275
	public long getTotalPages() {
276
		return 1 + totalResults/windowSize;
277
	}
278
 
279
	public long getCurrentPage() {
280
		return this.page;
281
	}
282
 
354 rajveer 283
    public Double getMinPrice() {
284
    	return this.minPrice;
285
    }
286
 
287
    public Double getMaxPrice() {
288
    	return this.maxPrice;
289
    }
290
 
339 rajveer 291
    /**
292
     * 
293
     * @return
294
     */
295
    public List<String[]> getCrumbs() {
296
    	return this.crumbs;
297
    }
298
 
299
    /**
300
     * 
301
     */
517 rajveer 302
 
303
    public String getHeaderSnippet(){
304
		return htmlSnippets.get("HEADER");
339 rajveer 305
	}
517 rajveer 306
 
307
	public String getMainMenuSnippet(){
308
		return htmlSnippets.get("MAIN_MENU");
309
	}
310
 
311
	public String getSearchBarSnippet(){
312
		return htmlSnippets.get("SEARCH_BAR");
313
	}
314
 
315
	public String getCustomerServiceSnippet(){
316
		return htmlSnippets.get("CUSTOMER_SERVICE");
317
	}
318
 
319
	public String getSearchHeaderSnippet(){
320
		return htmlSnippets.get("SEARCH_HEADER");
321
	}
322
 
323
	public String getSearchDetailsSnippet(){
324
		return htmlSnippets.get("SEARCH_DETAILS");
325
	}
326
 
327
	public String getMyResearchSnippet(){
328
		return htmlSnippets.get("MY_RESEARCH");
329
	}
569 rajveer 330
 
331
	public String getBrowseHistorySnippet(){
332
		return htmlSnippets.get("BROWSE_HISTORY");
333
	}
334
 
517 rajveer 335
	public String getFooterSnippet(){
336
		return htmlSnippets.get("FOOTER");
337
	}
338
 
339
	public String getJsFileSnippet(){
340
		return htmlSnippets.get("JS_FILES");
341
	}
342
 
343
	public String getCssFileSnippet(){
344
		return htmlSnippets.get("CSS_FILES");
345
	}
339 rajveer 346
}