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