Subversion Repositories SmartDukaan

Rev

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