Subversion Repositories SmartDukaan

Rev

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