001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.theme.ThemeDisplay;
022    
023    import java.io.Serializable;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class SearchContextFactory {
034    
035            public static SearchContext getInstance(HttpServletRequest request) {
036                    SearchContext searchContext = new SearchContext();
037    
038                    // Theme display
039    
040                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
041                            WebKeys.THEME_DISPLAY);
042    
043                    searchContext.setCompanyId(themeDisplay.getCompanyId());
044                    searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
045                    searchContext.setLayout(themeDisplay.getLayout());
046                    searchContext.setLocale(themeDisplay.getLocale());
047                    searchContext.setTimeZone(themeDisplay.getTimeZone());
048                    searchContext.setUserId(themeDisplay.getUserId());
049    
050                    // Attributes
051    
052                    Map<String, Serializable> attributes =
053                            new HashMap<String, Serializable>();
054    
055                    Map<String, String[]> parameters = request.getParameterMap();
056    
057                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
058                            String name = entry.getKey();
059                            String[] values = entry.getValue();
060    
061                            if (ArrayUtil.isNotEmpty(values)) {
062                                    if (values.length == 1) {
063                                            attributes.put(name, values[0]);
064                                    }
065                                    else {
066                                            attributes.put(name, values);
067                                    }
068                            }
069                    }
070    
071                    searchContext.setAttributes(attributes);
072    
073                    // Asset
074    
075                    long[] assetCategoryIds = StringUtil.split(
076                            ParamUtil.getString(request, "assetCategoryIds"), 0L);
077    
078                    String[] assetTagNames = StringUtil.split(
079                            ParamUtil.getString(request, "assetTagNames"));
080    
081                    searchContext.setAssetCategoryIds(assetCategoryIds);
082                    searchContext.setAssetTagNames(assetTagNames);
083    
084                    // Keywords
085    
086                    String keywords = ParamUtil.getString(request, "keywords");
087    
088                    searchContext.setKeywords(keywords);
089    
090                    // Query config
091    
092                    QueryConfig queryConfig = new QueryConfig();
093    
094                    queryConfig.setLocale(themeDisplay.getLocale());
095    
096                    searchContext.setQueryConfig(queryConfig);
097    
098                    return searchContext;
099            }
100    
101    }