Subversion Repositories SmartDukaan

Rev

Rev 536 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 536 Rev 545
Line 30... Line 30...
30
import org.apache.struts2.interceptor.ServletRequestAware;
30
import org.apache.struts2.interceptor.ServletRequestAware;
31
import org.apache.struts2.rest.DefaultHttpHeaders;
31
import org.apache.struts2.rest.DefaultHttpHeaders;
32
import org.apache.struts2.rest.HttpHeaders;
32
import org.apache.struts2.rest.HttpHeaders;
33
 
33
 
34
/**
34
/**
35
 * @author naveen
35
 * @author rajveer
36
 *
36
 *
37
 */
37
 */
38
public class SearchController extends BaseController {
38
public class SearchController extends BaseController {
39
 
39
 
40
	/**
40
	/**
Line 54... Line 54...
54
	private Map<String, String> snippets;
54
	private Map<String, String> snippets;
55
	
55
	
56
	/**
56
	/**
57
	 * 
57
	 * 
58
	 */
58
	 */
59
	private Map<String, List<String[]>> facets;
59
	private Map<String, List<String[]>> facets = null;
60
	
60
	
61
	/**
61
	/**
62
	 * 
62
	 * 
63
	 */
63
	 */
64
	private List<String[]> crumbs;
64
	private List<String[]> crumbs;
65
	
65
	
66
	private String  query;
66
	private String  query;
67
	
67
	
68
	private Double minPrice;
68
	private Double minPrice = null;
69
	private Double maxPrice;
69
	private Double maxPrice = null;
70
 
70
 
-
 
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;
71
	/**
77
	/**
72
	 * 
78
	 * 
73
	 * @return
79
	 * @return
74
	 * @throws UnsupportedEncodingException
80
	 * @throws UnsupportedEncodingException
75
	 */
81
	 */
76
    // GET /query
82
    // GET /query
77
    public HttpHeaders index() throws UnsupportedEncodingException {
83
    public HttpHeaders index() throws UnsupportedEncodingException {
78
    	
84
    	
-
 
85
    	// Something we need to figure out
79
    	PageLoaderHandler pageLoader = new PageLoaderHandler();
86
    	PageLoaderHandler pageLoader = new PageLoaderHandler();
80
		
-
 
81
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));
87
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));
82
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
88
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
83
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
89
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
84
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
90
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
85
		htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
91
		htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
86
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
92
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
87
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
93
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
88
		htmlSnippets.put("SEARCH_HEADER","");
94
		htmlSnippets.put("SEARCH_HEADER","");
89
		htmlSnippets.put("SEARCH_DETAILS","");
95
		htmlSnippets.put("SEARCH_DETAILS","");
90
		
96
		
-
 
97
 
91
    	log.info("this.request=" + this.request);
98
		log.info("this.request=" + this.request);
92
    	
-
 
93
    	query = this.request.getParameter("q");
-
 
94
    	
99
    	
95
    	// if multiple words are given, it should do AND for all of them
100
    	//url = this.request.getRequestURL();
96
    	if(query.contains(" ")){
101
		query = this.request.getParameter("q");
97
    		StringTokenizer tokens = new StringTokenizer(query, " ");
102
		url = "q="+ URLEncoder.encode(query, "UTF-8");
98
    		query = "";
103
		priceUrl = url;
99
    		while(tokens.hasMoreTokens()){
104
    	if(this.request.getParameter("page") != null){
-
 
105
    		this.page = Long.parseLong(this.request.getParameter("page"));
100
    			query = query + " AND " + tokens.nextToken();
106
    		this.beginIndex = this.windowSize * (this.page-1);
101
    		}
107
    	}
102
    		 
108
    	if(this.request.getParameter("min-price") != null){
-
 
109
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
-
 
110
    		url= url + "&min-price=" + this.request.getParameter("min-price");
103
    	}
111
    	}
104
    	log.info("query=" + query);
112
    	if(this.request.getParameter("max-price") != null){
-
 
113
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
-
 
114
    		url= url + "&max-price=" + this.request.getParameter("max-price");
-
 
115
    	}    	
105
    	
116
    	
106
    	String[] fqrys = this.request.getParameterValues("fq");
117
    	String[] fqrys = this.request.getParameterValues("fq");
107
 
118
    	
108
    	this.crumbs = new ArrayList<String[]>();
119
    	this.crumbs = new ArrayList<String[]>();
109
    	
120
    	
110
    	String strCrumb = "";
121
    	String strCrumb = "";
111
    	String urlCrumb = "";
122
    	String urlCrumb = "";
112
    	if(query != null) {
123
    	if(query != null) {
113
	    	strCrumb = query;
124
	    	strCrumb = query;
114
	    	urlCrumb = "q=" + URLEncoder.encode(query, "UTF-8"); 
125
	    	urlCrumb = url; 
115
	    	this.crumbs.add(new String[] { strCrumb, urlCrumb } );
126
	    	this.crumbs.add(new String[] { strCrumb, urlCrumb } );
116
			log.info("acrumb=" + 
127
			log.info("acrumb=" + 
117
					Arrays.toString(new String[] { strCrumb, urlCrumb }));
128
					Arrays.toString(new String[] { strCrumb, urlCrumb }));
118
    	}else {
129
    	}else {
119
    		/**
-
 
120
    		 * FIXME to handle if qry is null
-
 
121
    		 */
130
    		
122
    	}
131
    	}
123
    	
132
    	
124
//    	HashMap<String,String> facetqueries = new HashMap<String, String>();
-
 
125
    	
-
 
126
    	if(fqrys != null) {
133
    	if(fqrys != null) {
127
	    	log.info("fqrys=" + Arrays.toString(fqrys));
134
	    	log.info("fqrys=" + Arrays.toString(fqrys));
128
    	
135
    	
129
	    	for(int i=0; i<fqrys.length; i++) {
136
	    	for(int i=0; i<fqrys.length; i++) {
-
 
137
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
130
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
138
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
131
	//    		facetqueries.put(StringUtils.split(fqrys[i], ":")[0], StringUtils.split(fqrys[i], ":")[1]);
139
	//    		facetqueries.put(StringUtils.split(fqrys[i], ":")[0], StringUtils.split(fqrys[i], ":")[1]);
132
	    		//strCrumb += " " + facetValue;
140
	    		//strCrumb += " " + facetValue;
133
	    		strCrumb =  facetValue;
141
	    		strCrumb =  facetValue;
134
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
142
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
-
 
143
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
135
	    		String[] acrumb = new String[] { strCrumb, urlCrumb };
144
	    		String[] acrumb = new String[] { strCrumb, urlCrumb };
136
	    		log.info("acrumb=" + Arrays.toString(acrumb));
145
	    		log.info("acrumb=" + Arrays.toString(acrumb));
137
	    		
-
 
138
	    		this.crumbs.add(acrumb);
146
	    		this.crumbs.add(acrumb);
139
	    	}
147
	    	}
140
    	}
148
    	}
141
    	
149
    	url = urlCrumb;
142
    	/**
150
    	/**
143
		 * FIXME facte definition ids
151
		 * FIXME facet definition ids
144
		 */
152
		 */
145
    	// Hard coded for now
153
    	// Hard coded for now
146
//    	long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006, 
154
//    	long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006, 
147
//    			50007, 50008, 50009};
155
//    			50007, 50008, 50009};
148
    	
156
    	
Line 168... Line 176...
168
    	
176
    	
169
    	String[] facetLabels = new String[] {"Category","Price",
177
    	String[] facetLabels = new String[] {"Category","Price",
170
    	    	"Brand", "Data Connectivity", "Camera Resolution"
178
    	    	"Brand", "Data Connectivity", "Camera Resolution"
171
        	};
179
        	};
172
    	
180
    	
173
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs);
181
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice);
174
    	this.results = new LinkedHashMap<String, String[]>();
182
    	this.results = new LinkedHashMap<String, String[]>();
175
    	this.results =  search.getResultMap(); 
183
    	this.results =  search.getResultMap(); 
176
    	
184
    	
177
    /*	
-
 
178
    	long[] entityIDs = search.getResultEntityIDs();
-
 
179
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
-
 
180
    	
-
 
181
    	String[] entityNames = search.getResultEntityNames();
-
 
182
    	log.info("entityNames=" + Arrays.toString(entityNames));
-
 
183
    	
-
 
184
    	this.results = new LinkedHashMap<String, String[]>();
-
 
185
    	
-
 
186
    	if(entityIDs != null) {
-
 
187
	    	for(int i=0; i<entityIDs.length; i++) {
-
 
188
	    		String key = (new Long(entityIDs[i])).toString();
-
 
189
	    		
-
 
190
	    		String[] values = new String[] {entityNames[i]};
-
 
191
	    		
-
 
192
	    		this.results.put(key, values);
-
 
193
	    	}
-
 
194
    	}
-
 
195
    	*/
-
 
196
    	//log.info("this.results=" + this.results);
-
 
197
    	
-
 
198
    	// Facets
185
    	// Facets
199
    	this.facets = new LinkedHashMap<String, List<String[]>>();
-
 
-
 
186
    	
200
    	String qryString = this.request.getQueryString();
187
    	String qryString = this.request.getQueryString();
201
    	log.info("qryString=" + qryString);
188
    	log.info("qryString=" + qryString);
202
    	
189
    	
203
    	
-
 
-
 
190
		this.facets = new LinkedHashMap<String, List<String[]>>();
204
    	for (int i=0; i<facetDefIDs.length; i++) {
191
    	for (int i=0; i<facetDefIDs.length; i++) {
205
    		String facetDefID = facetDefIDs[i];
192
    		String facetDefID = facetDefIDs[i];
206
    		String facetLabel = facetLabels[i];
193
    		String facetLabel = facetLabels[i];
207
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
194
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
208
    		if(facetDetailMap==null)
195
    		if(facetDetailMap==null)
209
    			continue;
196
    			continue;
210
    		List<String[]> values = new ArrayList<String[]>();
197
    		List<String[]> values = new ArrayList<String[]>();
211
    		for(String facet: facetDetailMap.keySet()){
198
    		for(String facet: facetDetailMap.keySet()){
212
//    			String extraInfo = "LINK";
-
 
213
//    			if(facetqueries.containsKey(facet)){
-
 
214
//    				extraInfo = "NO_LINK";
-
 
215
//    			}
-
 
216
      			String drilldownURL = qryString;
199
    			String drilldownURL = url;  
217
    			drilldownURL += "&fq=" + facetDefID + ":" + 
200
      			drilldownURL += "&fq=" + facetDefID + ":" + 
218
    				URLEncoder.encode(facet, "UTF-8"); 
201
    				URLEncoder.encode(facet, "UTF-8"); 
219
      			String[] afacet = new String[] { facet, 
202
      			String[] afacet = new String[] { facet, 
220
      					facetDetailMap.get(facet).toString(), drilldownURL  };
203
      					facetDetailMap.get(facet).toString(), drilldownURL  };
221
      			values.add(afacet);    
204
      			values.add(afacet);    
222
      		  
205
      		  
Line 231... Line 214...
231
    		this.maxPrice = priceMap.get("max");
214
    		this.maxPrice = priceMap.get("max");
232
    	}else{
215
    	}else{
233
    		this.minPrice = 0.0;
216
    		this.minPrice = 0.0;
234
    		this.maxPrice = 0.0;
217
    		this.maxPrice = 0.0;
235
    	}
218
    	}
236
    	/*
-
 
237
    	for (int i=0; i<facetDefIDs.length; i++) {
-
 
238
    		long facetDefID = facetDefIDs[i];
-
 
239
    		String facetLabel = facetLabels[i];
-
 
240
    		//log.info("facetLabel=" + facetLabel);
-
 
241
    		
-
 
242
    		String[] facetValues = search.getFacetValues(facetDefID);
-
 
243
    		String[] facetCounts = search.getFacetCounts(facetDefID);
-
 
244
    		
-
 
245
    		
-
 
246
    		List<String[]> values = new ArrayList<String[]>();
-
 
247
    	
-
 
248
    		
-
 
249
    		for(int j=0; j<facetValues.length; j++) {
-
 
250
    			
-
 
251
    			if(facetCounts[j].equals("0")) {
-
 
252
    				continue;
-
 
253
    			}
-
 
254
    			
-
 
255
    			String drilldownURL = qryString;
-
 
256
    			drilldownURL += "&fq=F_" + facetDefID + ":" + 
-
 
257
    				URLEncoder.encode(facetValues[j], "UTF-8"); 
-
 
258
    			//log.info("drilldownURL=" + drilldownURL);
-
 
259
    			
-
 
260
    			String[] afacet = new String[] { facetValues[j], 
-
 
261
    					facetCounts[j], drilldownURL };
-
 
262
    			//log.info("afacet=" + Arrays.toString(afacet));
-
 
263
    			
-
 
264
    			values.add(afacet);    
-
 
265
    		}
-
 
266
    		//log.info("values=" + values);
-
 
267
    		
-
 
268
    		this.facets.put(facetLabel, values);
-
 
269
    		
-
 
270
    	}
-
 
271
    	//log.info("this.facets=" + this.facets);
-
 
272
    	*/
-
 
273
    	
219
    	
-
 
220
    	this.totalResults = search.getTotalResults();
274
    	return new DefaultHttpHeaders("index")
221
    	return new DefaultHttpHeaders("index")
275
            .disableCaching();
222
            .disableCaching();
276
    }
223
    }
277
 
224
 
278
 
225
 
Line 309... Line 256...
309
     */
256
     */
310
    public Map<String, List<String[]>> getFacets() {
257
    public Map<String, List<String[]>> getFacets() {
311
    	return this.facets;
258
    	return this.facets;
312
    }
259
    }
313
    
260
    
-
 
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
 
314
    public Double getMinPrice() {
287
    public Double getMinPrice() {
315
    	return this.minPrice;
288
    	return this.minPrice;
316
    }
289
    }
317
    
290
    
318
    public Double getMaxPrice() {
291
    public Double getMaxPrice() {