Subversion Repositories SmartDukaan

Rev

Rev 3656 | Rev 5525 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
339 rajveer 1
package in.shop2020.serving.controllers;
1044 chandransh 2
 
2263 vikas 3
import in.shop2020.datalogger.EventType;
3561 rajveer 4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
5
import in.shop2020.serving.cache.SnippetCacheWrapper;
339 rajveer 6
import in.shop2020.serving.services.SolrSearchService;
637 rajveer 7
import in.shop2020.serving.utils.Utils;
2511 vikas 8
import in.shop2020.utils.DataLogger;
339 rajveer 9
 
10
import java.io.UnsupportedEncodingException;
11
import java.net.URLEncoder;
12
import java.util.ArrayList;
13
import java.util.Arrays;
354 rajveer 14
import java.util.HashMap;
339 rajveer 15
import java.util.LinkedHashMap;
16
import java.util.List;
17
import java.util.Map;
2948 chandransh 18
import java.util.Map.Entry;
339 rajveer 19
 
20
import org.apache.commons.lang.StringUtils;
832 rajveer 21
import org.apache.log4j.Logger;
820 rajveer 22
import org.apache.struts2.convention.annotation.Result;
339 rajveer 23
 
1044 chandransh 24
 
339 rajveer 25
/**
545 rajveer 26
 * @author rajveer
339 rajveer 27
 *
28
 */
820 rajveer 29
@Result(name="redirect", location="${location}", type="redirect")
517 rajveer 30
public class SearchController extends BaseController {
339 rajveer 31
 
1044 chandransh 32
	private static final long serialVersionUID = -8392433517042806559L;
1999 vikas 33
	private static Logger log = Logger.getLogger(Class.class);
1044 chandransh 34
 
790 vikas 35
	private List<String> results;
517 rajveer 36
 
339 rajveer 37
	/**
38
	 * 
39
	 */
517 rajveer 40
	private Map<String, String> snippets;
339 rajveer 41
 
42
	/**
43
	 * 
44
	 */
545 rajveer 45
	private Map<String, List<String[]>> facets = null;
339 rajveer 46
 
47
	/**
48
	 * 
49
	 */
50
	private List<String[]> crumbs;
51
 
517 rajveer 52
	private String  query;
569 rajveer 53
	private String sortOrder = null;
545 rajveer 54
	private Double minPrice = null;
55
	private Double maxPrice = null;
354 rajveer 56
 
545 rajveer 57
	private long windowSize = 10;
58
	private long page = 1;
59
	private long totalResults;
60
	private long beginIndex = 0;
61
	private String url;
1921 vikas 62
	private String priceUrl = "?";
3561 rajveer 63
	private long categoryId = Utils.ROOT_CATEGORY;
569 rajveer 64
	private String sortUrl;
820 rajveer 65
	private String location;
4137 varun.gupt 66
 
339 rajveer 67
	/**
68
	 * 
69
	 * @return
70
	 * @throws UnsupportedEncodingException
71
	 */
72
    // GET /query
820 rajveer 73
 
74
    public String index() throws UnsupportedEncodingException {
517 rajveer 75
 
650 rajveer 76
    	log.info("this.request=" + this.request);
637 rajveer 77
 
545 rajveer 78
    	//url = this.request.getRequestURL();
79
		query = this.request.getParameter("q");
80
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 81
 
82
 
83
		if(this.request.getParameter("category") != null){
84
    		this.categoryId = Long.parseLong(this.request.getParameter("category"));
85
    		url= url + "&category=" + this.request.getParameter("category");
86
    	}
820 rajveer 87
		if(query.trim().isEmpty()){
2606 rajveer 88
			if(categoryId == Utils.MOBILE_PHONES_CATEGORY){
1472 rajveer 89
				location = "/all-mobile-phones/" + categoryId;
820 rajveer 90
				return "redirect";
2606 rajveer 91
			}else if(categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){
1472 rajveer 92
				location = "/all-mobile-accessories/" + categoryId;
93
				return "redirect";
3656 mandeep.dh 94
			} else if(categoryId == Utils.TABLETS_CATEGORY){
2606 rajveer 95
				location = "/all-tablets/" + categoryId;
96
				return "redirect";
3656 mandeep.dh 97
			} else if(categoryId == Utils.LAPTOPS_CATEGORY){
98
                location = "/all-laptops/" + categoryId;
99
                return "redirect";
100
            }else{
820 rajveer 101
				return "index";
102
			}
103
		}
569 rajveer 104
		sortUrl = url;
105
		if(this.request.getParameter("sort") != null){
106
    		url= url + "&sort=" + this.request.getParameter("sort");
107
    		sortOrder = this.request.getParameter("sort");
108
    	}
1921 vikas 109
		priceUrl += url;
545 rajveer 110
    	if(this.request.getParameter("page") != null){
111
    		this.page = Long.parseLong(this.request.getParameter("page"));
112
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 113
    	}
3173 rajveer 114
    	if(this.request.getParameter("minPrice") != null){
115
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
116
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 117
    	}
3173 rajveer 118
    	if(this.request.getParameter("maxPrice") != null){
119
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
120
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 121
    	}    	
339 rajveer 122
 
569 rajveer 123
 
339 rajveer 124
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 125
 
339 rajveer 126
    	this.crumbs = new ArrayList<String[]>();
127
 
4137 varun.gupt 128
    	String[] facetDefIDs = Utils.facetDefIDs;
129
    	String[] facetLabels = Utils.facetLabels;
569 rajveer 130
 
131
    	String urlCrumb = "";
132
    	if(query != null) {
133
	    	urlCrumb = url; 
134
	    }else {
135
 
136
    	}
137
 
339 rajveer 138
    	if(fqrys != null) {
139
	    	log.info("fqrys=" + Arrays.toString(fqrys));
569 rajveer 140
	    	List<String> arrList = Arrays.asList(facetDefIDs);
141
	    	for(int i=0; i<fqrys.length; i++){
142
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
143
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
144
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
2948 chandransh 145
	    		StringBuilder filterUrl = new StringBuilder(url);
545 rajveer 146
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 147
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
569 rajveer 148
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
149
	    		for(int j=0; j<fqrys.length; j++) {
150
	    			if(i==j){
151
	    				continue;
152
	    			}
2948 chandransh 153
	    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));
569 rajveer 154
		    	}
2948 chandransh 155
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
569 rajveer 156
	    		log.info("acrumb=" + Arrays.toString(acrumb));
157
	    		this.crumbs.add(acrumb);
158
	    	}
159
    	}
160
    	url = urlCrumb;
517 rajveer 161
 
162
 
339 rajveer 163
 
3561 rajveer 164
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder, sourceId);
790 vikas 165
 
354 rajveer 166
    	this.results =  search.getResultMap(); 
339 rajveer 167
 
545 rajveer 168
    	// Facets
339 rajveer 169
 
170
    	String qryString = this.request.getQueryString();
171
    	log.info("qryString=" + qryString);
172
 
545 rajveer 173
		this.facets = new LinkedHashMap<String, List<String[]>>();
339 rajveer 174
    	for (int i=0; i<facetDefIDs.length; i++) {
354 rajveer 175
    		String facetDefID = facetDefIDs[i];
176
    		String facetLabel = facetLabels[i];
177
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
178
    		if(facetDetailMap==null)
179
    			continue;
180
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 181
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
182
    		    String facet = facetEntry.getKey();
545 rajveer 183
    			String drilldownURL = url;  
184
      			drilldownURL += "&fq=" + facetDefID + ":" + 
354 rajveer 185
    				URLEncoder.encode(facet, "UTF-8"); 
186
      			String[] afacet = new String[] { facet, 
2948 chandransh 187
      			      facetEntry.getValue().toString(), drilldownURL  };
354 rajveer 188
      			values.add(afacet);    
189
 
190
    		}
191
 
192
    		this.facets.put(facetLabel, values);
193
    	}
194
 
195
    	Map<String, Double> priceMap = search.getPriceStatsMap();
517 rajveer 196
    	if(priceMap != null){
197
    		this.minPrice = priceMap.get("min");
198
    		this.maxPrice = priceMap.get("max");
199
    	}else{
200
    		this.minPrice = 0.0;
201
    		this.maxPrice = 0.0;
202
    	}
354 rajveer 203
 
545 rajveer 204
    	this.totalResults = search.getTotalResults();
3185 vikas 205
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
2157 vikas 206
                query, Long.toString(categoryId), Long.toString(totalResults));
820 rajveer 207
    	return "index";
339 rajveer 208
    }
517 rajveer 209
 
210
 
211
    public Map<String, String> getSnippets() throws Exception {
3273 vikas 212
        if(results != null){
213
            snippets = new HashMap<String, String>();   
214
            for(String docId: results){
3561 rajveer 215
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_SNIPPET_CACHE_KEY, docId, sourceId);
3273 vikas 216
                if (snippet != null) {
217
                    snippets.put(docId, snippet);
218
                }
219
            }
220
        }
221
        return snippets;
517 rajveer 222
    }
223
 
224
    /**
225
     * 
226
     * @return
227
     */
228
    public String getQuery() {
229
    	return this.query;
230
    }
339 rajveer 231
 
517 rajveer 232
 
339 rajveer 233
    /**
234
     * 
235
     * @return
236
     */
790 vikas 237
    public List<String> getResults() {
339 rajveer 238
    	return this.results;
239
    }
240
 
241
    /**
242
     * 
243
     * @return
244
     */
245
    public Map<String, List<String[]>> getFacets() {
246
    	return this.facets;
247
    }
248
 
545 rajveer 249
    public long getTotalResults(){
250
    	return totalResults;
251
    }
252
 
253
    public String getUrl(){
254
    	return this.url;
255
    }
256
 
257
    public String getPriceUrl(){
258
    	return this.priceUrl;
259
    }
260
 
569 rajveer 261
    public String getSortUrl(){
262
    	return this.sortUrl;
263
    }
264
 
545 rajveer 265
	public long getBeginIndex(){
266
		if(totalResults>0)
267
			return beginIndex+1;
268
		return beginIndex;
269
	}
270
 
271
	public long getTotalPages() {
1376 rajveer 272
		return 1 + (totalResults-1)/windowSize;
545 rajveer 273
	}
274
 
275
	public long getCurrentPage() {
276
		return this.page;
277
	}
278
 
354 rajveer 279
    public Double getMinPrice() {
280
    	return this.minPrice;
281
    }
282
 
283
    public Double getMaxPrice() {
284
    	return this.maxPrice;
285
    }
286
 
339 rajveer 287
    public List<String[]> getCrumbs() {
288
    	return this.crumbs;
289
    }
569 rajveer 290
 
786 rajveer 291
    public String getSortOrder(){
292
    	return this.sortOrder;
293
    }
820 rajveer 294
 
295
    public String getLocation(){
296
    	return this.location;
297
    }
339 rajveer 298
}