Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2846 mandeep.dh 1
package in.shop2020.serving.controllers;
2
 
6520 amit.gupta 3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
3561 rajveer 5
import in.shop2020.serving.cache.SnippetCacheWrapper;
2846 mandeep.dh 6
import in.shop2020.serving.services.SolrSearchService;
3303 rajveer 7
import in.shop2020.serving.services.SpecialPageConfigurer;
6430 amit.gupta 8
import in.shop2020.serving.utils.Utils;
6520 amit.gupta 9
import in.shop2020.thrift.clients.CatalogClient;
2846 mandeep.dh 10
 
11
import java.net.URLEncoder;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.LinkedHashMap;
16
import java.util.List;
17
import java.util.Map;
2948 chandransh 18
import java.util.Map.Entry;
2846 mandeep.dh 19
 
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.apache.struts2.convention.annotation.Result;
6520 amit.gupta 23
import org.apache.struts2.convention.annotation.Results;
2846 mandeep.dh 24
import org.json.JSONException;
25
 
6931 amit.gupta 26
import com.google.gson.Gson;
27
 
2846 mandeep.dh 28
/**
29
 * @author rajveer
30
 *
31
 */
6520 amit.gupta 32
@Results({
33
	@Result(name = "index", location = "special-page-index.vm"),
34
	@Result(name = "tag-index", location = "tag-page-index.vm")
35
})
2846 mandeep.dh 36
public class SpecialPageController extends BaseController {
37
    private static final long serialVersionUID = 1L;
38
    private static Logger log = Logger.getLogger(Class.class);
39
 
40
	private List<String> results;
41
	private Map<String, String> snippets;
42
	private Map<String, List<String[]>> facets;
6866 amit.gupta 43
	private Map<String, List<List<String>>> crumbs;
6520 amit.gupta 44
	private List<String> tagSnippets = new ArrayList<String>();
2846 mandeep.dh 45
	private String specialPageName;
46
	private String specialPageTitle;
47
	private String facetSelection = "";
6931 amit.gupta 48
 
49
	private Map<String, Double> dynamicSearchMap = null;
2846 mandeep.dh 50
 
51
	private String query;
52
 
6942 amit.gupta 53
	private Double minPrice =null;
54
	private Double maxPrice = null;
2846 mandeep.dh 55
 
6520 amit.gupta 56
	private long type = 0;
2846 mandeep.dh 57
	private long windowSize = 20;
58
	private long page = 1;
59
	private long totalResults;
60
	private long beginIndex = 0;
61
	private String url;
6973 amit.gupta 62
	private String sortOrder = "F_50030+desc";
63
	private String sortUrl = "?";
2846 mandeep.dh 64
	private String priceUrl = "?";
3303 rajveer 65
	private String uri;
66
 
6866 amit.gupta 67
	public String index() throws Exception {
3303 rajveer 68
		uri = request.getRequestURI();
3304 rajveer 69
		uri = uri.replace("/", "");
2846 mandeep.dh 70
	    log.info("Uri: " + uri);
3303 rajveer 71
	    specialPageName  = SpecialPageConfigurer.getSpecialPageNameFromUri(uri);
72
	    specialPageTitle = SpecialPageConfigurer.getSpecialPageTitleFromUri(uri);
73
	    query            = SpecialPageConfigurer.getQueryFromUri(uri);
6520 amit.gupta 74
	    type            =  SpecialPageConfigurer.getPageType(uri);
2846 mandeep.dh 75
 
76
    	String[] fqrys = this.request.getParameterValues("fq");
77
 
78
    	url = "?";
79
 
3320 rajveer 80
    	String urlCrumb = url;
2846 mandeep.dh 81
 
6866 amit.gupta 82
    	this.crumbs = new HashMap<String, List<List<String>>>();
83
 
2846 mandeep.dh 84
    	if(fqrys!= null){
6866 amit.gupta 85
    		log.info("fqrys=" + Arrays.toString(fqrys));
86
	    	String filterUrl = "";
87
	    	for(int i=0; i<fqrys.length; i++){
88
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
89
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6973 amit.gupta 90
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6866 amit.gupta 91
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
92
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6998 amit.gupta 93
	    		String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
6866 amit.gupta 94
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
95
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
96
	    		if(!crumbs.containsKey(facetLabel)) {
97
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
98
	    		}
99
 
100
	    		this.crumbs.get(facetLabel).add(acrumb);
101
	    	}
102
    	} else {
103
    		fqrys = new String[0];
2846 mandeep.dh 104
    	}
105
 
106
 
107
		url = urlCrumb;
108
 
6973 amit.gupta 109
		if(this.request.getParameter("minPrice") != null){
110
			this.minPrice = (new Double(this.request.getParameter("minPrice")));
111
			url= url + "&minPrice=" + this.request.getParameter("minPrice");
112
		}
113
 
114
		if(this.request.getParameter("maxPrice") != null){
115
			this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
116
			url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
117
		}
118
		sortUrl = url;
119
 
2846 mandeep.dh 120
    	if(this.request.getParameter("page") != null){
121
    		this.page = Long.parseLong(this.request.getParameter("page"));
122
    		this.beginIndex = this.windowSize * (this.page-1);
123
    	}
124
 
6973 amit.gupta 125
    	if(request.getParameter("sort")!=null){
6975 amit.gupta 126
    		url= url + "&sort=" + this.request.getParameter("sort");
6973 amit.gupta 127
    		sortOrder = this.request.getParameter("sort");
2846 mandeep.dh 128
    	}
129
 
3561 rajveer 130
 
6842 amit.gupta 131
    	if (type!=0) {
6923 amit.gupta 132
    		query = "*&fq=F_50029:"+query;
2846 mandeep.dh 133
    	}
6842 amit.gupta 134
 
135
 
12108 amit.gupta 136
    	SolrSearchService search = new SolrSearchService(query, fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, "F_50028+asc," + sortOrder, sourceId, userinfo.isPrivateDealUser());
6842 amit.gupta 137
    	this.results =  search.getResultMap(); 
6931 amit.gupta 138
    	this.dynamicSearchMap = search.getDynamicPriceMap();
6866 amit.gupta 139
    	setFacet(Arrays.asList(fqrys), search);
6842 amit.gupta 140
 
141
 
142
    	Map<String, Double> priceMap = search.getPriceStatsMap();
143
    	if(priceMap != null){
6942 amit.gupta 144
    		if (this.minPrice == null){
145
    			this.minPrice = priceMap.get("min");
146
    		}
147
    		if(this.maxPrice == null) {
148
    			this.maxPrice = priceMap.get("max");
149
    		}
6842 amit.gupta 150
    	}
151
    	else{
152
    		this.minPrice = 0.0;
153
    		this.maxPrice = 0.0;
154
    	}
155
 
156
    	this.totalResults = search.getTotalResults();
157
        return "index";
158
	}
2846 mandeep.dh 159
 
6842 amit.gupta 160
 
2846 mandeep.dh 161
    public String getPageMetaDesc() {
162
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
163
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
164
    }
165
 
3303 rajveer 166
 
2846 mandeep.dh 167
    public String getPageMetaKeywords() throws JSONException {
3303 rajveer 168
       	return SpecialPageConfigurer.getPageMetaKeywords(uri);
2846 mandeep.dh 169
    }
170
 
171
	public long getBeginIndex(){
172
		if(totalResults>0)
173
			return beginIndex+1;
174
		return beginIndex;
175
	}
176
 
177
	public long getTotalPages() {
178
		return 1 + (totalResults-1)/windowSize;
179
	}
180
 
3329 rajveer 181
    public Map<String, String> getSnippets(){
182
        if(results != null){
183
            snippets = new HashMap<String, String>();   
184
            for(String docId: results){
3561 rajveer 185
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3329 rajveer 186
                if (snippet != null) {
187
                    snippets.put(docId, snippet);
188
                }
189
            }
190
        }
191
        return snippets;
2846 mandeep.dh 192
    }
193
 
194
    public long getCurrentPage() {
195
        return this.page;
196
    }
197
 
198
    public Double getMinPrice() {
199
        return this.minPrice;
200
    }
201
 
202
    public Double getMaxPrice() {
203
        return this.maxPrice;
204
    }
205
 
6866 amit.gupta 206
    public Map<String,List<List<String>>> getCrumbs() {
2846 mandeep.dh 207
        return this.crumbs;
208
    }
209
    public String getSpecialPageTitle() {
210
        return this.specialPageTitle;
211
    }   
212
 
213
    public String getQuery() {
214
        return this.query;
215
    }
216
 
217
    public List<String> getResults() {
218
        return this.results;
219
    }
220
 
221
    public Map<String, List<String[]>> getFacets() {
222
        return this.facets;
223
    }
224
 
225
    public long getTotalResults(){
226
        return totalResults;
227
    }
228
 
229
    public String getUrl(){
230
        return this.url;
231
    }
232
 
233
    public String getPriceUrl(){
234
        return this.priceUrl;
235
    }
236
 
237
    public String getSpecialPageName() {
238
        return this.specialPageName;
6520 amit.gupta 239
    }
240
 
241
	public void addEntityId(Long entityId, List<String> snippets)	{
242
 
243
	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, entityId+"", sourceId);
244
 
245
	    if (snippet != null)	{
246
	        snippets.add(snippet);
247
	    }
248
	}
249
 
250
	public List<String> getTagSnippets() {
251
		CatalogClient catalogClientService;
252
		try {
253
			catalogClientService = new CatalogClient();
254
			Client cc = catalogClientService.getClient();
255
			for (long entityId : cc.getAllEntitiesByTagName(uri)) {
256
				addEntityId(entityId, tagSnippets);
257
			}
258
		} catch (Exception e) {
259
			// TODO Auto-generated catch block
260
			e.printStackTrace();
261
		}
262
		return tagSnippets;
263
	}
6866 amit.gupta 264
 
265
 
266
    public List<String> getChildren(String categoryLabel) {
267
    	return SearchController.getCategoriesChildren().get(categoryLabel);
268
    }
6931 amit.gupta 269
 
270
	public String getDynamicSearchMap(){
271
    	if (this.dynamicSearchMap == null) {
272
    		return "{}";
273
    	} else {
274
    		return new Gson().toJson(this.dynamicSearchMap);
275
    	}
276
    }
6973 amit.gupta 277
 
278
	public String getSortOrder(){
279
		return this.sortOrder;
280
	}
281
 
282
	public String getSortUrl(){
283
		return this.sortUrl;
284
	}
285
 
286
	public String getUri(){
287
		return this.uri;
288
	}
6998 amit.gupta 289
 
290
	private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
291
 
292
    	this.facets = new LinkedHashMap<String, List<String[]>>();
293
    	List<String> filtrableFacets = search.getFilterableFacets();
294
    	for (String filtrableFacet : filtrableFacets) {
295
 
296
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
297
    		if(facetDetailMap==null)
298
    			continue;
299
    		List<String[]> values = new ArrayList<String[]>();
300
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
301
    			String selected = "";
302
    			if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
303
    				selected = "checked=\"checked\"";
304
    			}
305
    		    String facet = facetEntry.getKey();
306
      			String drilldownURL = "&fq=" +  
307
    				URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8"); 
308
      			String[] afacet = new String[] { facet, 
309
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
310
      			values.add(afacet);    
311
    		}
312
 
313
    		this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
314
    	}
315
	}
2846 mandeep.dh 316
}