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"),
3012 rajveer 79
		@Action("/alcatel"),
1923 rajveer 80
 
81
		@Action("/brand")
82
	})
83
 
84
    public String index() throws SecurityException, IOException {
85
 
86
	    String uri = request.getRequestURI();
87
	    this.brandName = uri.replace("/", "");
1930 rajveer 88
	    if(this.brandName.equals("blackberry")){
89
            this.brandName = "BlackBerry";
90
        }else if(this.brandName.equals("lg")){
91
            this.brandName = "LG";
92
        }else if(this.brandName.equals("htc")){
93
            this.brandName = "HTC";
94
        }else{
95
        	String[] parts = this.brandName.split("-");
96
        	StringBuffer brand = new StringBuffer(); 
97
        	for(int i=0; i < parts.length; i++){
98
        	    if(i>0)
99
        	    brand.append(" ");
100
        	    brand.append(Character.toUpperCase(parts[i].charAt(0)));
101
        	    brand.append(parts[i].substring(1)); 
102
        	}
103
        	this.brandName = brand.toString();
104
        }
105
 
2104 vikas 106
	    if (this.brandName.equals("Samsung") || this.brandName.equals("Sony Ericsson")) {
107
	        this.brandTitle = this.brandName + " Mobile Phones India | Buy " + this.brandName + " Phones" ;
108
	    }
109
	    else {
110
    	    this.brandTitle = this.brandName + " Mobile Price India" ;
111
	    }
1923 rajveer 112
 
113
 
114
    	String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
115
        String[] facetLabels = new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
116
 
117
    	String[] fqrys = this.request.getParameterValues("fq");
118
 
119
    	url = "?";
120
 
121
 
2425 rajveer 122
    	if(brandName.equals("Android")){
123
    		query = "android";	
124
    	}else{
125
    		query = "*";
126
    	}
1923 rajveer 127
 
128
    	int length = 1;
129
 
130
    	if(fqrys!= null){
131
    		length += fqrys.length;
132
    	}
133
 
2425 rajveer 134
    	String[] newfqrys;
135
    	if(brandName.equals("Android")){
136
    		newfqrys = new String[length];
137
    	}else{
138
    		newfqrys = new String[length+1];
139
    	}
1923 rajveer 140
 
2425 rajveer 141
    	if(brandName.equals("Android")){
2709 rajveer 142
    		newfqrys[0] = "F_50010:" + "(\"Mobile Phones\" OR Tablets)";
2425 rajveer 143
    	}else{
144
    		newfqrys[0] = "F_50001:" + brandName;
2709 rajveer 145
    		newfqrys[length] = "F_50010:" + "(\"Mobile Phones\" OR Tablets)";
2425 rajveer 146
    	}
1923 rajveer 147
 
148
    	String urlCrumb = url;
149
 
150
    	this.crumbs = new ArrayList<String[]>();
151
    	List<String> arrList = Arrays.asList(facetDefIDs);
152
    	for(int i=1; i<length; i++) {
153
    		newfqrys[i] = fqrys[i-1];
154
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
155
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
2948 chandransh 156
    		StringBuilder filterUrl = new StringBuilder(url);
1923 rajveer 157
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
158
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
159
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
160
    		this.brandTitle += " | " + facetLabel + " " + facetValue;
161
    		this.facetSelection += facetValue + " ";
162
    		for(int j=1; j<length; j++) {
163
    			if(i==j){
164
    				continue;
165
    			}
2948 chandransh 166
    			filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8"));
1923 rajveer 167
	    	}
2948 chandransh 168
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
1923 rajveer 169
    		log.info("acrumb=" + Arrays.toString(acrumb));
170
    		this.crumbs.add(acrumb);
171
 
172
    	}
173
 
174
 
2425 rajveer 175
 
1923 rajveer 176
		url = urlCrumb;
177
 
178
    	if(this.request.getParameter("page") != null){
179
    		this.page = Long.parseLong(this.request.getParameter("page"));
180
    		this.beginIndex = this.windowSize * (this.page-1);
181
    	}
3173 rajveer 182
    	if(this.request.getParameter("minPrice") != null){
183
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
184
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
1923 rajveer 185
    	}
3173 rajveer 186
    	if(this.request.getParameter("maxPrice") != null){
187
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
188
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
1923 rajveer 189
    	}    	
190
 
191
    	query = query + "&sort=F_50002+asc";
192
 
193
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
194
    	this.results =  search.getResultMap(); 
195
 
196
 
197
    	this.facets = new LinkedHashMap<String, List<String[]>>();
198
    	String qryString = this.request.getQueryString();
199
    	log.info("qryString=" + qryString);
200
 
201
    	for (int i=0; i<facetDefIDs.length; i++) {
202
    		String facetDefID = facetDefIDs[i];
203
    		String facetLabel = facetLabels[i];
204
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
205
    		if(facetDetailMap==null)
206
    			continue;
207
    		List<String[]> values = new ArrayList<String[]>();
208
 
2948 chandransh 209
            for (Entry<String, Integer> facetEntry : facetDetailMap.entrySet()) {
210
                String facet = facetEntry.getKey();
211
                String drilldownURL = url + "&fq=" + facetDefID + ":"
212
                        + URLEncoder.encode(facet, "UTF-8");
213
                String[] afacet = new String[] { facet,
214
                        facetEntry.getValue().toString(), drilldownURL };
215
                values.add(afacet);
216
            }
217
 
1923 rajveer 218
    		this.facets.put(facetLabel, values);
219
    	}
220
 
221
 
222
 
223
    	Map<String, Double> priceMap = search.getPriceStatsMap();
224
    	if(priceMap != null){
225
    		this.minPrice = priceMap.get("min");
226
    		this.maxPrice = priceMap.get("max");
227
    	}else{
228
    		this.minPrice = 0.0;
229
    		this.maxPrice = 0.0;
230
    	}
231
    	this.totalResults = search.getTotalResults();
232
        return "index";
233
    }
234
 
235
 
236
    public String getBrandName() {
237
    	return this.brandName;
238
    }	
239
 
240
    public String getPageMetaDesc() {
241
    	return "Best price " + this.facetSelection + this.brandName.replaceAll("Phones", "mobile phones") 
2102 vikas 242
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
1923 rajveer 243
    }
244
 
245
    public String getPageMetaKeywords() {
2104 vikas 246
        	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 247
    }
248
 
249
    public String getBrandTitle() {
250
    	return this.brandTitle;
251
    }	
252
 
253
	public String getQuery() {
254
    	return this.query;
255
    }
256
 
257
 
258
    public List<String> getResults() {
259
    	return this.results;
260
    }
261
 
262
    public Map<String, List<String[]>> getFacets() {
263
    	return this.facets;
264
    }
265
 
266
    public long getTotalResults(){
267
    	return totalResults;
268
    }
269
 
270
    public String getUrl(){
271
    	return this.url;
272
    }
273
 
274
    public String getPriceUrl(){
275
    	return this.priceUrl;
276
    }
277
 
278
	public long getBeginIndex(){
279
		if(totalResults>0)
280
			return beginIndex+1;
281
		return beginIndex;
282
	}
283
 
284
	public long getTotalPages() {
285
		return 1 + (totalResults-1)/windowSize;
286
	}
287
 
288
	public long getCurrentPage() {
289
		return this.page;
290
	}
291
 
292
    public Double getMinPrice() {
293
    	return this.minPrice;
294
    }
295
 
296
    public Double getMaxPrice() {
297
    	return this.maxPrice;
298
    }
299
 
300
    public List<String[]> getCrumbs() {
301
    	return this.crumbs;
302
    }
303
 
304
    public Map<String, String> getSnippets(){
305
    	if(results != null){
306
    		snippets = new HashMap<String, String>();	
307
	    	for(String docId: results){
308
				try {
309
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
310
				} catch (IOException e) {
311
					log.error(e);
312
				}
313
			}
314
    	}
315
		return snippets;
316
    }
317
 
318
}