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