Subversion Repositories SmartDukaan

Rev

Rev 2070 | Rev 2505 | Go to most recent revision | Details | Compare with Previous | 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.serving.services.SolrSearchService;
7
import in.shop2020.serving.utils.FileUtils;
637 rajveer 8
import in.shop2020.serving.utils.Utils;
2070 rajveer 9
import in.shop2020.utils.CategoryManager;
517 rajveer 10
 
11
import java.io.File;
317 ashish 12
import java.io.IOException;
517 rajveer 13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.HashMap;
17
import java.util.LinkedHashMap;
18
import java.util.List;
19
import java.util.Map;
536 rajveer 20
import java.util.StringTokenizer;
317 ashish 21
 
517 rajveer 22
import org.apache.commons.lang.StringUtils;
832 rajveer 23
import org.apache.log4j.Logger;
974 vikas 24
import org.apache.struts2.convention.annotation.Action;
25
import org.apache.struts2.convention.annotation.Actions;
26
import org.apache.struts2.convention.annotation.Result;
317 ashish 27
import org.apache.struts2.rest.DefaultHttpHeaders;
28
import org.apache.struts2.rest.HttpHeaders;
29
 
30
/**
517 rajveer 31
 * @author rajveer
317 ashish 32
 *
33
 */
974 vikas 34
@Result(name = "show", location = "category-show.vm")
317 ashish 35
public class CategoryController extends BaseController {
36
 
1044 chandransh 37
	private static final long serialVersionUID = 8477108528765030321L;
38
 
832 rajveer 39
	private static Logger log = Logger.getLogger(Class.class);
517 rajveer 40
 
790 vikas 41
	private List<String> results;
517 rajveer 42
	private Map<String, String> snippets;
43
	private Map<String, List<String[]>> facets;
620 rajveer 44
	private List<String[]> crumbs;
517 rajveer 45
	/**
46
	 * 
47
	 */
317 ashish 48
	private String id;
49
 
536 rajveer 50
	private String categoryName;
974 vikas 51
	private String categoryTitle;
1259 vikas 52
	private String facetSelection = "";
536 rajveer 53
 
517 rajveer 54
	private String query;
55
 
56
	private Double minPrice;
57
	private Double maxPrice;
58
 
545 rajveer 59
 
60
	private long windowSize = 20;
61
	private long page = 1;
62
	private long totalResults;
63
	private long beginIndex = 0;
64
	private String url;
1921 vikas 65
	private String priceUrl = "?";
545 rajveer 66
 
317 ashish 67
	/**
974 vikas 68
	 * GET /abc/10004
317 ashish 69
	 * 
70
	 */
974 vikas 71
	@Actions({
1472 rajveer 72
		@Action("/all-mobile-phones"),
73
		@Action("/all-mobile-accessories"),
74
 
974 vikas 75
		@Action("/business-phones"),
76
		@Action("/high-end-multimedia-phones"),
77
		@Action("/low-end-multimedia-phones"),
78
		@Action("/basic-phones"),
79
 
80
		@Action("/bluetooth-headset"),
81
		@Action("/memory-card"),
82
		@Action("/battery"),
83
		@Action("/headset"),
84
		@Action("/charger"),
85
		@Action("/pen-drive"),
86
		@Action("/carrying-case"),
1005 vikas 87
		@Action("/car-charger"),
1306 chandransh 88
		@Action("/screen-guard"),
1005 vikas 89
 
1534 rajveer 90
		@Action("/face-plate"),
91
		@Action("/decal"),
92
		@Action("/data-cable"),
93
		@Action("/ear-buds"),
94
		@Action("/cleaning-kit"),
95
		@Action("/speaker"),
96
 
97
 
98
 
1005 vikas 99
		@Action("/category")
974 vikas 100
	})
101
 
317 ashish 102
    public HttpHeaders show() throws SecurityException, IOException {
103
    	log.info("id=" + id);
1472 rajveer 104
    	long categoryId = Long.parseLong(id);
105
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 106
 
2306 vikas 107
    	if( categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY ){
1472 rajveer 108
    		this.categoryTitle = "Mobile Price List India" + " | " + this.categoryName;
109
    	}else{
110
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
111
    	}
112
 
113
 
317 ashish 114
 
517 rajveer 115
		String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
116
 
117
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" 	};
118
 
119
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 120
 
121
    	url = "?";
122
 
517 rajveer 123
    	query = "*";
124
 
536 rajveer 125
    	int length = 1;
126
 
127
    	if(fqrys!= null){
128
    		length += fqrys.length;
129
    	}
130
 
131
    	String[] newfqrys = new String[length];
2306 vikas 132
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 133
    		newfqrys[0] = "F_50010:"+categoryName;
134
    	}else{
135
    		newfqrys[0] = "F_50011:"+categoryName;
136
    	}
137
 
545 rajveer 138
    	String urlCrumb = url;
536 rajveer 139
 
620 rajveer 140
    	this.crumbs = new ArrayList<String[]>();
141
    	List<String> arrList = Arrays.asList(facetDefIDs);
536 rajveer 142
    	for(int i=1; i<length; i++) {
143
    		newfqrys[i] = fqrys[i-1];
545 rajveer 144
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
1921 vikas 145
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
620 rajveer 146
    		String filterUrl = url;
147
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
148
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
149
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
1257 vikas 150
    		this.categoryTitle += " | " + facetLabel + " " + facetValue;
1259 vikas 151
    		this.facetSelection += facetValue + " ";
620 rajveer 152
    		for(int j=1; j<length; j++) {
153
    			if(i==j){
154
    				continue;
155
    			}
156
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
157
	    	}
158
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
159
    		log.info("acrumb=" + Arrays.toString(acrumb));
160
    		this.crumbs.add(acrumb);
161
 
536 rajveer 162
    	}
545 rajveer 163
		url = urlCrumb;
164
 
165
    	if(this.request.getParameter("page") != null){
166
    		this.page = Long.parseLong(this.request.getParameter("page"));
167
    		this.beginIndex = this.windowSize * (this.page-1);
168
    	}
169
    	if(this.request.getParameter("min-price") != null){
170
    		this.minPrice = (new Double(this.request.getParameter("min-price")));
171
    		url= url + "&min-price=" + this.request.getParameter("min-price");
172
    	}
173
    	if(this.request.getParameter("max-price") != null){
174
    		this.maxPrice = (new Double(this.request.getParameter("max-price")));
175
    		url= url + "&max-price=" + this.request.getParameter("max-price");
176
    	}    	
1377 rajveer 177
 
178
    	query = query + "&sort=F_50002+asc";
536 rajveer 179
 
2306 vikas 180
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, null);
517 rajveer 181
    	this.results =  search.getResultMap(); 
182
 
183
 
184
    	this.facets = new LinkedHashMap<String, List<String[]>>();
185
    	String qryString = this.request.getQueryString();
186
    	log.info("qryString=" + qryString);
187
 
188
    	for (int i=0; i<facetDefIDs.length; i++) {
189
    		String facetDefID = facetDefIDs[i];
190
    		String facetLabel = facetLabels[i];
191
    		HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
192
    		if(facetDetailMap==null)
193
    			continue;
194
    		List<String[]> values = new ArrayList<String[]>();
545 rajveer 195
    		String drilldownURL = new String();
517 rajveer 196
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 197
 
545 rajveer 198
    			/*
536 rajveer 199
    			if(qryString != null){
200
    				drilldownURL = qryString;
545 rajveer 201
 
536 rajveer 202
    			}
545 rajveer 203
    			*/
204
        		drilldownURL = url;
536 rajveer 205
 
545 rajveer 206
 
207
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
208
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 209
      			String[] afacet = new String[] { facet, 
210
      					facetDetailMap.get(facet).toString(), drilldownURL  };
211
      			values.add(afacet);    
545 rajveer 212
 
517 rajveer 213
    		}
214
 
215
    		this.facets.put(facetLabel, values);
216
    	}
217
 
218
 
545 rajveer 219
 
517 rajveer 220
    	Map<String, Double> priceMap = search.getPriceStatsMap();
221
    	if(priceMap != null){
222
    		this.minPrice = priceMap.get("min");
223
    		this.maxPrice = priceMap.get("max");
224
    	}else{
225
    		this.minPrice = 0.0;
226
    		this.maxPrice = 0.0;
227
    	}
545 rajveer 228
    	this.totalResults = search.getTotalResults();
317 ashish 229
        return new DefaultHttpHeaders("show");
230
    }
231
 
232
    public void setId(String id) {
536 rajveer 233
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
234
    	while(tokenizer.hasMoreTokens()){
235
    		this.id = tokenizer.nextToken();
236
    	}
317 ashish 237
    }
238
 
239
    public String getId() {
240
    	return this.id;
241
    }
517 rajveer 242
 
536 rajveer 243
    public String getCategoryName() {
244
    	return this.categoryName;
942 chandransh 245
    }	
517 rajveer 246
 
974 vikas 247
    public String getPageMetaDesc() {
1966 vikas 248
        return "Best price "
249
                + this.facetSelection.replaceAll("Phones", "")
250
                + this.categoryName
251
                        .replaceAll("Mobile Phones", "mobile phones")
252
                        .replaceAll("Phones", "mobile phones")
253
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 254
    }
255
 
256
    public String getPageMetaKeywords() {
2306 vikas 257
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 258
        	return this.categoryName + ", mobile phone";
259
    	}
2306 vikas 260
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 261
        	return this.categoryName + ", phone accessories";
262
    	}
263
        return "";
264
    }
265
 
266
    public String getCategoryTitle() {
267
    	return this.categoryTitle;
268
    }	
269
 
650 rajveer 270
	public String getQuery() {
517 rajveer 271
    	return this.query;
272
    }
273
 
274
 
790 vikas 275
    public List<String> getResults() {
517 rajveer 276
    	return this.results;
277
    }
278
 
279
    public Map<String, List<String[]>> getFacets() {
280
    	return this.facets;
281
    }
282
 
545 rajveer 283
    public long getTotalResults(){
284
    	return totalResults;
285
    }
286
 
287
    public String getUrl(){
288
    	return this.url;
289
    }
290
 
291
	public long getBeginIndex(){
292
		if(totalResults>0)
293
			return beginIndex+1;
294
		return beginIndex;
295
	}
296
 
297
	public long getTotalPages() {
1376 rajveer 298
		return 1 + (totalResults-1)/windowSize;
545 rajveer 299
	}
300
 
301
	public long getCurrentPage() {
302
		return this.page;
303
	}
304
 
517 rajveer 305
    public Double getMinPrice() {
306
    	return this.minPrice;
307
    }
308
 
309
    public Double getMaxPrice() {
310
    	return this.maxPrice;
311
    }
312
 
1921 vikas 313
    public String getPriceUrl() {
314
        return this.priceUrl;
315
    }
316
 
627 rajveer 317
    public List<String[]> getCrumbs() {
318
    	return this.crumbs;
319
    }
320
 
1200 chandransh 321
    public Map<String, String> getSnippets(){
517 rajveer 322
    	if(results != null){
323
    		snippets = new HashMap<String, String>();	
790 vikas 324
	    	for(String docId: results){
1200 chandransh 325
				try {
326
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
327
				} catch (IOException e) {
328
					log.error(e);
329
				}
517 rajveer 330
			}
331
    	}
332
		return snippets;
333
    }
334
 
317 ashish 335
}