Subversion Repositories SmartDukaan

Rev

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