Subversion Repositories SmartDukaan

Rev

Rev 6520 | Rev 6866 | 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
 
6842 amit.gupta 126
    	if (type!=0) {
127
    		query = "*&fq=F_50029:"+uri;
2846 mandeep.dh 128
    	}
6842 amit.gupta 129
 
130
    	String sortOrder = "F_50002+asc";
131
 
132
    	SolrSearchService search = new SolrSearchService(query, fqrys, Utils.facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, sortOrder, sourceId);
133
    	this.results =  search.getResultMap(); 
134
 
135
 
136
    	this.facets = new LinkedHashMap<String, List<String[]>>();
137
    	String qryString = this.request.getQueryString();
138
    	log.info("qryString=" + qryString);
139
 
140
    	for (int i=0; i<Utils.facetDefIDs.length; i++) {
141
    		String facetDefID = Utils.facetDefIDs[i];
142
    		String facetLabel = Utils.facetLabels[i];
143
    		Map<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
144
    		if(facetDetailMap==null)
145
    			continue;
146
    		List<String[]> values = new ArrayList<String[]>();
147
    		String drilldownURL = url + "&fq=" + facetDefID + ":";
148
    		for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
149
    		    String facet = facetEntry.getKey();
150
 
151
    			String[] afacet = new String[] { facet, 
152
      					facetEntry.getValue().toString(), drilldownURL  + URLEncoder.encode(facet, "UTF-8")  };
153
 
154
      			values.add(afacet);
155
    		}
156
 
157
    		this.facets.put(facetLabel, values);
158
    	}
159
 
160
    	Map<String, Double> priceMap = search.getPriceStatsMap();
161
    	if(priceMap != null){
162
    		this.minPrice = priceMap.get("min");
163
    		this.maxPrice = priceMap.get("max");
164
    	}
165
    	else{
166
    		this.minPrice = 0.0;
167
    		this.maxPrice = 0.0;
168
    	}
169
 
170
    	this.totalResults = search.getTotalResults();
171
        return "index";
172
	}
2846 mandeep.dh 173
 
6842 amit.gupta 174
 
2846 mandeep.dh 175
    public String getPageMetaDesc() {
176
    	return "Best price " + this.facetSelection + this.specialPageName.replaceAll("Phones", "mobile phones") 
177
    	    + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
178
    }
179
 
3303 rajveer 180
 
2846 mandeep.dh 181
    public String getPageMetaKeywords() throws JSONException {
3303 rajveer 182
       	return SpecialPageConfigurer.getPageMetaKeywords(uri);
2846 mandeep.dh 183
    }
184
 
185
	public long getBeginIndex(){
186
		if(totalResults>0)
187
			return beginIndex+1;
188
		return beginIndex;
189
	}
190
 
191
	public long getTotalPages() {
192
		return 1 + (totalResults-1)/windowSize;
193
	}
194
 
3329 rajveer 195
    public Map<String, String> getSnippets(){
196
        if(results != null){
197
            snippets = new HashMap<String, String>();   
198
            for(String docId: results){
3561 rajveer 199
	    	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
3329 rajveer 200
                if (snippet != null) {
201
                    snippets.put(docId, snippet);
202
                }
203
            }
204
        }
205
        return snippets;
2846 mandeep.dh 206
    }
207
 
208
    public long getCurrentPage() {
209
        return this.page;
210
    }
211
 
212
    public Double getMinPrice() {
213
        return this.minPrice;
214
    }
215
 
216
    public Double getMaxPrice() {
217
        return this.maxPrice;
218
    }
219
 
220
    public List<String[]> getCrumbs() {
221
        return this.crumbs;
222
    }
223
    public String getSpecialPageTitle() {
224
        return this.specialPageTitle;
225
    }   
226
 
227
    public String getQuery() {
228
        return this.query;
229
    }
230
 
231
    public List<String> getResults() {
232
        return this.results;
233
    }
234
 
235
    public Map<String, List<String[]>> getFacets() {
236
        return this.facets;
237
    }
238
 
239
    public long getTotalResults(){
240
        return totalResults;
241
    }
242
 
243
    public String getUrl(){
244
        return this.url;
245
    }
246
 
247
    public String getPriceUrl(){
248
        return this.priceUrl;
249
    }
250
 
251
    public String getSpecialPageName() {
252
        return this.specialPageName;
6520 amit.gupta 253
    }
254
 
255
	public void addEntityId(Long entityId, List<String> snippets)	{
256
 
257
	    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, entityId+"", sourceId);
258
 
259
	    if (snippet != null)	{
260
	        snippets.add(snippet);
261
	    }
262
	}
263
 
264
	public List<String> getTagSnippets() {
265
		CatalogClient catalogClientService;
266
		try {
267
			catalogClientService = new CatalogClient();
268
			Client cc = catalogClientService.getClient();
269
			for (long entityId : cc.getAllEntitiesByTagName(uri)) {
270
				addEntityId(entityId, tagSnippets);
271
			}
272
		} catch (Exception e) {
273
			// TODO Auto-generated catch block
274
			e.printStackTrace();
275
		}
276
		return tagSnippets;
277
	}
2846 mandeep.dh 278
}