Subversion Repositories SmartDukaan

Rev

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