Subversion Repositories SmartDukaan

Rev

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