Subversion Repositories SmartDukaan

Rev

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