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