Subversion Repositories SmartDukaan

Rev

Rev 2899 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2846 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.serving.services.SolrSearchService;
7
import in.shop2020.serving.utils.FileUtils;
8
import in.shop2020.serving.utils.Utils;
9
 
10
import java.io.BufferedReader;
11
import java.io.File;
12
import java.io.FileReader;
13
import java.io.IOException;
14
import java.net.URLEncoder;
15
import java.util.ArrayList;
16
import java.util.Arrays;
17
import java.util.HashMap;
18
import java.util.LinkedHashMap;
19
import java.util.List;
20
import java.util.Map;
21
 
22
import org.apache.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
24
import org.apache.struts2.convention.annotation.Action;
25
import org.apache.struts2.convention.annotation.Actions;
26
import org.apache.struts2.convention.annotation.Result;
27
import org.json.JSONException;
28
import org.json.JSONObject;
29
 
30
/**
31
 * @author rajveer
32
 *
33
 */
34
@Result(name = "index", location = "special-page-index.vm")
35
public class SpecialPageController extends BaseController {
36
    private static final long serialVersionUID = 1L;
37
    private static Logger log = Logger.getLogger(Class.class);
38
	private static Map<String, JSONObject> specialPagesDetails;
39
 
40
	private static final String[] facetDefIDs = 
41
	    new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
42
 
43
	private static final String[] facetLabels = 
44
	    new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
45
 
46
	static {
47
	    try {
48
	        loadSpecialPagesDetails();
49
	    }
50
	    catch (IOException e) {
51
            log.error("Could not load special pages details", e);
52
        }
53
	    catch (JSONException e) {
54
            log.error("Could not load special pages details", e);
55
        }
56
	}
57
 
58
	private List<String> results;
59
	private Map<String, String> snippets;
60
	private Map<String, List<String[]>> facets;
61
	private List<String[]> crumbs;
62
 
63
	private String specialPageName;
64
	private String specialPageTitle;
65
	private String facetSelection = "";
66
 
67
	private String query;
68
 
69
	private Double minPrice;
70
	private Double maxPrice;
71
 
72
	private long windowSize = 20;
73
	private long page = 1;
74
	private long totalResults;
75
	private long beginIndex = 0;
76
	private String url;
77
	private String priceUrl = "?";
78
 
79
	/**
80
	 * GET /abc/10004
81
	 * 
82
	 */
83
//    @Actions({ @Action("/nokia"),
84
//               @Action("/samsung"),
85
//               @Action("/spice"),
86
//               @Action("/blackberry"),
87
//               @Action("/sony-ericsson"),
88
//               @Action("/micromax"),
89
//               @Action("/lg"),
90
//               @Action("/htc"),
91
//               @Action("/motorola"),
92
//               @Action("/dell"),
93
//               @Action("/apple"),
94
//               @Action("/android"),
95
//               @Action("/brand") })
96
    public String index() throws SecurityException, IOException, JSONException {
97
	    String uri = request.getRequestURI();
98
	    log.info("Uri: " + uri);
99
	    specialPageName  = getSpecialPageNameFromUri(uri);
100
	    specialPageTitle = getSpecialPageTitleFromUri(uri);
101
	    query            = getQueryFromUri(uri);
102
 
103
    	String[] fqrys = this.request.getParameterValues("fq");
104
 
105
    	url = "?";
106
 
107
    	int length = 1;
108
 
109
    	if(fqrys!= null){
110
    		length += fqrys.length;
111
    	}
112
 
113
    	String[] newfqrys;
114
    	if(specialPageName.equals("Android")){
115
    		newfqrys = new String[length];
116
    	}
117
    	else {
118
    		newfqrys = new String[length+1];
119
    	}
120
 
121
    	if(specialPageName.equals("Android")){
122
    		newfqrys[0] = "F_50010:" + "(\"Mobile Phones\" OR Tablets)";
123
    	}
124
    	else {
125
    		newfqrys[0] = "F_50001:" + specialPageName;
126
    		newfqrys[length] = "F_50010:" + "(\"Mobile Phones\" OR Tablets)";
127
    	}
128
 
129
    	String urlCrumb = url;
130
 
131
    	this.crumbs = new ArrayList<String[]>();
132
    	List<String> arrList = Arrays.asList(facetDefIDs);
133
    	for(int i=1; i<length; i++) {
134
    		newfqrys[i] = fqrys[i-1];
135
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
136
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
137
    		String filterUrl = url;
138
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
139
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
140
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
141
    		this.specialPageTitle += " | " + facetLabel + " " + facetValue;
142
    		this.facetSelection += facetValue + " ";
143
    		for(int j=1; j<length; j++) {
144
    			if(i==j){
145
    				continue;
146
    			}
147
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
148
	    	}
149
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
150
    		log.info("acrumb=" + Arrays.toString(acrumb));
151
    		this.crumbs.add(acrumb);
152
    	}
153
 
154
		url = urlCrumb;
155
 
156
    	if(this.request.getParameter("page") != null){
157
    		this.page = Long.parseLong(this.request.getParameter("page"));
158
    		this.beginIndex = this.windowSize * (this.page-1);
159
    	}
160
 
161
    	if(this.request.getParameter("min-price") != null){
162
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
163
    		url= url + "&min-price=" + this.request.getParameter("min-price");
164
    	}
165
 
166
    	if(this.request.getParameter("max-price") != null){
167
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
168
    		url= url + "&max-price=" + this.request.getParameter("max-price");
169
    	}
170
 
171
    	query = query + "&sort=F_50002+asc";
172
 
173
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
174
    	this.results =  search.getResultMap(); 
175
 
176
 
177
    	this.facets = new LinkedHashMap<String, List<String[]>>();
178
    	String qryString = this.request.getQueryString();
179
    	log.info("qryString=" + qryString);
180
 
181
    	for (int i=0; i<facetDefIDs.length; i++) {
182
    		String facetDefID = facetDefIDs[i];
183
    		String facetLabel = facetLabels[i];
184
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
185
    		if(facetDetailMap==null)
186
    			continue;
187
    		List<String[]> values = new ArrayList<String[]>();
188
    		String drilldownURL = new String();
189
    		for(String facet: facetDetailMap.keySet()){
190
        		drilldownURL = url;
191
 
192
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
193
    				URLEncoder.encode(facet, "UTF-8");
194
 
195
    			String[] afacet = new String[] { facet, 
196
      					facetDetailMap.get(facet).toString(), drilldownURL  };
197
 
198
      			values.add(afacet);      			
199
    		}
200
 
201
    		this.facets.put(facetLabel, values);
202
    	}
203
 
204
    	Map<String, Double> priceMap = search.getPriceStatsMap();
205
    	if(priceMap != null){
206
    		this.minPrice = priceMap.get("min");
207
    		this.maxPrice = priceMap.get("max");
208
    	}
209
    	else{
210
    		this.minPrice = 0.0;
211
    		this.maxPrice = 0.0;
212
    	}
213
 
214
    	this.totalResults = search.getTotalResults();
215
        return "index";
216
    }
217
 
218
    public String getPageMetaDesc() {
219
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
220
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
221
    }
222
 
223
    public String getPageMetaKeywords() throws JSONException {
224
       	return specialPagesDetails.get(request.getRequestURI()).getString("metaKeywords");
225
    }
226
 
227
	public long getBeginIndex(){
228
		if(totalResults>0)
229
			return beginIndex+1;
230
		return beginIndex;
231
	}
232
 
233
	public long getTotalPages() {
234
		return 1 + (totalResults-1)/windowSize;
235
	}
236
 
237
	public Map<String, String> getSnippets(){
238
    	if(results != null){
239
    		snippets = new HashMap<String, String>();	
240
	    	for(String docId: results){
241
				try {
242
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
243
				} catch (IOException e) {
244
					log.error(e);
245
				}
246
			}
247
    	}
248
		return snippets;
249
    }
250
 
251
    /**
252
     * Loads all special pages related details
253
     */
254
    private static void loadSpecialPagesDetails() throws IOException, JSONException {
255
        FileReader fr = new FileReader(Utils.EXPORT_ENTITIES_PATH + "../../javascripts/special-pages.json");
256
        BufferedReader br = new BufferedReader(fr);
257
        String str = null;
258
 
259
        specialPagesDetails = new HashMap<String, JSONObject>();
260
        while ((str = br.readLine()) != null) {
261
            JSONObject jsonObject = new JSONObject(str);
262
            specialPagesDetails.put(jsonObject.getString("saholicURL"),
263
                    jsonObject);
264
        }
265
 
266
        log.info(specialPagesDetails);
267
    }
268
 
269
    /**
270
     * Returns true in case a string passed is a special page name
271
     */
272
    public static boolean isValidSpecialPage(String specialPage) {
273
        if (specialPage == null) {
274
            return false;
275
        }
276
 
277
        return specialPagesDetails.containsKey(specialPage);
278
    }
279
 
280
    private String getQueryFromUri(String uri) throws JSONException {
281
        return specialPagesDetails.get(uri).getString("searchQuery");
282
    }
283
 
284
    private String getSpecialPageTitleFromUri(String uri) throws JSONException {
285
        return specialPagesDetails.get(uri).getString("pageTitle");
286
    }
287
 
288
    private String getSpecialPageNameFromUri(String uri) throws JSONException {
289
        return specialPagesDetails.get(uri).getString("displayName");
290
    }
291
 
292
    public long getCurrentPage() {
293
        return this.page;
294
    }
295
 
296
    public Double getMinPrice() {
297
        return this.minPrice;
298
    }
299
 
300
    public Double getMaxPrice() {
301
        return this.maxPrice;
302
    }
303
 
304
    public List<String[]> getCrumbs() {
305
        return this.crumbs;
306
    }
307
    public String getSpecialPageTitle() {
308
        return this.specialPageTitle;
309
    }   
310
 
311
    public String getQuery() {
312
        return this.query;
313
    }
314
 
315
    public List<String> getResults() {
316
        return this.results;
317
    }
318
 
319
    public Map<String, List<String[]>> getFacets() {
320
        return this.facets;
321
    }
322
 
323
    public long getTotalResults(){
324
        return totalResults;
325
    }
326
 
327
    public String getUrl(){
328
        return this.url;
329
    }
330
 
331
    public String getPriceUrl(){
332
        return this.priceUrl;
333
    }
334
 
335
    public String getSpecialPageName() {
336
        return this.specialPageName;
337
    }   
338
}