Subversion Repositories SmartDukaan

Rev

Rev 5873 | Rev 6931 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5873 Rev 6866
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.datalogger.EventType;
3
import in.shop2020.datalogger.EventType;
-
 
4
import in.shop2020.model.v1.catalog.Category;
4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
5
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
5
import in.shop2020.serving.cache.SnippetCacheWrapper;
6
import in.shop2020.serving.cache.SnippetCacheWrapper;
6
import in.shop2020.serving.services.SolrSearchService;
7
import in.shop2020.serving.services.SolrSearchService;
7
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.serving.utils.Utils;
-
 
9
import in.shop2020.utils.CategoryManager;
8
import in.shop2020.utils.DataLogger;
10
import in.shop2020.utils.DataLogger;
9
 
11
 
10
import java.io.UnsupportedEncodingException;
12
import java.io.UnsupportedEncodingException;
11
import java.net.URLEncoder;
13
import java.net.URLEncoder;
12
import java.util.ArrayList;
14
import java.util.ArrayList;
Line 45... Line 47...
45
	private Map<String, List<String[]>> facets = null;
47
	private Map<String, List<String[]>> facets = null;
46
	
48
	
47
	/**
49
	/**
48
	 * 
50
	 * 
49
	 */
51
	 */
50
	private List<String[]> crumbs;
52
	private Map<String, List<List<String>>> crumbs;
51
	
53
	
52
	private String  query;
54
	private String  query;
53
	private String sortOrder = null;
55
	private String sortOrder = null;
54
	private Double minPrice = null;
56
	private Double minPrice = null;
55
	private Double maxPrice = null;
57
	private Double maxPrice = null;
Line 58... Line 60...
58
	private long page = 1;
60
	private long page = 1;
59
	private long totalResults;
61
	private long totalResults;
60
	private long beginIndex = 0;
62
	private long beginIndex = 0;
61
	private String url;
63
	private String url;
62
	private String priceUrl = "?";
64
	private String priceUrl = "?";
63
	private long categoryId = Utils.ROOT_CATEGORY;
-
 
64
	private String sortUrl;
65
	private String sortUrl;
65
	private String location;
66
	private String location;
-
 
67
	private static HashMap<String, List<String>> categoriesChildren = null;
66
	
68
	
67
	/**
69
	/**
68
	 * 
70
	 * 
69
	 * @return
71
	 * @return
70
	 * @throws UnsupportedEncodingException
72
	 * @throws UnsupportedEncodingException
71
	 */
73
	 */
72
    // GET /query
74
    // GET /query
73
 
75
 
74
    public String index() throws UnsupportedEncodingException {
76
    public String index() throws Exception {
75
    	
77
    	
76
    	log.info("this.request=" + this.request);
78
    	log.info("this.request=" + this.request);
77
    	
79
    	
78
    	//url = this.request.getRequestURL();
80
    	//url = this.request.getRequestURL();
79
		query = this.request.getParameter("q");
81
		query = this.request.getParameter("q");
80
		url = "q="+ URLEncoder.encode(query, "UTF-8");
82
		url = "q="+ URLEncoder.encode(query, "UTF-8");
81
 
83
 
82
 
84
 
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
    	}
-
 
87
		if(query.trim().isEmpty()){
85
		if(query.trim().isEmpty()){
88
			if(categoryId == Utils.MOBILE_PHONES_CATEGORY){
-
 
89
				location = "/all-mobile-phones/" + categoryId;
-
 
90
				return "redirect";
-
 
91
			}else if(categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){
-
 
92
				location = "/all-mobile-accessories/" + categoryId;
-
 
93
				return "redirect";
86
				location = "/";
94
			} else if(categoryId == Utils.TABLETS_CATEGORY){
-
 
95
				location = "/all-tablets/" + categoryId;
-
 
96
				return "redirect";
-
 
97
			} else if(categoryId == Utils.LAPTOPS_CATEGORY){
-
 
98
                location = "/all-laptops/" + categoryId;
-
 
99
                return "redirect";
-
 
100
			} else if(categoryId == Utils.CAMERAS_CATEGORY){
-
 
101
				location = "/all-cameras/" + categoryId;
-
 
102
				return "redirect";
87
				return "redirect";
103
            }else{
-
 
104
				return "index";
-
 
105
			}
-
 
106
		}
88
		}
107
		sortUrl = url;
89
		sortUrl = url;
108
		if(this.request.getParameter("sort") != null){
90
		if(this.request.getParameter("sort") != null){
109
    		url= url + "&sort=" + this.request.getParameter("sort");
91
    		url= url + "&sort=" + this.request.getParameter("sort");
110
    		sortOrder = this.request.getParameter("sort");
92
    		sortOrder = this.request.getParameter("sort");
Line 124... Line 106...
124
    	}    	
106
    	}    	
125
    	
107
    	
126
    	
108
    	
127
    	String[] fqrys = this.request.getParameterValues("fq");
109
    	String[] fqrys = this.request.getParameterValues("fq");
128
    	
110
    	
129
    	this.crumbs = new ArrayList<String[]>();
111
    	this.crumbs = new HashMap<String, List<List<String>>>();
130
    	
112
    	
131
    	String[] facetDefIDs = Utils.facetDefIDs;
113
    	String[] facetDefIDs = Utils.facetDefIDs;
132
    	String[] facetLabels = Utils.facetLabels;
114
    	String[] facetLabels = Utils.facetLabels;
133
    	
115
    	
134
    	String urlCrumb = "";
116
    	String urlCrumb = "";
Line 139... Line 121...
139
    	}
121
    	}
140
    	
122
    	
141
    	if(fqrys != null) {
123
    	if(fqrys != null) {
142
	    	log.info("fqrys=" + Arrays.toString(fqrys));
124
	    	log.info("fqrys=" + Arrays.toString(fqrys));
143
	    	List<String> arrList = Arrays.asList(facetDefIDs);
125
	    	List<String> arrList = Arrays.asList(facetDefIDs);
-
 
126
	    	String filterUrl = "";
144
	    	for(int i=0; i<fqrys.length; i++){
127
	    	for(int i=0; i<fqrys.length; i++){
145
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
128
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
146
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
129
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
147
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
130
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
148
	    		StringBuilder filterUrl = new StringBuilder(url);
-
 
149
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
131
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
150
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
132
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
151
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];  
133
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
152
	    		for(int j=0; j<fqrys.length; j++) {
134
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
-
 
135
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
153
	    			if(i==j){
136
	    		if(!crumbs.containsKey(facetLabel)) {
154
	    				continue;
137
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
155
	    			}
138
	    		}
156
	    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));
-
 
157
		    	}
139
	    		
158
	    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
-
 
159
	    		log.info("acrumb=" + Arrays.toString(acrumb));
-
 
160
	    		this.crumbs.add(acrumb);
140
	    		this.crumbs.get(facetLabel).add(acrumb);
161
	    	}
141
	    	}
162
    	}
142
    	}
163
    	url = urlCrumb;
143
    	url = urlCrumb;
164
 
144
 
165
    	
145
    	
166
    	
146
    	
167
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder, sourceId);
147
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, sortOrder, sourceId);
168
 
148
 
169
    	this.results =  search.getResultMap(); 
149
    	this.results =  search.getResultMap(); 
170
    	
150
    	
171
    	// Facets
151
    	// Facets
-
 
152
    	List<String> fqs = null;
-
 
153
    	if (fqrys != null) {
-
 
154
    		fqs = Arrays.asList(fqrys);
-
 
155
    	}else {
-
 
156
    		fqs = new ArrayList<String>();
-
 
157
    	}
172
    	
158
    	
173
    	String qryString = this.request.getQueryString();
-
 
174
    	log.info("qryString=" + qryString);
159
		setFacet(fqs, search);
175
    	
160
    	
-
 
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{
176
    	String[] toshowfacetDefIDs;
178
    	String[] toshowfacetDefIDs;
177
    	String[] toshowfacetLabels;
179
    	String[] toshowfacetLabels;
178
    	
180
    	
179
    	if (categoryId == Utils.ROOT_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY ){
181
		if (fqs.contains("F_50010:Mobile Phones") || fqs.contains("F_50010:Cameras") || fqs.contains("F_50010:Tablets")){
180
    		toshowfacetDefIDs = Utils.rootfacetDefIDs;
182
    		toshowfacetDefIDs = Utils.facetDefIDs;
181
        	toshowfacetLabels = Utils.rootfacetLabels;
183
    		toshowfacetLabels = Utils.facetLabels;
182
    	}
184
    	}
183
    	else {
185
    	else {
184
    		toshowfacetDefIDs = Utils.facetDefIDs;
186
    		toshowfacetDefIDs = Utils.rootfacetDefIDs;
185
        	toshowfacetLabels = Utils.facetLabels;
187
    		toshowfacetLabels = Utils.rootfacetLabels;
186
    	}
188
    	}
187
    	
189
 
188
		this.facets = new LinkedHashMap<String, List<String[]>>();
190
    	this.facets = new LinkedHashMap<String, List<String[]>>();
189
    	for (int i=0; i<toshowfacetDefIDs.length; i++) {
191
    	for (int i=0; i<toshowfacetDefIDs.length; i++) {
190
    		String facetDefID = toshowfacetDefIDs[i];
192
    		String facetDefID = toshowfacetDefIDs[i];
191
    		String facetLabel = toshowfacetLabels[i];
193
    		String facetLabel = toshowfacetLabels[i];
-
 
194
    		
192
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
195
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
193
    		if(facetDetailMap==null)
196
    		if(facetDetailMap==null)
194
    			continue;
197
    			continue;
195
    		List<String[]> values = new ArrayList<String[]>();
198
    		List<String[]> values = new ArrayList<String[]>();
196
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
199
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
-
 
200
    			String selected = "";
-
 
201
    			if(fqs.contains(facetDefID + ":" + facetEntry.getKey())) {
-
 
202
    				selected = "checked=\"checked\"";
-
 
203
    			}
197
    		    String facet = facetEntry.getKey();
204
    		    String facet = facetEntry.getKey();
198
    			String drilldownURL = url;  
205
      			String drilldownURL = "&fq=" +  
199
      			drilldownURL += "&fq=" + facetDefID + ":" + 
-
 
200
    				URLEncoder.encode(facet, "UTF-8"); 
206
    				URLEncoder.encode( facetDefID + ":" + facet, "UTF-8"); 
201
      			String[] afacet = new String[] { facet, 
207
      			String[] afacet = new String[] { facet, 
202
      			      facetEntry.getValue().toString(), drilldownURL  };
208
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
203
      			values.add(afacet);    
209
      			values.add(afacet);    
204
      		  
-
 
205
    		}
210
    		}
206
    		
211
    		
207
    		this.facets.put(facetLabel, values);
212
    		this.facets.put(facetLabel, values);
208
    	}
213
    	}
209
    	
-
 
210
    	Map<String, Double> priceMap = search.getPriceStatsMap();
-
 
211
    	if(priceMap != null){
-
 
212
    		this.minPrice = priceMap.get("min");
-
 
213
    		this.maxPrice = priceMap.get("max");
-
 
214
    	}else{
-
 
215
    		this.minPrice = 0.0;
-
 
216
    		this.maxPrice = 0.0;
-
 
217
    	}
214
	}
218
    	
-
 
219
    	this.totalResults = search.getTotalResults();
-
 
220
        DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),
-
 
221
                query, Long.toString(categoryId), Long.toString(totalResults));
-
 
222
    	return "index";
-
 
223
    }
-
 
224
 
215
 
225
 
216
 
226
    public Map<String, String> getSnippets() throws Exception {
217
	public Map<String, String> getSnippets() throws Exception {
227
        if(results != null){
218
        if(results != null){
228
            snippets = new HashMap<String, String>();   
219
            snippets = new HashMap<String, String>();   
229
            for(String docId: results){
220
            for(String docId: results){
230
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_SNIPPET_CACHE_KEY, docId, sourceId);
221
                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_SNIPPET_CACHE_KEY, docId, sourceId);
231
                if (snippet != null) {
222
                if (snippet != null) {
Line 297... Line 288...
297
    
288
    
298
    public Double getMaxPrice() {
289
    public Double getMaxPrice() {
299
    	return this.maxPrice;
290
    	return this.maxPrice;
300
    }
291
    }
301
    
292
    
302
    public List<String[]> getCrumbs() {
293
    public Map<String, List<List<String>>> getCrumbs() {
303
    	return this.crumbs;
294
    	return this.crumbs;
304
    }
295
    }
305
 
296
 
306
    public String getSortOrder(){
297
    public String getSortOrder(){
307
    	return this.sortOrder;
298
    	return this.sortOrder;
308
    }
299
    }
309
    
300
    
310
    public String getLocation(){
301
    public String getLocation(){
311
    	return this.location;
302
    	return this.location;
312
    }
303
    }
-
 
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
    }
313
}
330
}