Subversion Repositories SmartDukaan

Rev

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