Subversion Repositories SmartDukaan

Rev

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