Subversion Repositories SmartDukaan

Rev

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