Subversion Repositories SmartDukaan

Rev

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