Subversion Repositories SmartDukaan

Rev

Rev 3561 | Rev 6430 | 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
 
3561 rajveer 3
import in.shop2020.serving.cache.SnippetCacheWrapper;
4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
2846 mandeep.dh 5
import in.shop2020.serving.services.SolrSearchService;
3303 rajveer 6
import in.shop2020.serving.services.SpecialPageConfigurer;
2846 mandeep.dh 7
 
8
import java.io.IOException;
9
import java.net.URLEncoder;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.HashMap;
13
import java.util.LinkedHashMap;
14
import java.util.List;
15
import java.util.Map;
2948 chandransh 16
import java.util.Map.Entry;
2846 mandeep.dh 17
 
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.log4j.Logger;
20
import org.apache.struts2.convention.annotation.Result;
21
import org.json.JSONException;
22
 
23
/**
24
 * @author rajveer
25
 *
26
 */
27
@Result(name = "index", location = "special-page-index.vm")
28
public class SpecialPageController extends BaseController {
29
    private static final long serialVersionUID = 1L;
30
    private static Logger log = Logger.getLogger(Class.class);
31
 
32
	private static final String[] facetDefIDs = 
33
	    new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
34
 
35
	private static final String[] facetLabels = 
36
	    new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
37
 
38
	private List<String> results;
39
	private Map<String, String> snippets;
40
	private Map<String, List<String[]>> facets;
41
	private List<String[]> crumbs;
42
 
43
	private String specialPageName;
44
	private String specialPageTitle;
45
	private String facetSelection = "";
46
 
47
	private String query;
48
 
49
	private Double minPrice;
50
	private Double maxPrice;
51
 
52
	private long windowSize = 20;
53
	private long page = 1;
54
	private long totalResults;
55
	private long beginIndex = 0;
56
	private String url;
57
	private String priceUrl = "?";
3303 rajveer 58
	private String uri;
59
 
60
	public String index() throws SecurityException, IOException, JSONException {
61
		uri = request.getRequestURI();
3304 rajveer 62
		uri = uri.replace("/", "");
2846 mandeep.dh 63
	    log.info("Uri: " + uri);
3303 rajveer 64
	    specialPageName  = SpecialPageConfigurer.getSpecialPageNameFromUri(uri);
65
	    specialPageTitle = SpecialPageConfigurer.getSpecialPageTitleFromUri(uri);
66
	    query            = SpecialPageConfigurer.getQueryFromUri(uri);
2846 mandeep.dh 67
 
68
    	String[] fqrys = this.request.getParameterValues("fq");
69
 
70
    	url = "?";
71
 
3320 rajveer 72
    	String urlCrumb = url;
2846 mandeep.dh 73
 
3320 rajveer 74
    	this.crumbs = new ArrayList<String[]>();
75
    	List<String> arrList = Arrays.asList(facetDefIDs);
76
 
2846 mandeep.dh 77
    	if(fqrys!= null){
3320 rajveer 78
    		for(int i=0; i<fqrys.length; i++) {
79
        		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
80
        		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
81
        		String filterUrl = url;
82
        		String facetName = StringUtils.split(fqrys[i], ":")[0];
83
        		String facetValue = StringUtils.split(fqrys[i], ":")[1];
84
        		String facetLabel = facetLabels[arrList.indexOf(facetName)];
85
        		this.specialPageTitle += " | " + facetLabel + " " + facetValue;
86
        		this.facetSelection += facetValue + " ";
87
        		for(int j=0; j<fqrys.length; j++) {
88
        			if(i==j){
89
        				continue;
90
        			}
91
        			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
92
    	    	}
93
        		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
94
        		log.info("acrumb=" + Arrays.toString(acrumb));
95
        		this.crumbs.add(acrumb);
96
        	}
2846 mandeep.dh 97
    	}
98
 
99
 
100
		url = urlCrumb;
101
 
102
    	if(this.request.getParameter("page") != null){
103
    		this.page = Long.parseLong(this.request.getParameter("page"));
104
    		this.beginIndex = this.windowSize * (this.page-1);
105
    	}
106
 
3173 rajveer 107
    	if(this.request.getParameter("minPrice") != null){
108
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
109
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
2846 mandeep.dh 110
    	}
111
 
3173 rajveer 112
    	if(this.request.getParameter("maxPrice") != null){
113
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
114
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
2846 mandeep.dh 115
    	}
116
 
3561 rajveer 117
    	String sortOrder = "F_50002+asc";
118
 
119
    	SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, sortOrder, sourceId);
2846 mandeep.dh 120
    	this.results =  search.getResultMap(); 
121
 
122
 
123
    	this.facets = new LinkedHashMap<String, List<String[]>>();
124
    	String qryString = this.request.getQueryString();
125
    	log.info("qryString=" + qryString);
126
 
127
    	for (int i=0; i<facetDefIDs.length; i++) {
128
    		String facetDefID = facetDefIDs[i];
129
    		String facetLabel = facetLabels[i];
5729 amit.gupta 130
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
2846 mandeep.dh 131
    		if(facetDetailMap==null)
132
    			continue;
133
    		List<String[]> values = new ArrayList<String[]>();
2948 chandransh 134
    		String drilldownURL = url + "&fq=" + facetDefID + ":";
135
    		for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
136
    		    String facet = facetEntry.getKey();
2846 mandeep.dh 137
 
138
    			String[] afacet = new String[] { facet, 
3320 rajveer 139
      					facetEntry.getValue().toString(), drilldownURL  + URLEncoder.encode(facet, "UTF-8")  };
2846 mandeep.dh 140
 
2948 chandransh 141
      			values.add(afacet);
2846 mandeep.dh 142
    		}
143
 
144
    		this.facets.put(facetLabel, values);
145
    	}
146
 
147
    	Map<String, Double> priceMap = search.getPriceStatsMap();
148
    	if(priceMap != null){
149
    		this.minPrice = priceMap.get("min");
150
    		this.maxPrice = priceMap.get("max");
151
    	}
152
    	else{
153
    		this.minPrice = 0.0;
154
    		this.maxPrice = 0.0;
155
    	}
156
 
157
    	this.totalResults = search.getTotalResults();
158
        return "index";
159
    }
160
 
161
    public String getPageMetaDesc() {
162
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
163
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
164
    }
165
 
3303 rajveer 166
 
2846 mandeep.dh 167
    public String getPageMetaKeywords() throws JSONException {
3303 rajveer 168
       	return SpecialPageConfigurer.getPageMetaKeywords(uri);
2846 mandeep.dh 169
    }
170
 
171
	public long getBeginIndex(){
172
		if(totalResults>0)
173
			return beginIndex+1;
174
		return beginIndex;
175
	}
176
 
177
	public long getTotalPages() {
178
		return 1 + (totalResults-1)/windowSize;
179
	}
180
 
3329 rajveer 181
    public Map<String, String> getSnippets(){
182
        if(results != null){
183
            snippets = new HashMap<String, String>();   
184
            for(String docId: results){
3561 rajveer 185
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3329 rajveer 186
                if (snippet != null) {
187
                    snippets.put(docId, snippet);
188
                }
189
            }
190
        }
191
        return snippets;
2846 mandeep.dh 192
    }
193
 
194
    public long getCurrentPage() {
195
        return this.page;
196
    }
197
 
198
    public Double getMinPrice() {
199
        return this.minPrice;
200
    }
201
 
202
    public Double getMaxPrice() {
203
        return this.maxPrice;
204
    }
205
 
206
    public List<String[]> getCrumbs() {
207
        return this.crumbs;
208
    }
209
    public String getSpecialPageTitle() {
210
        return this.specialPageTitle;
211
    }   
212
 
213
    public String getQuery() {
214
        return this.query;
215
    }
216
 
217
    public List<String> getResults() {
218
        return this.results;
219
    }
220
 
221
    public Map<String, List<String[]>> getFacets() {
222
        return this.facets;
223
    }
224
 
225
    public long getTotalResults(){
226
        return totalResults;
227
    }
228
 
229
    public String getUrl(){
230
        return this.url;
231
    }
232
 
233
    public String getPriceUrl(){
234
        return this.priceUrl;
235
    }
236
 
237
    public String getSpecialPageName() {
238
        return this.specialPageName;
239
    }   
240
}