Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7226 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
3
import in.shop2020.datalogger.EventType;
4
import in.shop2020.model.v1.catalog.Category;
5
//import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
6
//import in.shop2020.serving.cache.SnippetCacheWrapper;
7
import in.shop2020.serving.service.SearchService;
8
import in.shop2020.serving.utils.Utils;
9
import in.shop2020.utils.CategoryManager;
10
import in.shop2020.utils.DataLogger;
11
 
12
import java.io.UnsupportedEncodingException;
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;
20
import java.util.Map.Entry;
21
 
22
import org.apache.commons.lang.StringUtils;
23
import org.apache.log4j.Logger;
24
import org.apache.struts2.convention.annotation.Result;
25
 
26
import com.google.gson.Gson;
27
 
28
 
29
/**
30
 * @author rajveer
31
 *
32
 */
33
@Result(name="redirect", location="${location}", type="redirect")
34
public class SearchController extends BaseController {
35
 
36
    private static final long serialVersionUID = -8392433517042806559L;
37
    private static Logger log = Logger.getLogger(Class.class);
38
 
39
    private List<String> results;
40
    private Map<String, Double> dynamicSearchMap;
41
 
42
    /**
43
     * 
44
     */
45
    private Map<String, String> snippets;
46
 
47
    /**
48
     * 
49
     */
50
    private Map<String, List<String[]>> facets = null;
51
 
52
    /**
53
     * 
54
     */
55
    private Map<String, List<List<String>>> crumbs;
56
 
57
    private String  query;
7255 amit.gupta 58
    private String  q;
7226 anupam.sin 59
    private String sortOrder = null;
60
    private Double minPrice = null;
61
    private Double maxPrice = null;
62
 
63
    private long windowSize = 20;
64
    private long page = 1;
65
    private long totalResults;
66
    private long beginIndex = 0;
67
    private String url;
68
    private String priceUrl = "?";
69
    private String sortUrl;
70
    private String location;
71
    private static HashMap<String, List<String>> categoriesChildren = null;
72
    private static Map<Long, String> categoryLabelMap = null;
73
 
74
    /**
75
     * 
76
     * @return
77
     * @throws UnsupportedEncodingException
78
     */
79
    // GET /query
80
 
81
    public String index() {
82
 
83
        log.info("this.request=" + this.request);
84
 
85
        //url = this.request.getRequestURL();
86
        query = this.request.getParameter("q");
7255 amit.gupta 87
        q = this.query;
7226 anupam.sin 88
        try {
89
            url = "q="+ URLEncoder.encode(query, "UTF-8");
90
 
91
            if(query.trim().isEmpty()){
92
                    location = "/";
93
                    return "redirect";
94
            }
95
            sortUrl = url;
96
            if(this.request.getParameter("sort") != null){
97
                url= url + "&sort=" + this.request.getParameter("sort");
98
                sortOrder = this.request.getParameter("sort");
99
            }
100
            priceUrl += url;
101
            if(this.request.getParameter("page") != null){
102
                this.page = Long.parseLong(this.request.getParameter("page"));
103
                this.beginIndex = this.windowSize * (this.page-1);
104
            }
105
            if(this.request.getParameter("minPrice") != null){
106
                this.minPrice = (new Double(this.request.getParameter("minPrice")));
107
                url= url + "&minPrice=" + this.request.getParameter("minPrice");
108
            }
109
            if(this.request.getParameter("maxPrice") != null){
110
                this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
111
                url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
112
            }       
113
 
114
 
115
            String[] fqrys = this.request.getParameterValues("fq");
116
 
117
            this.crumbs = new HashMap<String, List<List<String>>>();
118
 
119
 
120
            String urlCrumb = "";
121
            if(query != null) {
122
                urlCrumb = url; 
123
            }else {
124
 
125
            }
126
 
127
            if(fqrys != null) {
128
                log.info("fqrys=" + Arrays.toString(fqrys));
129
                String filterUrl = "";
130
                for(int i=0; i<fqrys.length; i++){
131
                    urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
132
                    priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
133
                    sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
134
                    String facetName = StringUtils.split(fqrys[i], ":")[0];
135
                    String facetValue = StringUtils.split(fqrys[i], ":")[1];
136
                    String facetLabel = Utils.FACET_LABEL_MAP.get(facetName);
137
                    filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
138
                    List<String> acrumb = Arrays.asList(facetValue, filterUrl);
139
                    if(!crumbs.containsKey(facetLabel)) {
140
                        crumbs.put(facetLabel, new ArrayList<List<String>>());
141
                    }
142
 
143
                    this.crumbs.get(facetLabel).add(acrumb);
144
                }
145
            }
146
            url = urlCrumb;
147
 
148
 
149
 
150
            SearchService search = new SearchService(query, fqrys, (page-1)*windowSize, windowSize, minPrice, maxPrice, sortOrder, sourceId);
151
 
152
            this.results =  search.getResultMap(); 
153
            this.dynamicSearchMap = search.getDynamicPriceMap();
154
            // Facets
155
            List<String> fqs = null;
156
            if (fqrys != null) {
157
                fqs = Arrays.asList(fqrys);
158
            }else {
159
                fqs = new ArrayList<String>();
160
            }
161
 
162
            setFacet(fqs, search);
163
 
164
            //If 
165
            Map<String, Double> priceMap = search.getPriceStatsMap();
166
            if(priceMap != null){
167
                if (this.minPrice == null){
168
                    this.minPrice = priceMap.get("min");
169
                }
170
                if(this.maxPrice == null) {
171
                    this.maxPrice = priceMap.get("max");
172
                }
173
            } else{
174
                this.minPrice = 0.0;
175
                this.maxPrice = 0.0;
176
            }
177
 
178
            this.totalResults = search.getTotalResults();
179
        } catch (Exception e) {
180
            // TODO Auto-generated catch block
181
            e.printStackTrace();
182
        }
183
        return "index";
184
    }
185
 
186
 
187
    private void setFacet(List<String> fqs, SearchService search) throws Exception{
188
 
189
        this.facets = new LinkedHashMap<String, List<String[]>>();
190
        List<String> filtrableFacets = search.getFilterableFacets();
191
        for (String filtrableFacet : filtrableFacets) {
192
 
193
            Map<String, Integer> facetDetailMap = search.getFacetDetails(filtrableFacet);
194
            if(facetDetailMap==null)
195
                continue;
196
            List<String[]> values = new ArrayList<String[]>();
197
            for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){
198
                String selected = "";
199
                if(fqs.contains(filtrableFacet + ":" + facetEntry.getKey())) {
200
                    selected = "checked=\"checked\"";
201
                }
202
                String facet = facetEntry.getKey();
203
                String drilldownURL = "&fq=" +  
204
                    URLEncoder.encode( filtrableFacet + ":" + facet, "UTF-8"); 
205
                String[] afacet = new String[] { facet, 
206
                      facetEntry.getValue().toString(), drilldownURL,  selected};
207
                values.add(afacet);    
208
            }
209
 
210
            this.facets.put(Utils.FACET_LABEL_MAP.get(filtrableFacet), values);
211
        }
212
    }
213
 
214
 
215
//    public Map<String, String> getSnippets() throws Exception {
216
//        if(results != null){
217
//            snippets = new HashMap<String, String>();   
218
//            for(String docId: results){
219
//                String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
220
//                if (snippet != null) {
221
//                    snippets.put(docId, snippet);
222
//                }
223
//            }
224
//        }
225
//        return snippets;
226
//    }
227
 
228
    /**
229
     * 
230
     * @return
231
     */
232
    public String getQuery() {
233
        return this.query;
234
    }
235
 
236
 
237
    /**
238
     * 
239
     * @return
240
     */
241
    public List<String> getResults() {
242
        return this.results;
243
    }
244
 
245
    /**
246
     * 
247
     * @return
248
     */
249
    public Map<String, List<String[]>> getFacets() {
250
        return this.facets;
251
    }
252
 
253
    public long getTotalResults(){
254
        return totalResults;
255
    }
256
 
257
    public String getUrl(){
258
        return this.url;
259
    }
260
 
261
    public String getPriceUrl(){
262
        return this.priceUrl;
263
    }
264
 
265
    public String getSortUrl(){
266
        return this.sortUrl;
267
    }
268
 
269
    public long getBeginIndex(){
270
        if(totalResults>0)
271
            return beginIndex+1;
272
        return beginIndex;
273
    }
274
 
275
    public long getTotalPages() {
276
        return 1 + (totalResults-1)/windowSize;
277
    }
278
 
279
    public long getCurrentPage() {
280
        return this.page;
281
    }
282
 
283
    public Double getMinPrice() {
284
        return this.minPrice;
285
    }
286
 
287
    public Double getMaxPrice() {
288
        return this.maxPrice;
289
    }
290
 
291
    public Map<String, List<List<String>>> getCrumbs() {
292
        return this.crumbs;
293
    }
294
 
295
    public String getSortOrder(){
296
        return this.sortOrder;
297
    }
298
 
299
    public String getLocation(){
300
        return this.location;
301
    }
302
 
303
    public List<String> getChildren(String categoryLabel) {
304
        return getCategoriesChildren().get(categoryLabel);
305
    }
306
 
307
    public static Map<String, List<String>> getCategoriesChildren(){
308
        Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
309
        if (categoriesChildren == null) {
310
            categoriesChildren = new HashMap<String, List<String>>();
311
            for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
312
                idLabelMap.put(category.getId(), category.getLabel());
313
            }
314
            for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
315
                if(category.getParent_category_id() != 10000 && category.getParent_category_id() != 0 &&
316
                        category.getParent_category_id() != Utils.MOBILE_PHONES_CATEGORY){
317
                    String parentLabel = idLabelMap.get(category.getParent_category_id());
318
                    if(!categoriesChildren.containsKey(parentLabel)){
319
                        categoriesChildren.put(parentLabel, new ArrayList<String>());
320
                    }
321
                    categoriesChildren.get(parentLabel).add(category.getLabel());
322
                }
323
            }
324
        }
325
 
326
        return categoriesChildren;
327
    }
328
 
329
    public static String getCategoryLabel(Long id){
330
        if(categoryLabelMap == null){
331
            Map<Long,String> idLabelMap = new HashMap<Long, String>(); 
332
            for (Category category: CategoryManager.getCategoryManager().getCategories().values()){
333
                idLabelMap.put(category.getId(), category.getLabel());
334
            }
335
            categoryLabelMap = idLabelMap;
336
        } 
337
        return categoryLabelMap.get(id);
338
    }
339
 
340
    public String getDynamicSearchMap(){
341
        if (this.dynamicSearchMap == null) {
342
            return "{}";
343
        } else {
344
            return new Gson().toJson(this.dynamicSearchMap);
345
        }
346
    }
7255 amit.gupta 347
 
348
 
349
	public void setQ(String q) {
350
		this.q = q;
351
	}
352
 
353
 
354
	public String getQ() {
355
		return q;
356
	}
7226 anupam.sin 357
}