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