Subversion Repositories SmartDukaan

Rev

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