Subversion Repositories SmartDukaan

Rev

Rev 6973 | 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
 
84
    	String[] facetLabels = Utils.facetLabels;
6430 amit.gupta 85
    	List<String> arrList = Arrays.asList(Utils.facetDefIDs);
3320 rajveer 86
 
2846 mandeep.dh 87
    	if(fqrys!= null){
6866 amit.gupta 88
    		log.info("fqrys=" + Arrays.toString(fqrys));
89
	    	String filterUrl = "";
90
	    	for(int i=0; i<fqrys.length; i++){
91
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
92
	    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6973 amit.gupta 93
	    		sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
6866 amit.gupta 94
	    		String facetName = StringUtils.split(fqrys[i], ":")[0];
95
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
96
	    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
97
	    		filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
98
	    		List<String> acrumb = Arrays.asList(facetValue, filterUrl);
99
	    		if(!crumbs.containsKey(facetLabel)) {
100
	    			crumbs.put(facetLabel, new ArrayList<List<String>>());
101
	    		}
102
 
103
	    		this.crumbs.get(facetLabel).add(acrumb);
104
	    	}
105
    	} else {
106
    		fqrys = new String[0];
2846 mandeep.dh 107
    	}
108
 
109
 
110
		url = urlCrumb;
111
 
6973 amit.gupta 112
		if(this.request.getParameter("minPrice") != null){
113
			this.minPrice = (new Double(this.request.getParameter("minPrice")));
114
			url= url + "&minPrice=" + this.request.getParameter("minPrice");
115
		}
116
 
117
		if(this.request.getParameter("maxPrice") != null){
118
			this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
119
			url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
120
		}
121
		sortUrl = url;
122
 
2846 mandeep.dh 123
    	if(this.request.getParameter("page") != null){
124
    		this.page = Long.parseLong(this.request.getParameter("page"));
125
    		this.beginIndex = this.windowSize * (this.page-1);
126
    	}
127
 
6973 amit.gupta 128
    	if(request.getParameter("sort")!=null){
6975 amit.gupta 129
    		url= url + "&sort=" + this.request.getParameter("sort");
6973 amit.gupta 130
    		sortOrder = this.request.getParameter("sort");
2846 mandeep.dh 131
    	}
132
 
3561 rajveer 133
 
6842 amit.gupta 134
    	if (type!=0) {
6923 amit.gupta 135
    		query = "*&fq=F_50029:"+query;
2846 mandeep.dh 136
    	}
6842 amit.gupta 137
 
138
 
6973 amit.gupta 139
    	SolrSearchService search = new SolrSearchService(query, fqrys, Utils.facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, "F_50028+asc," + sortOrder, sourceId);
6842 amit.gupta 140
    	this.results =  search.getResultMap(); 
6931 amit.gupta 141
    	this.dynamicSearchMap = search.getDynamicPriceMap();
6866 amit.gupta 142
    	setFacet(Arrays.asList(fqrys), search);
6842 amit.gupta 143
 
144
 
145
    	Map<String, Double> priceMap = search.getPriceStatsMap();
146
    	if(priceMap != null){
6942 amit.gupta 147
    		if (this.minPrice == null){
148
    			this.minPrice = priceMap.get("min");
149
    		}
150
    		if(this.maxPrice == null) {
151
    			this.maxPrice = priceMap.get("max");
152
    		}
6842 amit.gupta 153
    	}
154
    	else{
155
    		this.minPrice = 0.0;
156
    		this.maxPrice = 0.0;
157
    	}
158
 
159
    	this.totalResults = search.getTotalResults();
160
        return "index";
161
	}
2846 mandeep.dh 162
 
6842 amit.gupta 163
 
2846 mandeep.dh 164
    public String getPageMetaDesc() {
165
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
166
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
167
    }
168
 
3303 rajveer 169
 
2846 mandeep.dh 170
    public String getPageMetaKeywords() throws JSONException {
3303 rajveer 171
       	return SpecialPageConfigurer.getPageMetaKeywords(uri);
2846 mandeep.dh 172
    }
173
 
174
	public long getBeginIndex(){
175
		if(totalResults>0)
176
			return beginIndex+1;
177
		return beginIndex;
178
	}
179
 
180
	public long getTotalPages() {
181
		return 1 + (totalResults-1)/windowSize;
182
	}
183
 
3329 rajveer 184
    public Map<String, String> getSnippets(){
185
        if(results != null){
186
            snippets = new HashMap<String, String>();   
187
            for(String docId: results){
3561 rajveer 188
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3329 rajveer 189
                if (snippet != null) {
190
                    snippets.put(docId, snippet);
191
                }
192
            }
193
        }
194
        return snippets;
2846 mandeep.dh 195
    }
196
 
197
    public long getCurrentPage() {
198
        return this.page;
199
    }
200
 
201
    public Double getMinPrice() {
202
        return this.minPrice;
203
    }
204
 
205
    public Double getMaxPrice() {
206
        return this.maxPrice;
207
    }
208
 
6866 amit.gupta 209
    public Map<String,List<List<String>>> getCrumbs() {
2846 mandeep.dh 210
        return this.crumbs;
211
    }
212
    public String getSpecialPageTitle() {
213
        return this.specialPageTitle;
214
    }   
215
 
216
    public String getQuery() {
217
        return this.query;
218
    }
219
 
220
    public List<String> getResults() {
221
        return this.results;
222
    }
223
 
224
    public Map<String, List<String[]>> getFacets() {
225
        return this.facets;
226
    }
227
 
228
    public long getTotalResults(){
229
        return totalResults;
230
    }
231
 
232
    public String getUrl(){
233
        return this.url;
234
    }
235
 
236
    public String getPriceUrl(){
237
        return this.priceUrl;
238
    }
239
 
240
    public String getSpecialPageName() {
241
        return this.specialPageName;
6520 amit.gupta 242
    }
243
 
244
	public void addEntityId(Long entityId, List<String> snippets)	{
245
 
246
	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, entityId+"", sourceId);
247
 
248
	    if (snippet != null)	{
249
	        snippets.add(snippet);
250
	    }
251
	}
252
 
253
	public List<String> getTagSnippets() {
254
		CatalogClient catalogClientService;
255
		try {
256
			catalogClientService = new CatalogClient();
257
			Client cc = catalogClientService.getClient();
258
			for (long entityId : cc.getAllEntitiesByTagName(uri)) {
259
				addEntityId(entityId, tagSnippets);
260
			}
261
		} catch (Exception e) {
262
			// TODO Auto-generated catch block
263
			e.printStackTrace();
264
		}
265
		return tagSnippets;
266
	}
6866 amit.gupta 267
 
268
    private void setFacet(List<String> fqs, SolrSearchService search) throws Exception{
269
    	String[] toshowfacetDefIDs;
270
    	String[] toshowfacetLabels;
271
 
272
		toshowfacetDefIDs = Utils.facetDefIDs;
273
		toshowfacetLabels = Utils.facetLabels;
274
 
275
    	this.facets = new LinkedHashMap<String, List<String[]>>();
276
    	for (int i=0; i<toshowfacetDefIDs.length; i++) {
277
    		String facetDefID = toshowfacetDefIDs[i];
278
    		String facetLabel = toshowfacetLabels[i];
279
 
280
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
281
    		if(facetDetailMap==null)
282
    			continue;
283
    		List<String[]> values = new ArrayList<String[]>();
284
    		for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
285
    			String selected = "";
286
    			if(fqs.contains(facetDefID + ":" + facetEntry.getKey())) {
287
    				selected = "checked=\"checked\"";
288
    			}
289
    		    String facet = facetEntry.getKey();
290
      			String drilldownURL = "&fq=" +  
291
    				URLEncoder.encode( facetDefID + ":" + facet, "UTF-8"); 
292
      			String[] afacet = new String[] { facet, 
293
      			      facetEntry.getValue().toString(), drilldownURL,  selected};
294
      			values.add(afacet);    
295
    		}
296
 
297
    		this.facets.put(facetLabel, values);
298
    	}
299
	}
300
 
301
    public List<String> getChildren(String categoryLabel) {
302
    	return SearchController.getCategoriesChildren().get(categoryLabel);
303
    }
6931 amit.gupta 304
 
305
	public String getDynamicSearchMap(){
306
    	if (this.dynamicSearchMap == null) {
307
    		return "{}";
308
    	} else {
309
    		return new Gson().toJson(this.dynamicSearchMap);
310
    	}
311
    }
6973 amit.gupta 312
 
313
	public String getSortOrder(){
314
		return this.sortOrder;
315
	}
316
 
317
	public String getSortUrl(){
318
		return this.sortUrl;
319
	}
320
 
321
	public String getUri(){
322
		return this.uri;
323
	}
2846 mandeep.dh 324
}