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
 
3242 vikas 6
import in.shop2020.serving.cache.CategorySnippetCacheWrapper;
1923 rajveer 7
import in.shop2020.serving.services.SolrSearchService;
8
 
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;
1923 rajveer 18
 
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.apache.struts2.convention.annotation.Action;
22
import org.apache.struts2.convention.annotation.Actions;
23
import org.apache.struts2.convention.annotation.Result;
24
 
25
/**
26
 * @author rajveer
27
 *
28
 */
29
@Result(name = "index", location = "brand-index.vm")
30
public class BrandController extends BaseController {
31
 
32
	private static final long serialVersionUID = 8477108528765030321L;
33
 
34
	private static Logger log = Logger.getLogger(Class.class);
35
 
36
	private List<String> results;
37
	private Map<String, String> snippets;
38
	private Map<String, List<String[]>> facets;
39
	private List<String[]> crumbs;
40
	/**
41
	 * 
42
	 */
43
	private String brandName;
44
	private String brandTitle;
45
	private String facetSelection = "";
46
 
47
	private String query;
48
 
49
	private Double minPrice;
50
	private Double maxPrice;
51
 
52
 
53
	private long windowSize = 20;
54
	private long page = 1;
55
	private long totalResults;
56
	private long beginIndex = 0;
57
	private String url;
2789 rajveer 58
	private String priceUrl = "?";
1923 rajveer 59
 
60
	/**
61
	 * GET /abc/10004
62
	 * 
63
	 */
64
	@Actions({
65
	    @Action("/nokia"),
66
	    @Action("/samsung"),
67
		@Action("/spice"),
1930 rajveer 68
		@Action("/blackberry"),
1923 rajveer 69
		@Action("/sony-ericsson"),
70
		@Action("/micromax"),
71
		@Action("/lg"),
72
		@Action("/htc"),
73
		@Action("/motorola"),
74
		@Action("/dell"),
2029 rajveer 75
		@Action("/apple"),
2425 rajveer 76
		@Action("/android"),
3012 rajveer 77
		@Action("/alcatel"),
1923 rajveer 78
 
79
		@Action("/brand")
80
	})
81
 
82
    public String index() throws SecurityException, IOException {
83
 
84
	    String uri = request.getRequestURI();
85
	    this.brandName = uri.replace("/", "");
1930 rajveer 86
	    if(this.brandName.equals("blackberry")){
87
            this.brandName = "BlackBerry";
88
        }else if(this.brandName.equals("lg")){
89
            this.brandName = "LG";
90
        }else if(this.brandName.equals("htc")){
91
            this.brandName = "HTC";
92
        }else{
93
        	String[] parts = this.brandName.split("-");
94
        	StringBuffer brand = new StringBuffer(); 
95
        	for(int i=0; i < parts.length; i++){
96
        	    if(i>0)
97
        	    brand.append(" ");
98
        	    brand.append(Character.toUpperCase(parts[i].charAt(0)));
99
        	    brand.append(parts[i].substring(1)); 
100
        	}
101
        	this.brandName = brand.toString();
102
        }
103
 
2104 vikas 104
	    if (this.brandName.equals("Samsung") || this.brandName.equals("Sony Ericsson")) {
105
	        this.brandTitle = this.brandName + " Mobile Phones India | Buy " + this.brandName + " Phones" ;
106
	    }
107
	    else {
108
    	    this.brandTitle = this.brandName + " Mobile Price India" ;
109
	    }
1923 rajveer 110
 
111
 
112
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
113
        String[] facetLabels = new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
114
 
115
    	String[] fqrys = this.request.getParameterValues("fq");
116
 
117
    	url = "?";
118
 
119
 
2425 rajveer 120
    	if(brandName.equals("Android")){
121
    		query = "android";	
122
    	}else{
123
    		query = "*";
124
    	}
1923 rajveer 125
 
126
    	int length = 1;
127
 
128
    	if(fqrys!= null){
129
    		length += fqrys.length;
130
    	}
131
 
2425 rajveer 132
    	String[] newfqrys;
133
    	if(brandName.equals("Android")){
134
    		newfqrys = new String[length];
135
    	}else{
136
    		newfqrys = new String[length+1];
137
    	}
1923 rajveer 138
 
2425 rajveer 139
    	if(brandName.equals("Android")){
3261 rajveer 140
    		newfqrys[0] = "F_50010:" + "(\"Mobile Phones\") OR Tablets";
2425 rajveer 141
    	}else{
142
    		newfqrys[0] = "F_50001:" + brandName;
3261 rajveer 143
    		newfqrys[length] = "F_50010:" + "(\"Mobile Phones\") OR Tablets";
2425 rajveer 144
    	}
1923 rajveer 145
 
146
    	String urlCrumb = url;
147
 
148
    	this.crumbs = new ArrayList<String[]>();
149
    	List<String> arrList = Arrays.asList(facetDefIDs);
150
    	for(int i=1; i<length; i++) {
151
    		newfqrys[i] = fqrys[i-1];
152
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
153
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
2948 chandransh 154
    		StringBuilder filterUrl = new StringBuilder(url);
1923 rajveer 155
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
156
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
157
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
158
    		this.brandTitle += " | " + facetLabel + " " + facetValue;
159
    		this.facetSelection += facetValue + " ";
160
    		for(int j=1; j<length; j++) {
161
    			if(i==j){
162
    				continue;
163
    			}
2948 chandransh 164
    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8"));
1923 rajveer 165
	    	}
2948 chandransh 166
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
1923 rajveer 167
    		log.info("acrumb=" + Arrays.toString(acrumb));
168
    		this.crumbs.add(acrumb);
169
 
170
    	}
171
 
172
 
2425 rajveer 173
 
1923 rajveer 174
		url = urlCrumb;
175
 
176
    	if(this.request.getParameter("page") != null){
177
    		this.page = Long.parseLong(this.request.getParameter("page"));
178
    		this.beginIndex = this.windowSize * (this.page-1);
179
    	}
3173 rajveer 180
    	if(this.request.getParameter("minPrice") != null){
181
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
182
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
1923 rajveer 183
    	}
3173 rajveer 184
    	if(this.request.getParameter("maxPrice") != null){
185
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
186
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
1923 rajveer 187
    	}    	
188
 
189
    	query = query + "&sort=F_50002+asc";
190
 
191
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
192
    	this.results =  search.getResultMap(); 
193
 
194
 
195
    	this.facets = new LinkedHashMap<String, List<String[]>>();
196
    	String qryString = this.request.getQueryString();
197
    	log.info("qryString=" + qryString);
198
 
199
    	for (int i=0; i<facetDefIDs.length; i++) {
200
    		String facetDefID = facetDefIDs[i];
201
    		String facetLabel = facetLabels[i];
202
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
203
    		if(facetDetailMap==null)
204
    			continue;
205
    		List<String[]> values = new ArrayList<String[]>();
206
 
2948 chandransh 207
            for (Entry<String, Integer> facetEntry : facetDetailMap.entrySet()) {
208
                String facet = facetEntry.getKey();
209
                String drilldownURL = url + "&fq=" + facetDefID + ":"
210
                        + URLEncoder.encode(facet, "UTF-8");
211
                String[] afacet = new String[] { facet,
212
                        facetEntry.getValue().toString(), drilldownURL };
213
                values.add(afacet);
214
            }
215
 
1923 rajveer 216
    		this.facets.put(facetLabel, values);
217
    	}
218
 
219
 
220
 
221
    	Map<String, Double> priceMap = search.getPriceStatsMap();
222
    	if(priceMap != null){
223
    		this.minPrice = priceMap.get("min");
224
    		this.maxPrice = priceMap.get("max");
225
    	}else{
226
    		this.minPrice = 0.0;
227
    		this.maxPrice = 0.0;
228
    	}
229
    	this.totalResults = search.getTotalResults();
230
        return "index";
231
    }
232
 
233
 
234
    public String getBrandName() {
235
    	return this.brandName;
236
    }	
237
 
238
    public String getPageMetaDesc() {
239
    	return "Best price " + this.facetSelection + this.brandName.replaceAll("Phones", "mobile phones") 
2102 vikas 240
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
1923 rajveer 241
    }
242
 
243
    public String getPageMetaKeywords() {
2104 vikas 244
        	return this.brandName + " mobile phone, " + this.brandName + " mobile phone price, "  + this.brandName + " mobile phone deals, "+ this.brandName + " mobile phone india, buy " + this.brandName + " mobiles";
1923 rajveer 245
    }
246
 
247
    public String getBrandTitle() {
248
    	return this.brandTitle;
249
    }	
250
 
251
	public String getQuery() {
252
    	return this.query;
253
    }
254
 
255
 
256
    public List<String> getResults() {
257
    	return this.results;
258
    }
259
 
260
    public Map<String, List<String[]>> getFacets() {
261
    	return this.facets;
262
    }
263
 
264
    public long getTotalResults(){
265
    	return totalResults;
266
    }
267
 
268
    public String getUrl(){
269
    	return this.url;
270
    }
271
 
272
    public String getPriceUrl(){
273
    	return this.priceUrl;
274
    }
275
 
276
	public long getBeginIndex(){
277
		if(totalResults>0)
278
			return beginIndex+1;
279
		return beginIndex;
280
	}
281
 
282
	public long getTotalPages() {
283
		return 1 + (totalResults-1)/windowSize;
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
 
302
    public Map<String, String> getSnippets(){
3242 vikas 303
        if(results != null){
304
            snippets = new HashMap<String, String>();   
305
            for(String docId: results){
306
                String snippet = CategorySnippetCacheWrapper.getSnippet(docId);
307
                if (snippet != null) {
308
                    snippets.put(docId, snippet);
309
                }
310
            }
311
        }
312
        return snippets;
1923 rajveer 313
    }
314
 
315
}