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
import org.apache.struts2.rest.DefaultHttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
27
 
28
/**
29
 * @author rajveer
30
 *
31
 */
32
@Result(name = "index", location = "brand-index.vm")
33
public class BrandController extends BaseController {
34
 
35
	private static final long serialVersionUID = 8477108528765030321L;
36
 
37
	private static Logger log = Logger.getLogger(Class.class);
38
 
39
	private List<String> results;
40
	private Map<String, String> snippets;
41
	private Map<String, List<String[]>> facets;
42
	private List<String[]> crumbs;
43
	/**
44
	 * 
45
	 */
46
	private String brandName;
47
	private String brandTitle;
48
	private String facetSelection = "";
49
 
50
	private String query;
51
 
52
	private Double minPrice;
53
	private Double maxPrice;
54
 
55
 
56
	private long windowSize = 20;
57
	private long page = 1;
58
	private long totalResults;
59
	private long beginIndex = 0;
60
	private String url;
61
	private String priceUrl;
62
 
63
	/**
64
	 * GET /abc/10004
65
	 * 
66
	 */
67
	@Actions({
68
	    @Action("/nokia"),
69
	    @Action("/samsung"),
70
		@Action("/spice"),
1930 rajveer 71
		@Action("/blackberry"),
1923 rajveer 72
		@Action("/sony-ericsson"),
73
		@Action("/micromax"),
74
		@Action("/lg"),
75
		@Action("/htc"),
76
		@Action("/motorola"),
77
		@Action("/dell"),
2029 rajveer 78
		@Action("/apple"),
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
 
121
 
122
    	query = "*";
123
 
124
    	int length = 1;
125
 
126
    	if(fqrys!= null){
127
    		length += fqrys.length;
128
    	}
129
 
130
 
131
    	String[] newfqrys = new String[length+1];
132
 
133
    	newfqrys[0] = "F_50001:" + brandName;
134
 
135
 
136
    	String urlCrumb = url;
137
 
138
    	this.crumbs = new ArrayList<String[]>();
139
    	List<String> arrList = Arrays.asList(facetDefIDs);
140
    	for(int i=1; i<length; i++) {
141
    		newfqrys[i] = fqrys[i-1];
142
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
143
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
144
    		String filterUrl = url;
145
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
146
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
147
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
148
    		this.brandTitle += " | " + facetLabel + " " + facetValue;
149
    		this.facetSelection += facetValue + " ";
150
    		for(int j=1; j<length; j++) {
151
    			if(i==j){
152
    				continue;
153
    			}
154
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
155
	    	}
156
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
157
    		log.info("acrumb=" + Arrays.toString(acrumb));
158
    		this.crumbs.add(acrumb);
159
 
160
    	}
161
 
162
    	newfqrys[length] = "F_50010:" + "Mobile Phones";
163
 
164
		url = urlCrumb;
165
 
166
    	if(this.request.getParameter("page") != null){
167
    		this.page = Long.parseLong(this.request.getParameter("page"));
168
    		this.beginIndex = this.windowSize * (this.page-1);
169
    	}
170
    	if(this.request.getParameter("min-price") != null){
171
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
172
    		url= url + "&min-price=" + this.request.getParameter("min-price");
173
    	}
174
    	if(this.request.getParameter("max-price") != null){
175
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
176
    		url= url + "&max-price=" + this.request.getParameter("max-price");
177
    	}    	
178
 
179
    	query = query + "&sort=F_50002+asc";
180
 
181
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
182
    	this.results =  search.getResultMap(); 
183
 
184
 
185
    	this.facets = new LinkedHashMap<String, List<String[]>>();
186
    	String qryString = this.request.getQueryString();
187
    	log.info("qryString=" + qryString);
188
 
189
    	for (int i=0; i<facetDefIDs.length; i++) {
190
    		String facetDefID = facetDefIDs[i];
191
    		String facetLabel = facetLabels[i];
192
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
193
    		if(facetDetailMap==null)
194
    			continue;
195
    		List<String[]> values = new ArrayList<String[]>();
196
    		String drilldownURL = new String();
197
    		for(String facet: facetDetailMap.keySet()){
198
 
199
    			/*
200
    			if(qryString != null){
201
    				drilldownURL = qryString;
202
 
203
    			}
204
    			*/
205
        		drilldownURL = url;
206
 
207
 
208
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
209
    				URLEncoder.encode(facet, "UTF-8");
210
      			String[] afacet = new String[] { facet, 
211
      					facetDetailMap.get(facet).toString(), drilldownURL  };
212
      			values.add(afacet);    
213
 
214
    		}
215
 
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(){
303
    	if(results != null){
304
    		snippets = new HashMap<String, String>();	
305
	    	for(String docId: results){
306
				try {
307
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
308
				} catch (IOException e) {
309
					log.error(e);
310
				}
311
			}
312
    	}
313
		return snippets;
314
    }
315
 
316
}