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
 
26
/**
27
 * @author rajveer
28
 *
29
 */
30
@Result(name = "index", location = "brand-index.vm")
31
public class BrandController extends BaseController {
32
 
33
	private static final long serialVersionUID = 8477108528765030321L;
34
 
35
	private static Logger log = Logger.getLogger(Class.class);
36
 
37
	private List<String> results;
38
	private Map<String, String> snippets;
39
	private Map<String, List<String[]>> facets;
40
	private List<String[]> crumbs;
41
	/**
42
	 * 
43
	 */
44
	private String brandName;
45
	private String brandTitle;
46
	private String facetSelection = "";
47
 
48
	private String query;
49
 
50
	private Double minPrice;
51
	private Double maxPrice;
52
 
53
 
54
	private long windowSize = 20;
55
	private long page = 1;
56
	private long totalResults;
57
	private long beginIndex = 0;
58
	private String url;
59
	private String priceUrl;
60
 
61
	/**
62
	 * GET /abc/10004
63
	 * 
64
	 */
65
	@Actions({
66
	    @Action("/nokia"),
67
	    @Action("/samsung"),
68
		@Action("/spice"),
1930 rajveer 69
		@Action("/blackberry"),
1923 rajveer 70
		@Action("/sony-ericsson"),
71
		@Action("/micromax"),
72
		@Action("/lg"),
73
		@Action("/htc"),
74
		@Action("/motorola"),
75
		@Action("/dell"),
2029 rajveer 76
		@Action("/apple"),
2425 rajveer 77
		@Action("/android"),
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")){
140
    		newfqrys[0] = "F_50010:" + "Mobile Phones";
141
    	}else{
142
    		newfqrys[0] = "F_50001:" + brandName;
143
    		newfqrys[length] = "F_50010:" + "Mobile Phones";
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");
154
    		String filterUrl = url;
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
    			}
164
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
165
	    	}
166
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
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
    	}
180
    	if(this.request.getParameter("min-price") != null){
181
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
182
    		url= url + "&min-price=" + this.request.getParameter("min-price");
183
    	}
184
    	if(this.request.getParameter("max-price") != null){
185
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
186
    		url= url + "&max-price=" + this.request.getParameter("max-price");
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
    		String drilldownURL = new String();
207
    		for(String facet: facetDetailMap.keySet()){
208
 
209
    			/*
210
    			if(qryString != null){
211
    				drilldownURL = qryString;
212
 
213
    			}
214
    			*/
215
        		drilldownURL = url;
216
 
217
 
218
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
219
    				URLEncoder.encode(facet, "UTF-8");
220
      			String[] afacet = new String[] { facet, 
221
      					facetDetailMap.get(facet).toString(), drilldownURL  };
222
      			values.add(afacet);    
223
 
224
    		}
225
 
226
    		this.facets.put(facetLabel, values);
227
    	}
228
 
229
 
230
 
231
    	Map<String, Double> priceMap = search.getPriceStatsMap();
232
    	if(priceMap != null){
233
    		this.minPrice = priceMap.get("min");
234
    		this.maxPrice = priceMap.get("max");
235
    	}else{
236
    		this.minPrice = 0.0;
237
    		this.maxPrice = 0.0;
238
    	}
239
    	this.totalResults = search.getTotalResults();
240
        return "index";
241
    }
242
 
243
 
244
    public String getBrandName() {
245
    	return this.brandName;
246
    }	
247
 
248
    public String getPageMetaDesc() {
249
    	return "Best price " + this.facetSelection + this.brandName.replaceAll("Phones", "mobile phones") 
2102 vikas 250
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
1923 rajveer 251
    }
252
 
253
    public String getPageMetaKeywords() {
2104 vikas 254
        	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 255
    }
256
 
257
    public String getBrandTitle() {
258
    	return this.brandTitle;
259
    }	
260
 
261
	public String getQuery() {
262
    	return this.query;
263
    }
264
 
265
 
266
    public List<String> getResults() {
267
    	return this.results;
268
    }
269
 
270
    public Map<String, List<String[]>> getFacets() {
271
    	return this.facets;
272
    }
273
 
274
    public long getTotalResults(){
275
    	return totalResults;
276
    }
277
 
278
    public String getUrl(){
279
    	return this.url;
280
    }
281
 
282
    public String getPriceUrl(){
283
    	return this.priceUrl;
284
    }
285
 
286
	public long getBeginIndex(){
287
		if(totalResults>0)
288
			return beginIndex+1;
289
		return beginIndex;
290
	}
291
 
292
	public long getTotalPages() {
293
		return 1 + (totalResults-1)/windowSize;
294
	}
295
 
296
	public long getCurrentPage() {
297
		return this.page;
298
	}
299
 
300
    public Double getMinPrice() {
301
    	return this.minPrice;
302
    }
303
 
304
    public Double getMaxPrice() {
305
    	return this.maxPrice;
306
    }
307
 
308
    public List<String[]> getCrumbs() {
309
    	return this.crumbs;
310
    }
311
 
312
    public Map<String, String> getSnippets(){
313
    	if(results != null){
314
    		snippets = new HashMap<String, String>();	
315
	    	for(String docId: results){
316
				try {
317
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
318
				} catch (IOException e) {
319
					log.error(e);
320
				}
321
			}
322
    	}
323
		return snippets;
324
    }
325
 
326
}