Subversion Repositories SmartDukaan

Rev

Rev 6430 | Rev 6842 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2846 mandeep.dh 1
package in.shop2020.serving.controllers;
2
 
6520 amit.gupta 3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
3561 rajveer 5
import in.shop2020.serving.cache.SnippetCacheWrapper;
2846 mandeep.dh 6
import in.shop2020.serving.services.SolrSearchService;
3303 rajveer 7
import in.shop2020.serving.services.SpecialPageConfigurer;
6430 amit.gupta 8
import in.shop2020.serving.utils.Utils;
6520 amit.gupta 9
import in.shop2020.thrift.clients.CatalogClient;
2846 mandeep.dh 10
 
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;
2948 chandransh 19
import java.util.Map.Entry;
2846 mandeep.dh 20
 
21
import org.apache.commons.lang.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.apache.struts2.convention.annotation.Result;
6520 amit.gupta 24
import org.apache.struts2.convention.annotation.Results;
2846 mandeep.dh 25
import org.json.JSONException;
26
 
27
/**
28
 * @author rajveer
29
 *
30
 */
6520 amit.gupta 31
@Results({
32
	@Result(name = "index", location = "special-page-index.vm"),
33
	@Result(name = "tag-index", location = "tag-page-index.vm")
34
})
2846 mandeep.dh 35
public class SpecialPageController extends BaseController {
36
    private static final long serialVersionUID = 1L;
37
    private static Logger log = Logger.getLogger(Class.class);
38
 
6430 amit.gupta 39
/*	private static final String[] facetDefIDs = 
2846 mandeep.dh 40
	    new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
41
 
42
	private static final String[] facetLabels = 
43
	    new String[] {"Category","Sub Category","Price", "Brand", "Data Connectivity", "Camera Resolution" };
6430 amit.gupta 44
*/
2846 mandeep.dh 45
	private List<String> results;
46
	private Map<String, String> snippets;
47
	private Map<String, List<String[]>> facets;
48
	private List<String[]> crumbs;
6520 amit.gupta 49
	private List<String> tagSnippets = new ArrayList<String>();
2846 mandeep.dh 50
	private String specialPageName;
51
	private String specialPageTitle;
52
	private String facetSelection = "";
53
 
54
	private String query;
55
 
56
	private Double minPrice;
57
	private Double maxPrice;
58
 
6520 amit.gupta 59
	private long type = 0;
2846 mandeep.dh 60
	private long windowSize = 20;
61
	private long page = 1;
62
	private long totalResults;
63
	private long beginIndex = 0;
64
	private String url;
65
	private String priceUrl = "?";
3303 rajveer 66
	private String uri;
67
 
68
	public String index() throws SecurityException, IOException, JSONException {
69
		uri = request.getRequestURI();
3304 rajveer 70
		uri = uri.replace("/", "");
2846 mandeep.dh 71
	    log.info("Uri: " + uri);
3303 rajveer 72
	    specialPageName  = SpecialPageConfigurer.getSpecialPageNameFromUri(uri);
73
	    specialPageTitle = SpecialPageConfigurer.getSpecialPageTitleFromUri(uri);
74
	    query            = SpecialPageConfigurer.getQueryFromUri(uri);
6520 amit.gupta 75
	    type            =  SpecialPageConfigurer.getPageType(uri);
2846 mandeep.dh 76
 
77
    	String[] fqrys = this.request.getParameterValues("fq");
78
 
79
    	url = "?";
80
 
3320 rajveer 81
    	String urlCrumb = url;
2846 mandeep.dh 82
 
3320 rajveer 83
    	this.crumbs = new ArrayList<String[]>();
6430 amit.gupta 84
    	List<String> arrList = Arrays.asList(Utils.facetDefIDs);
3320 rajveer 85
 
2846 mandeep.dh 86
    	if(fqrys!= null){
3320 rajveer 87
    		for(int i=0; i<fqrys.length; i++) {
88
        		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
89
        		priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
90
        		String filterUrl = url;
91
        		String facetName = StringUtils.split(fqrys[i], ":")[0];
92
        		String facetValue = StringUtils.split(fqrys[i], ":")[1];
6430 amit.gupta 93
        		String facetLabel = Utils.facetLabels[arrList.indexOf(facetName)];
3320 rajveer 94
        		this.specialPageTitle += " | " + facetLabel + " " + facetValue;
95
        		this.facetSelection += facetValue + " ";
96
        		for(int j=0; j<fqrys.length; j++) {
97
        			if(i==j){
98
        				continue;
99
        			}
100
        			filterUrl += "&fq=" + URLEncoder.encode(fqrys[j], "UTF-8");
101
    	    	}
102
        		String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
103
        		log.info("acrumb=" + Arrays.toString(acrumb));
104
        		this.crumbs.add(acrumb);
105
        	}
2846 mandeep.dh 106
    	}
107
 
108
 
109
		url = urlCrumb;
110
 
111
    	if(this.request.getParameter("page") != null){
112
    		this.page = Long.parseLong(this.request.getParameter("page"));
113
    		this.beginIndex = this.windowSize * (this.page-1);
114
    	}
115
 
3173 rajveer 116
    	if(this.request.getParameter("minPrice") != null){
117
    		this.minPrice = (new Double(this.request.getParameter("minPrice")));
118
    		url= url + "&minPrice=" + this.request.getParameter("minPrice");
2846 mandeep.dh 119
    	}
120
 
3173 rajveer 121
    	if(this.request.getParameter("maxPrice") != null){
122
    		this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
123
    		url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
2846 mandeep.dh 124
    	}
3561 rajveer 125
 
6520 amit.gupta 126
    	if (type==0) {
127
	    	String sortOrder = "F_50002+asc";
128
 
129
	    	SolrSearchService search = new SolrSearchService(query, fqrys, Utils.facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, sortOrder, sourceId);
130
	    	this.results =  search.getResultMap(); 
131
 
132
 
133
	    	this.facets = new LinkedHashMap<String, List<String[]>>();
134
	    	String qryString = this.request.getQueryString();
135
	    	log.info("qryString=" + qryString);
136
 
137
	    	for (int i=0; i<Utils.facetDefIDs.length; i++) {
138
	    		String facetDefID = Utils.facetDefIDs[i];
139
	    		String facetLabel = Utils.facetLabels[i];
140
	    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
141
	    		if(facetDetailMap==null)
142
	    			continue;
143
	    		List<String[]> values = new ArrayList<String[]>();
144
	    		String drilldownURL = url + "&fq=" + facetDefID + ":";
145
	    		for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
146
	    		    String facet = facetEntry.getKey();
147
 
148
	    			String[] afacet = new String[] { facet, 
149
	      					facetEntry.getValue().toString(), drilldownURL  + URLEncoder.encode(facet, "UTF-8")  };
150
 
151
	      			values.add(afacet);
152
	    		}
153
 
154
	    		this.facets.put(facetLabel, values);
155
	    	}
156
 
157
	    	Map<String, Double> priceMap = search.getPriceStatsMap();
158
	    	if(priceMap != null){
159
	    		this.minPrice = priceMap.get("min");
160
	    		this.maxPrice = priceMap.get("max");
161
	    	}
162
	    	else{
163
	    		this.minPrice = 0.0;
164
	    		this.maxPrice = 0.0;
165
	    	}
166
 
167
	    	this.totalResults = search.getTotalResults();
168
	        return "index";
169
    	} else {
170
    		return "tag-index";
2846 mandeep.dh 171
    	}
172
    }
173
 
174
    public String getPageMetaDesc() {
175
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
176
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
177
    }
178
 
3303 rajveer 179
 
2846 mandeep.dh 180
    public String getPageMetaKeywords() throws JSONException {
3303 rajveer 181
       	return SpecialPageConfigurer.getPageMetaKeywords(uri);
2846 mandeep.dh 182
    }
183
 
184
	public long getBeginIndex(){
185
		if(totalResults>0)
186
			return beginIndex+1;
187
		return beginIndex;
188
	}
189
 
190
	public long getTotalPages() {
191
		return 1 + (totalResults-1)/windowSize;
192
	}
193
 
3329 rajveer 194
    public Map<String, String> getSnippets(){
195
        if(results != null){
196
            snippets = new HashMap<String, String>();   
197
            for(String docId: results){
3561 rajveer 198
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3329 rajveer 199
                if (snippet != null) {
200
                    snippets.put(docId, snippet);
201
                }
202
            }
203
        }
204
        return snippets;
2846 mandeep.dh 205
    }
206
 
207
    public long getCurrentPage() {
208
        return this.page;
209
    }
210
 
211
    public Double getMinPrice() {
212
        return this.minPrice;
213
    }
214
 
215
    public Double getMaxPrice() {
216
        return this.maxPrice;
217
    }
218
 
219
    public List<String[]> getCrumbs() {
220
        return this.crumbs;
221
    }
222
    public String getSpecialPageTitle() {
223
        return this.specialPageTitle;
224
    }   
225
 
226
    public String getQuery() {
227
        return this.query;
228
    }
229
 
230
    public List<String> getResults() {
231
        return this.results;
232
    }
233
 
234
    public Map<String, List<String[]>> getFacets() {
235
        return this.facets;
236
    }
237
 
238
    public long getTotalResults(){
239
        return totalResults;
240
    }
241
 
242
    public String getUrl(){
243
        return this.url;
244
    }
245
 
246
    public String getPriceUrl(){
247
        return this.priceUrl;
248
    }
249
 
250
    public String getSpecialPageName() {
251
        return this.specialPageName;
6520 amit.gupta 252
    }
253
 
254
	public void addEntityId(Long entityId, List<String> snippets)	{
255
 
256
	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, entityId+"", sourceId);
257
 
258
	    if (snippet != null)	{
259
	        snippets.add(snippet);
260
	    }
261
	}
262
 
263
	public List<String> getTagSnippets() {
264
		CatalogClient catalogClientService;
265
		try {
266
			catalogClientService = new CatalogClient();
267
			Client cc = catalogClientService.getClient();
268
			for (long entityId : cc.getAllEntitiesByTagName(uri)) {
269
				addEntityId(entityId, tagSnippets);
270
			}
271
		} catch (Exception e) {
272
			// TODO Auto-generated catch block
273
			e.printStackTrace();
274
		}
275
		return tagSnippets;
276
	}
2846 mandeep.dh 277
}