Subversion Repositories SmartDukaan

Rev

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