Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
517 rajveer 6
import in.shop2020.model.v1.widgets.WidgetService.Client;
7
import in.shop2020.serving.pages.PageContentKeys;
8
import in.shop2020.serving.pages.PageEnum;
536 rajveer 9
import in.shop2020.serving.services.CategoryManager;
517 rajveer 10
import in.shop2020.serving.services.PageLoaderHandler;
11
import in.shop2020.serving.services.SolrSearchService;
12
import in.shop2020.serving.utils.FileUtils;
13
import in.shop2020.thrift.clients.WidgetServiceClient;
14
import in.shop2020.util.Utils;
15
 
16
import java.io.File;
317 ashish 17
import java.io.IOException;
517 rajveer 18
import java.net.URLEncoder;
19
import java.util.ArrayList;
20
import java.util.Arrays;
21
import java.util.HashMap;
22
import java.util.LinkedHashMap;
23
import java.util.List;
24
import java.util.Map;
536 rajveer 25
import java.util.StringTokenizer;
317 ashish 26
 
517 rajveer 27
import org.apache.commons.lang.StringUtils;
317 ashish 28
import org.apache.juli.logging.Log;
29
import org.apache.juli.logging.LogFactory;
30
import org.apache.struts2.rest.DefaultHttpHeaders;
31
import org.apache.struts2.rest.HttpHeaders;
32
 
33
/**
517 rajveer 34
 * @author rajveer
317 ashish 35
 *
36
 */
37
public class CategoryController extends BaseController {
38
 
39
	/**
40
	 * 
41
	 */
42
	private static Log log = LogFactory.getLog(CategoryController.class);
517 rajveer 43
 
317 ashish 44
	/**
45
	 * 
46
	 */
517 rajveer 47
	private Map<String,String> htmlSnippets;
48
	private Map<String, String[]> results;
49
	private Map<String, String> snippets;
50
	private Map<String, List<String[]>> facets;
51
	/**
52
	 * 
53
	 */
317 ashish 54
	private String id;
55
 
536 rajveer 56
	private String categoryName;
57
 
517 rajveer 58
	private String query;
59
 
60
	private Double minPrice;
61
	private Double maxPrice;
62
 
545 rajveer 63
 
64
	private long windowSize = 20;
65
	private long page = 1;
66
	private long totalResults;
67
	private long beginIndex = 0;
68
	private String url;
69
	private String priceUrl;
70
 
317 ashish 71
	/**
72
	 * GET /category/10004
73
	 * 
74
	 */
75
    public HttpHeaders show() throws SecurityException, IOException {
76
    	log.info("id=" + id);
536 rajveer 77
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(Long.parseLong(id));
78
 
79
 
517 rajveer 80
    	htmlSnippets = new HashMap<String, String>();
81
    	PageLoaderHandler pageLoader = new PageLoaderHandler();
82
 
83
		htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));
84
		htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
85
		htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
86
		htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
87
		htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
88
		htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
89
 
90
		htmlSnippets.put("CATEGORY_HEADER", pageLoader.getCategoryHeaderSnippet());
91
		htmlSnippets.put("MAIN_BANNER", "");
92
		htmlSnippets.put("SIDE_BANNER", "");
93
		htmlSnippets.put("TAB_BUTTONS", "");
545 rajveer 94
 
317 ashish 95
 
517 rajveer 96
 
97
 
98
		String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
99
 
100
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" 	};
101
 
102
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 103
 
104
    	url = "?";
536 rajveer 105
 
545 rajveer 106
 
107
 
517 rajveer 108
    	query = "*";
109
 
536 rajveer 110
    	int length = 1;
111
 
112
    	if(fqrys!= null){
113
    		length += fqrys.length;
114
    	}
115
 
116
    	String[] newfqrys = new String[length];
545 rajveer 117
    	newfqrys[0] = "Category:"+categoryName;
118
    	String urlCrumb = url;
536 rajveer 119
 
120
    	for(int i=1; i<length; i++) {
121
    		newfqrys[i] = fqrys[i-1];
545 rajveer 122
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
123
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
536 rajveer 124
    	}
545 rajveer 125
		url = urlCrumb;
126
 
127
    	if(this.request.getParameter("page") != null){
128
    		this.page = Long.parseLong(this.request.getParameter("page"));
129
    		this.beginIndex = this.windowSize * (this.page-1);
130
    	}
131
    	if(this.request.getParameter("min-price") != null){
132
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
133
    		url= url + "&min-price=" + this.request.getParameter("min-price");
134
    	}
135
    	if(this.request.getParameter("max-price") != null){
136
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
137
    		url= url + "&max-price=" + this.request.getParameter("max-price");
138
    	}    	
536 rajveer 139
 
545 rajveer 140
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice);
517 rajveer 141
    	this.results = new LinkedHashMap<String, String[]>();
142
    	this.results =  search.getResultMap(); 
143
 
144
 
145
    	this.facets = new LinkedHashMap<String, List<String[]>>();
146
    	String qryString = this.request.getQueryString();
147
    	log.info("qryString=" + qryString);
148
 
149
    	for (int i=0; i<facetDefIDs.length; i++) {
150
    		String facetDefID = facetDefIDs[i];
151
    		String facetLabel = facetLabels[i];
152
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
153
    		if(facetDetailMap==null)
154
    			continue;
155
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 156
    		String drilldownURL = new String();
517 rajveer 157
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 158
 
545 rajveer 159
    			/*
536 rajveer 160
    			if(qryString != null){
161
    				drilldownURL = qryString;
545 rajveer 162
 
536 rajveer 163
    			}
545 rajveer 164
    			*/
165
        		drilldownURL = url;
536 rajveer 166
 
545 rajveer 167
 
168
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
169
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 170
      			String[] afacet = new String[] { facet, 
171
      					facetDetailMap.get(facet).toString(), drilldownURL  };
172
      			values.add(afacet);    
545 rajveer 173
 
517 rajveer 174
    		}
175
 
176
    		this.facets.put(facetLabel, values);
177
    	}
178
 
179
 
545 rajveer 180
 
517 rajveer 181
    	Map<String, Double> priceMap = search.getPriceStatsMap();
182
    	if(priceMap != null){
183
    		this.minPrice = priceMap.get("min");
184
    		this.maxPrice = priceMap.get("max");
185
    	}else{
186
    		this.minPrice = 0.0;
187
    		this.maxPrice = 0.0;
188
    	}
545 rajveer 189
    	this.totalResults = search.getTotalResults();
317 ashish 190
        return new DefaultHttpHeaders("show");
191
    }
192
 
193
    /**
194
     * 
195
     * @param id
196
     */
197
    public void setId(String id) {
536 rajveer 198
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
199
    	while(tokenizer.hasMoreTokens()){
200
    		this.id = tokenizer.nextToken();
201
    	}
317 ashish 202
    }
203
 
204
    /**
205
     * 
206
     */
207
    public String getId() {
208
    	return this.id;
209
    }
517 rajveer 210
 
536 rajveer 211
    public String getCategoryName() {
212
    	return this.categoryName;
213
    }
214
 
215
 
517 rajveer 216
    public String getHeaderSnippet(){
217
		return htmlSnippets.get("HEADER");
218
	}
219
 
220
	public String getMainMenuSnippet(){
221
		return htmlSnippets.get("MAIN_MENU");
222
	}
223
 
224
	public String getSearchBarSnippet(){
225
		return htmlSnippets.get("SEARCH_BAR");
226
	}
227
 
228
	public String getCustomerServiceSnippet(){
229
		return htmlSnippets.get("CUSTOMER_SERVICE");
230
	}
231
 
232
	public String getSearchHeaderSnippet(){
233
		return htmlSnippets.get("SEARCH_HEADER");
234
	}
235
 
236
	public String getSearchDetailsSnippet(){
237
		return htmlSnippets.get("SEARCH_DETAILS");
238
	}
239
 
240
	public String getMyResearchSnippet(){
241
		return htmlSnippets.get("MY_RESEARCH");
242
	}
243
 
244
	public String getFooterSnippet(){
245
		return htmlSnippets.get("FOOTER");
246
	}
247
 
248
	public String getJsFileSnippet(){
249
		return htmlSnippets.get("JS_FILES");
250
	}
251
 
252
	public String getCssFileSnippet(){
253
		return htmlSnippets.get("CSS_FILES");
254
	}
255
 
256
    /**
257
     * 
258
     * @return
259
     */
260
    public String getQuery() {
261
    	return this.query;
262
    }
263
 
264
 
265
    /**
266
     * 
267
     * @return
268
     */
269
    public Map<String, String[]> getResults() {
270
    	return this.results;
271
    }
272
 
273
    /**
274
     * 
275
     * @return
276
     */
277
    public Map<String, List<String[]>> getFacets() {
278
    	return this.facets;
279
    }
280
 
545 rajveer 281
 
282
    public long getTotalResults(){
283
    	return totalResults;
284
    }
285
 
286
    public String getUrl(){
287
    	return this.url;
288
    }
289
 
290
    public String getPriceUrl(){
291
    	return this.priceUrl;
292
    }
293
 
294
	public long getBeginIndex(){
295
		if(totalResults>0)
296
			return beginIndex+1;
297
		return beginIndex;
298
	}
299
 
300
	public long getTotalPages() {
301
		return 1 + totalResults/windowSize;
302
	}
303
 
304
	public long getCurrentPage() {
305
		return this.page;
306
	}
307
 
517 rajveer 308
    public Double getMinPrice() {
309
    	return this.minPrice;
310
    }
311
 
312
    public Double getMaxPrice() {
313
    	return this.maxPrice;
314
    }
315
 
316
    public Map<String, String> getSnippets() throws Exception {
317
    	if(results != null){
318
    		snippets = new HashMap<String, String>();	
319
	    	for(String docId: results.keySet()){
536 rajveer 320
				snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
517 rajveer 321
			}
322
    	}
323
		return snippets;
324
    }
325
 
317 ashish 326
}