Subversion Repositories SmartDukaan

Rev

Rev 2505 | Rev 2948 | 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"),
2506 rajveer 74
		@Action("/all-tablets"),
1472 rajveer 75
 
974 vikas 76
		@Action("/business-phones"),
77
		@Action("/high-end-multimedia-phones"),
78
		@Action("/low-end-multimedia-phones"),
79
		@Action("/basic-phones"),
80
 
81
		@Action("/bluetooth-headset"),
82
		@Action("/memory-card"),
83
		@Action("/battery"),
84
		@Action("/headset"),
85
		@Action("/charger"),
86
		@Action("/pen-drive"),
87
		@Action("/carrying-case"),
1005 vikas 88
		@Action("/car-charger"),
1306 chandransh 89
		@Action("/screen-guard"),
1005 vikas 90
 
1534 rajveer 91
		@Action("/face-plate"),
92
		@Action("/decal"),
93
		@Action("/data-cable"),
94
		@Action("/ear-buds"),
95
		@Action("/cleaning-kit"),
96
		@Action("/speaker"),
97
 
98
 
99
 
1005 vikas 100
		@Action("/category")
974 vikas 101
	})
102
 
317 ashish 103
    public HttpHeaders show() throws SecurityException, IOException {
104
    	log.info("id=" + id);
1472 rajveer 105
    	long categoryId = Long.parseLong(id);
106
    	this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
974 vikas 107
 
2505 rajveer 108
    	if( categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
1472 rajveer 109
    		this.categoryTitle = "Mobile Price List India" + " | " + this.categoryName;
110
    	}else{
111
    		this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
112
    	}
113
 
114
 
317 ashish 115
 
517 rajveer 116
		String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
117
 
118
    	String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution" 	};
119
 
120
    	String[] fqrys = this.request.getParameterValues("fq");
545 rajveer 121
 
122
    	url = "?";
123
 
517 rajveer 124
    	query = "*";
125
 
536 rajveer 126
    	int length = 1;
127
 
128
    	if(fqrys!= null){
129
    		length += fqrys.length;
130
    	}
131
 
132
    	String[] newfqrys = new String[length];
2306 vikas 133
    	if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
820 rajveer 134
    		newfqrys[0] = "F_50010:"+categoryName;
135
    	}else{
136
    		newfqrys[0] = "F_50011:"+categoryName;
137
    	}
138
 
545 rajveer 139
    	String urlCrumb = url;
536 rajveer 140
 
620 rajveer 141
    	this.crumbs = new ArrayList<String[]>();
142
    	List<String> arrList = Arrays.asList(facetDefIDs);
536 rajveer 143
    	for(int i=1; i<length; i++) {
144
    		newfqrys[i] = fqrys[i-1];
545 rajveer 145
    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
1921 vikas 146
    		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
620 rajveer 147
    		String filterUrl = url;
148
    		String facetName = StringUtils.split(fqrys[i-1], ":")[0];
149
    		String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
150
    		String facetLabel = facetLabels[arrList.indexOf(facetName)];
1257 vikas 151
    		this.categoryTitle += " | " + facetLabel + " " + facetValue;
1259 vikas 152
    		this.facetSelection += facetValue + " ";
620 rajveer 153
    		for(int j=1; j<length; j++) {
154
    			if(i==j){
155
    				continue;
156
    			}
157
    			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
158
	    	}
159
    		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
160
    		log.info("acrumb=" + Arrays.toString(acrumb));
161
    		this.crumbs.add(acrumb);
162
 
536 rajveer 163
    	}
545 rajveer 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
    	}    	
1377 rajveer 178
 
179
    	query = query + "&sort=F_50002+asc";
536 rajveer 180
 
2306 vikas 181
    	SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, null);
517 rajveer 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[]>();
545 rajveer 196
    		String drilldownURL = new String();
517 rajveer 197
    		for(String facet: facetDetailMap.keySet()){
536 rajveer 198
 
545 rajveer 199
    			/*
536 rajveer 200
    			if(qryString != null){
201
    				drilldownURL = qryString;
545 rajveer 202
 
536 rajveer 203
    			}
545 rajveer 204
    			*/
205
        		drilldownURL = url;
536 rajveer 206
 
545 rajveer 207
 
208
    			drilldownURL = drilldownURL + "&fq=" + facetDefID + ":" + 
209
    				URLEncoder.encode(facet, "UTF-8");
517 rajveer 210
      			String[] afacet = new String[] { facet, 
211
      					facetDetailMap.get(facet).toString(), drilldownURL  };
212
      			values.add(afacet);    
545 rajveer 213
 
517 rajveer 214
    		}
215
 
216
    		this.facets.put(facetLabel, values);
217
    	}
218
 
219
 
545 rajveer 220
 
517 rajveer 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
    	}
545 rajveer 229
    	this.totalResults = search.getTotalResults();
317 ashish 230
        return new DefaultHttpHeaders("show");
231
    }
232
 
233
    public void setId(String id) {
536 rajveer 234
    	StringTokenizer tokenizer = new StringTokenizer(id,"-");
235
    	while(tokenizer.hasMoreTokens()){
236
    		this.id = tokenizer.nextToken();
237
    	}
317 ashish 238
    }
239
 
240
    public String getId() {
241
    	return this.id;
242
    }
517 rajveer 243
 
536 rajveer 244
    public String getCategoryName() {
245
    	return this.categoryName;
942 chandransh 246
    }	
517 rajveer 247
 
974 vikas 248
    public String getPageMetaDesc() {
1966 vikas 249
        return "Best price "
250
                + this.facetSelection.replaceAll("Phones", "")
251
                + this.categoryName
252
                        .replaceAll("Mobile Phones", "mobile phones")
253
                        .replaceAll("Phones", "mobile phones")
254
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
974 vikas 255
    }
256
 
257
    public String getPageMetaKeywords() {
2306 vikas 258
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
974 vikas 259
        	return this.categoryName + ", mobile phone";
260
    	}
2306 vikas 261
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
974 vikas 262
        	return this.categoryName + ", phone accessories";
263
    	}
264
        return "";
265
    }
266
 
267
    public String getCategoryTitle() {
268
    	return this.categoryTitle;
269
    }	
270
 
650 rajveer 271
	public String getQuery() {
517 rajveer 272
    	return this.query;
273
    }
274
 
275
 
790 vikas 276
    public List<String> getResults() {
517 rajveer 277
    	return this.results;
278
    }
279
 
280
    public Map<String, List<String[]>> getFacets() {
281
    	return this.facets;
282
    }
283
 
545 rajveer 284
    public long getTotalResults(){
285
    	return totalResults;
286
    }
287
 
288
    public String getUrl(){
289
    	return this.url;
290
    }
291
 
292
	public long getBeginIndex(){
293
		if(totalResults>0)
294
			return beginIndex+1;
295
		return beginIndex;
296
	}
297
 
298
	public long getTotalPages() {
1376 rajveer 299
		return 1 + (totalResults-1)/windowSize;
545 rajveer 300
	}
301
 
302
	public long getCurrentPage() {
303
		return this.page;
304
	}
305
 
517 rajveer 306
    public Double getMinPrice() {
307
    	return this.minPrice;
308
    }
309
 
310
    public Double getMaxPrice() {
311
    	return this.maxPrice;
312
    }
313
 
1921 vikas 314
    public String getPriceUrl() {
315
        return this.priceUrl;
316
    }
317
 
627 rajveer 318
    public List<String[]> getCrumbs() {
319
    	return this.crumbs;
320
    }
321
 
1200 chandransh 322
    public Map<String, String> getSnippets(){
517 rajveer 323
    	if(results != null){
324
    		snippets = new HashMap<String, String>();	
790 vikas 325
	    	for(String docId: results){
1200 chandransh 326
				try {
327
					snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
328
				} catch (IOException e) {
329
					log.error(e);
330
				}
517 rajveer 331
			}
332
    	}
333
		return snippets;
334
    }
335
 
317 ashish 336
}