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.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.HashMap;
025    import java.util.Map;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class SearchContextFactory {
033    
034            public static SearchContext getInstance(HttpServletRequest request) {
035                    SearchContext searchContext = new SearchContext();
036    
037                    // Theme display
038    
039                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
040                            WebKeys.THEME_DISPLAY);
041    
042                    searchContext.setCompanyId(themeDisplay.getCompanyId());
043                    searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
044                    searchContext.setLayout(themeDisplay.getLayout());
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                    Map<String, String[]> parameters = request.getParameterMap();
055    
056                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
057                            String name = entry.getKey();
058                            String[] values = entry.getValue();
059    
060                            if ((values != null) && (values.length > 0)) {
061                                    if (values.length == 1) {
062                                            attributes.put(name, values[0]);
063                                    }
064                                    else {
065                                            attributes.put(name, values);
066                                    }
067                            }
068                    }
069    
070                    searchContext.setAttributes(attributes);
071    
072                    // Asset
073    
074                    long[] assetCategoryIds = StringUtil.split(
075                            ParamUtil.getString(request, "assetCategoryIds"), 0L);
076    
077                    String[] assetTagNames = StringUtil.split(
078                            ParamUtil.getString(request, "assetTagNames"));
079    
080                    searchContext.setAssetCategoryIds(assetCategoryIds);
081                    searchContext.setAssetTagNames(assetTagNames);
082    
083                    // Keywords
084    
085                    String keywords = ParamUtil.getString(request, "keywords");
086    
087                    searchContext.setKeywords(keywords);
088    
089                    // Query config
090    
091                    QueryConfig queryConfig = new QueryConfig();
092    
093                    queryConfig.setLocale(themeDisplay.getLocale());
094    
095                    searchContext.setQueryConfig(queryConfig);
096    
097                    return searchContext;
098            }
099    
100    }