Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1923 rajveer 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.File;
11
import java.io.IOException;
12
import java.net.URLEncoder;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.HashMap;
16
import java.util.LinkedHashMap;
17
import java.util.List;
18
import java.util.Map;
19
 
20
import org.apache.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.apache.struts2.convention.annotation.Action;
23
import org.apache.struts2.convention.annotation.Actions;
24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.rest.DefaultHttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
27
 
28
/**
29
 * @author rajveer
30
 *
31
 */
32
@Result(name = "index", location = "brand-index.vm")
33
public class BrandController extends BaseController {
34
 
35
	private static final long serialVersionUID = 8477108528765030321L;
36
 
37
	private static Logger log = Logger.getLogger(Class.class);
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
	 * 
45
	 */
46
	private String brandName;
47
	private String brandTitle;
48
	private String facetSelection = "";
49
 
50
	private String query;
51
 
52
	private Double minPrice;
53
	private Double maxPrice;
54
 
55
 
56
	private long windowSize = 20;
57
	private long page = 1;
58
	private long totalResults;
59
	private long beginIndex = 0;
60
	private String url;
61
	private String priceUrl;
62
 
63
	/**
64
	 * GET /abc/10004
65
	 * 
66
	 */
67
	@Actions({
68
	    @Action("/nokia"),
69
	    @Action("/samsung"),
70
		@Action("/spice"),
71
		@Action("/blackBerry"),
72
		@Action("/sony-ericsson"),
73
		@Action("/micromax"),
74
		@Action("/lg"),
75
		@Action("/htc"),
76
		@Action("/motorola"),
77
		@Action("/dell"),
78
 
79
		@Action("/brand")
80
	})
81
 
82
    public String index() throws SecurityException, IOException {
83
 
84
	    String uri = request.getRequestURI();
85
	    this.brandName = uri.replace("/", "");
86
 
87
    	String[] parts = this.brandName.split("-");
88
    	StringBuffer brand = new StringBuffer(); 
89
    	for(int i=0; i < parts.length; i++){
90
    	    if(i>0)
91
    	    brand.append(" ");
92
    	    brand.append(Character.toUpperCase(parts[i].charAt(0)));
93
    	    brand.append(parts[i].substring(1)); 
94
    	}
95
    	this.brandName = brand.toString();
96
    	this.brandTitle = this.brandName + " Mobile Price List India" ;
97
 
98
 
99
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
100
        String[] facetLabels = new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
101
 
102
    	String[] fqrys = this.request.getParameterValues("fq");
103
 
104
    	url = "?";
105
 
106
 
107
 
108
    	query = "*";
109
 
110
    	int length = 1;
111
 
112
    	if(fqrys!= null){
113
    		length += fqrys.length;
114
    	}
115
 
116
 
117
    	String[] newfqrys = new String[length+1];
118
 
119
    	newfqrys[0] = "F_50001:" + brandName;
120
 
121
 
122
    	String urlCrumb = url;
123
 
124
    	this.crumbs = new ArrayList<String[]>();
125
    	List<String> arrList = Arrays.asList(facetDefIDs);
126
    	for(int i=1; i<length; i++) {
127
    		newfqrys[i] = fqrys[i-1];
128
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
129
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
130
    		String filterUrl = url;
131
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
132
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
133
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
134
    		this.brandTitle += " | " + facetLabel + " " + facetValue;
135
    		this.facetSelection += facetValue + " ";
136
    		for(int j=1; j<length; j++) {
137
    			if(i==j){
138
    				continue;
139
    			}
140
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
141
	    	}
142
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
143
    		log.info("acrumb=" + Arrays.toString(acrumb));
144
    		this.crumbs.add(acrumb);
145
 
146
    	}
147
 
148
    	newfqrys[length] = "F_50010:" + "Mobile Phones";
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
    	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
    	if(this.request.getParameter("max-price") != null){
161
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
162
    		url= url + "&max-price=" + this.request.getParameter("max-price");
163
    	}    	
164
 
165
    	query = query + "&sort=F_50002+asc";
166
 
167
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
168
    	this.results =  search.getResultMap(); 
169
 
170
 
171
    	this.facets = new LinkedHashMap<String, List<String[]>>();
172
    	String qryString = this.request.getQueryString();
173
    	log.info("qryString=" + qryString);
174
 
175
    	for (int i=0; i<facetDefIDs.length; i++) {
176
    		String facetDefID = facetDefIDs[i];
177
    		String facetLabel = facetLabels[i];
178
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
179
    		if(facetDetailMap==null)
180
    			continue;
181
    		List<String[]> values = new ArrayList<String[]>();
182
    		String drilldownURL = new String();
183
    		for(String facet: facetDetailMap.keySet()){
184
 
185
    			/*
186
    			if(qryString != null){
187
    				drilldownURL = qryString;
188
 
189
    			}
190
    			*/
191
        		drilldownURL = url;
192
 
193
 
194
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
195
    				URLEncoder.encode(facet, "UTF-8");
196
      			String[] afacet = new String[] { facet, 
197
      					facetDetailMap.get(facet).toString(), drilldownURL  };
198
      			values.add(afacet);    
199
 
200
    		}
201
 
202
    		this.facets.put(facetLabel, values);
203
    	}
204
 
205
 
206
 
207
    	Map<String, Double> priceMap = search.getPriceStatsMap();
208
    	if(priceMap != null){
209
    		this.minPrice = priceMap.get("min");
210
    		this.maxPrice = priceMap.get("max");
211
    	}else{
212
    		this.minPrice = 0.0;
213
    		this.maxPrice = 0.0;
214
    	}
215
    	this.totalResults = search.getTotalResults();
216
        return "index";
217
    }
218
 
219
 
220
    public String getBrandName() {
221
    	return this.brandName;
222
    }	
223
 
224
    public String getPageMetaDesc() {
225
    	return "Best price " + this.facetSelection + this.brandName.replaceAll("Phones", "mobile phones") 
226
    	    + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
227
    }
228
 
229
    public String getPageMetaKeywords() {
230
        	return this.brandName + " mobile phone";
231
    }
232
 
233
    public String getBrandTitle() {
234
    	return this.brandTitle;
235
    }	
236
 
237
	public String getQuery() {
238
    	return this.query;
239
    }
240
 
241
 
242
    public List<String> getResults() {
243
    	return this.results;
244
    }
245
 
246
    public Map<String, List<String[]>> getFacets() {
247
    	return this.facets;
248
    }
249
 
250
    public long getTotalResults(){
251
    	return totalResults;
252
    }
253
 
254
    public String getUrl(){
255
    	return this.url;
256
    }
257
 
258
    public String getPriceUrl(){
259
    	return this.priceUrl;
260
    }
261
 
262
	public long getBeginIndex(){
263
		if(totalResults>0)
264
			return beginIndex+1;
265
		return beginIndex;
266
	}
267
 
268
	public long getTotalPages() {
269
		return 1 + (totalResults-1)/windowSize;
270
	}
271
 
272
	public long getCurrentPage() {
273
		return this.page;
274
	}
275
 
276
    public Double getMinPrice() {
277
    	return this.minPrice;
278
    }
279
 
280
    public Double getMaxPrice() {
281
    	return this.maxPrice;
282
    }
283
 
284
    public List<String[]> getCrumbs() {
285
    	return this.crumbs;
286
    }
287
 
288
    public Map<String, String> getSnippets(){
289
    	if(results != null){
290
    		snippets = new HashMap<String, String>();	
291
	    	for(String docId: results){
292
				try {
293
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
294
				} catch (IOException e) {
295
					log.error(e);
296
				}
297
			}
298
    	}
299
		return snippets;
300
    }
301
 
302
}