001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.theme.ThemeDisplay;
021    
022    import java.io.Serializable;
023    
024    import java.util.Enumeration;
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.setLocale(themeDisplay.getLocale());
046                    searchContext.setTimeZone(themeDisplay.getTimeZone());
047                    searchContext.setUserId(themeDisplay.getUserId());
048    
049                    // Attributes
050    
051                    Map<String, Serializable> attributes =
052                            new HashMap<String, Serializable>();
053    
054                    Enumeration<String> enu = request.getParameterNames();
055    
056                    while (enu.hasMoreElements()) {
057                            String param = enu.nextElement();
058    
059                            String[] values = request.getParameterValues(param);
060    
061                            if ((values != null) && (values.length > 0)) {
062                                    if (values.length == 1) {
063                                            attributes.put(param, values[0]);
064                                    }
065                                    else {
066                                            attributes.put(param, 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    }