Subversion Repositories SmartDukaan

Rev

Rev 12108 | Rev 12391 | 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;
6866 amit.gupta 4
import in.shop2020.model.v1.catalog.Category;
3561 rajveer 5
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
6
import in.shop2020.serving.cache.SnippetCacheWrapper;
339 rajveer 7
import in.shop2020.serving.services.SolrSearchService;
637 rajveer 8
import in.shop2020.serving.utils.Utils;
6866 amit.gupta 9
import in.shop2020.utils.CategoryManager;
2511 vikas 10
import in.shop2020.utils.DataLogger;
339 rajveer 11
 
12
import java.io.UnsupportedEncodingException;
13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.Arrays;
354 rajveer 16
import java.util.HashMap;
339 rajveer 17
import java.util.LinkedHashMap;
18
import java.util.List;
19
import java.util.Map;
2948 chandransh 20
import java.util.Map.Entry;
339 rajveer 21
 
22
import org.apache.commons.lang.StringUtils;
832 rajveer 23
import org.apache.log4j.Logger;
820 rajveer 24
import org.apache.struts2.convention.annotation.Result;
339 rajveer 25
 
6931 amit.gupta 26
import com.google.gson.Gson;
1044 chandransh 27
 
6931 amit.gupta 28
 
339 rajveer 29
/**
545 rajveer 30
 * @author rajveer
339 rajveer 31
 *
32
 */
820 rajveer 33
@Result(name="redirect", location="${location}", type="redirect")
517 rajveer 34
public class SearchController extends BaseController {
339 rajveer 35
 
1044 chandransh 36
	private static final long serialVersionUID = -8392433517042806559L;
1999 vikas 37
	private static Logger log = Logger.getLogger(Class.class);
1044 chandransh 38
 
9840 amit.gupta 39
	private static HashMap<String, List<String>> categoriesChildren = null;
40
	private static Map<Long, String> categoryLabelMap = null;
41
 
790 vikas 42
	private List<String> results;
12141 amit.gupta 43
	private List<String> dealResults;
6931 amit.gupta 44
	private Map<String, Double> dynamicSearchMap;
517 rajveer 45
 
339 rajveer 46
	/**
47
	 * 
48
	 */
517 rajveer 49
	private Map<String, String> snippets;
339 rajveer 50
 
51
	/**
52
	 * 
53
	 */
545 rajveer 54
	private Map<String, List<String[]>> facets = null;
339 rajveer 55
 
56
	/**
57
	 * 
58
	 */
6866 amit.gupta 59
	private Map<String, List<List<String>>> crumbs;
339 rajveer 60
 
517 rajveer 61
	private String  query;
569 rajveer 62
	private String sortOrder = null;
545 rajveer 63
	private Double minPrice = null;
64
	private Double maxPrice = null;
354 rajveer 65
 
6942 amit.gupta 66
	private long windowSize = 20;
545 rajveer 67
	private long page = 1;
68
	private long totalResults;
69
	private long beginIndex = 0;
70
	private String url;
1921 vikas 71
	private String priceUrl = "?";
569 rajveer 72
	private String sortUrl;
820 rajveer 73
	private String location;
4137 varun.gupt 74
 
339 rajveer 75
	/**
76
	 * 
77
	 * @return
78
	 * @throws UnsupportedEncodingException
79
	 */
80
    // GET /query
820 rajveer 81
 
6866 amit.gupta 82
    public String index() throws Exception {
517 rajveer 83
 
637 rajveer 84
 
545 rajveer 85
		query = this.request.getParameter("q");
8327 amit.gupta 86
 
8324 amit.gupta 87
		url = "q="+ URLEncoder.encode(query, "UTF-8");
569 rajveer 88
 
89
 
820 rajveer 90
		if(query.trim().isEmpty()){
6866 amit.gupta 91
				location = "/";
820 rajveer 92
				return "redirect";
93
		}
569 rajveer 94
		sortUrl = url;
95
		if(this.request.getParameter("sort") != null){
96
    		url= url + "&sort=" + this.request.getParameter("sort");
97
    		sortOrder = this.request.getParameter("sort");
98
    	}
1921 vikas 99
		priceUrl += url;
545 rajveer 100
    	if(this.request.getParameter("page") != null){
101
    		this.page = Long.parseLong(this.request.getParameter("page"));
102
    		this.beginIndex = this.windowSize * (this.page-1);
536 rajveer 103
    	}
3173 rajveer 104
    	if(this.request.getParameter("minPrice") != null){
105
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
106
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
545 rajveer 107
    	}
3173 rajveer 108
    	if(this.request.getParameter("maxPrice") != null){
109
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
110
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
545 rajveer 111
    	}    	
339 rajveer 112
 
569 rajveer 113
 
339 rajveer 114
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 115
 
6866 amit.gupta 116
    	this.crumbs = new HashMap<String, List<List<String>>>();
339 rajveer 117
 
6998 amit.gupta 118
 
569 rajveer 119
    	String urlCrumb = "";
120
    	if(query != null) {
121
	    	urlCrumb = url; 
122
	    }else {
123
 
124
    	}
125
 
339 rajveer 126
    	if(fqrys != null) {
127
	    	log.info("fqrys=" + Arrays.toString(fqrys));
6866 amit.gupta 128
	    	String filterUrl = "";
569 rajveer 129
	    	for(int i=0; i<fqrys.length; i++){
130
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
131
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
132
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
545 rajveer 133
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
339 rajveer 134
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6998 amit.gupta 135
	    		String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
6866 amit.gupta 136
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
137
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
138
	    		if(!crumbs.containsKey(facetLabel)) {
139
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
140
	    		}
141
 
142
	    		this.crumbs.get(facetLabel).add(acrumb);
569 rajveer 143
	    	}
144
    	}
145
    	url = urlCrumb;
517 rajveer 146
 
147
 
8325 amit.gupta 148
    	log.info("Queried term:" + query);
12108 amit.gupta 149
    	SolrSearchService search = new SolrSearchService(URLEncoder.encode("\"" + query + "\"" + " OR " + query, "UTF-8"), fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, sortOrder, sourceId, userinfo.isPrivateDealUser());
790 vikas 150
 
354 rajveer 151
    	this.results =  search.getResultMap(); 
12141 amit.gupta 152
    	this.dealResults =  search.getDealMap();    	
6931 amit.gupta 153
    	this.dynamicSearchMap = search.getDynamicPriceMap();
545 rajveer 154
    	// Facets
6866 amit.gupta 155
    	List<String> fqs = null;
156
    	if (fqrys != null) {
157
    		fqs = Arrays.asList(fqrys);
158
    	}else {
159
    		fqs = new ArrayList<String>();
160
    	}
339 rajveer 161
 
6866 amit.gupta 162
		setFacet(fqs, search);
339 rajveer 163
 
6931 amit.gupta 164
		//If 
6866 amit.gupta 165
    	Map<String, Double> priceMap = search.getPriceStatsMap();
166
    	if(priceMap != null){
6931 amit.gupta 167
    		if (this.minPrice == null){
168
    			this.minPrice = priceMap.get("min");
169
    		}
170
    		if(this.maxPrice == null) {
171
    			this.maxPrice = priceMap.get("max");
172
    		}
173
    	} else{
6866 amit.gupta 174
    		this.minPrice = 0.0;
175
    		this.maxPrice = 0.0;
176
    	}
177
 
178
    	this.totalResults = search.getTotalResults();
179
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
180
                query, Long.toString(Utils.ROOT_CATEGORY), Long.toString(totalResults));
181
    	return "index";
182
    }
183
 
184
 
185
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
186
 
187
    	this.facets = new LinkedHashMap<String, List<String[]>>();
6998 amit.gupta 188
    	List<String> filtrableFacets = search.getFilterableFacets();
189
    	for (String filtrableFacet : filtrableFacets) {
6866 amit.gupta 190
 
6998 amit.gupta 191
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
354 rajveer 192
    		if(facetDetailMap==null)
193
    			continue;
194
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 195
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
6866 amit.gupta 196
    			String selected = "";
6998 amit.gupta 197
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
6866 amit.gupta 198
    				selected = "checked=\"checked\"";
199
    			}
2948 chandransh 200
    		    String facet = facetEntry.getKey();
6866 amit.gupta 201
      			String drilldownURL = "&fq=" +  
6998 amit.gupta 202
    				URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8"); 
354 rajveer 203
      			String[] afacet = new String[] { facet, 
6866 amit.gupta 204
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
354 rajveer 205
      			values.add(afacet);    
206
    		}
207
 
6998 amit.gupta 208
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
354 rajveer 209
    	}
6866 amit.gupta 210
	}
517 rajveer 211
 
212
 
6866 amit.gupta 213
	public Map<String, String> getSnippets() throws Exception {
3273 vikas 214
        if(results != null){
12141 amit.gupta 215
    		snippets = new HashMap<String, String>();	
216
    		if(userinfo.isPrivateDealUser()) {
217
    			for(String docId: results){
218
	    			if(dealResults.contains(docId)) {
219
	    				String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.PRIVATE_DEAL_SNIPPET_KEY, docId, sourceId);
220
	    				if (snippet != null) {
221
	    					snippets.put(docId, snippet);
222
	    					continue;
223
	    				}
224
	    			}
225
	    			String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
226
	    			if (snippet != null) {
227
	    				snippets.put(docId, snippet);
228
	    			}
229
	    		} 
230
			} else {
231
				for(String docId: results){
232
					String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
233
					if (snippet != null) {
234
						snippets.put(docId, snippet);
235
					}
236
				} 
237
 
238
			}
239
    	}
240
		return snippets;
517 rajveer 241
    }
242
 
243
    /**
244
     * 
245
     * @return
246
     */
247
    public String getQuery() {
248
    	return this.query;
249
    }
339 rajveer 250
 
517 rajveer 251
 
339 rajveer 252
    /**
253
     * 
254
     * @return
255
     */
790 vikas 256
    public List<String> getResults() {
339 rajveer 257
    	return this.results;
258
    }
259
 
260
    /**
261
     * 
262
     * @return
263
     */
264
    public Map<String, List<String[]>> getFacets() {
265
    	return this.facets;
266
    }
267
 
545 rajveer 268
    public long getTotalResults(){
269
    	return totalResults;
270
    }
271
 
272
    public String getUrl(){
273
    	return this.url;
274
    }
275
 
276
    public String getPriceUrl(){
277
    	return this.priceUrl;
278
    }
279
 
569 rajveer 280
    public String getSortUrl(){
281
    	return this.sortUrl;
282
    }
283
 
545 rajveer 284
	public long getBeginIndex(){
285
		if(totalResults>0)
286
			return beginIndex+1;
287
		return beginIndex;
288
	}
289
 
290
	public long getTotalPages() {
1376 rajveer 291
		return 1 + (totalResults-1)/windowSize;
545 rajveer 292
	}
293
 
294
	public long getCurrentPage() {
295
		return this.page;
296
	}
297
 
354 rajveer 298
    public Double getMinPrice() {
299
    	return this.minPrice;
300
    }
301
 
302
    public Double getMaxPrice() {
303
    	return this.maxPrice;
304
    }
305
 
6866 amit.gupta 306
    public Map<String, List<List<String>>> getCrumbs() {
339 rajveer 307
    	return this.crumbs;
308
    }
569 rajveer 309
 
786 rajveer 310
    public String getSortOrder(){
311
    	return this.sortOrder;
312
    }
820 rajveer 313
 
314
    public String getLocation(){
315
    	return this.location;
316
    }
6866 amit.gupta 317
 
318
    public List<String> getChildren(String categoryLabel) {
319
    	return getCategoriesChildren().get(categoryLabel);
320
    }
321
 
322
    public static Map<String, List<String>> getCategoriesChildren(){
323
    	Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
324
    	if (categoriesChildren == null) {
325
    		categoriesChildren = new HashMap<String, List<String>>();
326
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
327
    			idLabelMap.put(category.getId(), category.getLabel());
328
    		}
329
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
330
    			if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
331
    					category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
332
    				String parentLabel = idLabelMap.get(category.getParent_category_id());
333
    				if(!categoriesChildren.containsKey(parentLabel)){
334
    					categoriesChildren.put(parentLabel, new ArrayList<String>());
335
    				}
336
    				categoriesChildren.get(parentLabel).add(category.getLabel());
337
    			}
338
    		}
339
    	}
340
 
341
    	return categoriesChildren;
342
    }
6931 amit.gupta 343
 
6998 amit.gupta 344
    public static String getCategoryLabel(Long id){
345
    	if(categoryLabelMap == null){
346
    		Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
347
    		for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
348
    			idLabelMap.put(category.getId(), category.getLabel());
349
    		}
350
    		categoryLabelMap = idLabelMap;
351
    	} 
352
    	return categoryLabelMap.get(id);
353
    }
354
 
6931 amit.gupta 355
	public String getDynamicSearchMap(){
356
    	if (this.dynamicSearchMap == null) {
357
    		return "{}";
358
    	} else {
359
    		return new Gson().toJson(this.dynamicSearchMap);
360
    	}
361
    }
339 rajveer 362
}