001    /**
002     * Copyright (c) 2000-present 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.model.Layout;
022    import com.liferay.portal.theme.ThemeDisplay;
023    
024    import java.io.Serializable;
025    
026    import java.util.HashMap;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.TimeZone;
030    
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class SearchContextFactory {
037    
038            public static SearchContext getInstance(HttpServletRequest request) {
039                    SearchContext searchContext = new SearchContext();
040    
041                    // Theme display
042    
043                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
044                            WebKeys.THEME_DISPLAY);
045    
046                    searchContext.setCompanyId(themeDisplay.getCompanyId());
047                    searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
048                    searchContext.setLayout(themeDisplay.getLayout());
049                    searchContext.setLocale(themeDisplay.getLocale());
050                    searchContext.setTimeZone(themeDisplay.getTimeZone());
051                    searchContext.setUserId(themeDisplay.getUserId());
052    
053                    // Attributes
054    
055                    Map<String, Serializable> attributes = new HashMap<>();
056    
057                    Map<String, String[]> parameters = request.getParameterMap();
058    
059                    for (Map.Entry<String, String[]> entry : parameters.entrySet()) {
060                            String name = entry.getKey();
061                            String[] values = entry.getValue();
062    
063                            if (ArrayUtil.isNotEmpty(values)) {
064                                    if (values.length == 1) {
065                                            attributes.put(name, values[0]);
066                                    }
067                                    else {
068                                            attributes.put(name, values);
069                                    }
070                            }
071                    }
072    
073                    searchContext.setAttributes(attributes);
074    
075                    // Asset
076    
077                    long[] assetCategoryIds = StringUtil.split(
078                            ParamUtil.getString(request, "assetCategoryIds"), 0L);
079    
080                    String[] assetTagNames = StringUtil.split(
081                            ParamUtil.getString(request, "assetTagNames"));
082    
083                    searchContext.setAssetCategoryIds(assetCategoryIds);
084                    searchContext.setAssetTagNames(assetTagNames);
085    
086                    // Keywords
087    
088                    String keywords = ParamUtil.getString(request, "keywords");
089    
090                    searchContext.setKeywords(keywords);
091    
092                    // Query config
093    
094                    QueryConfig queryConfig = searchContext.getQueryConfig();
095    
096                    queryConfig.setLocale(themeDisplay.getLocale());
097    
098                    return searchContext;
099            }
100    
101            public static SearchContext getInstance(
102                    long[] assetCategoryIds, String[] assetTagNames,
103                    Map<String, Serializable> attributes, long companyId, String keywords,
104                    Layout layout, Locale locale, long scopeGroupId, TimeZone timeZone,
105                    long userId) {
106    
107                    SearchContext searchContext = new SearchContext();
108    
109                    // Theme display
110    
111                    searchContext.setCompanyId(companyId);
112                    searchContext.setGroupIds(new long[] {scopeGroupId});
113                    searchContext.setLayout(layout);
114                    searchContext.setLocale(locale);
115                    searchContext.setTimeZone(timeZone);
116                    searchContext.setUserId(userId);
117    
118                    // Attributes
119    
120                    if (attributes != null) {
121                            searchContext.setAttributes(attributes);
122                    }
123                    else {
124                            searchContext.setAttributes(new HashMap<String, Serializable>());
125                    }
126    
127                    // Asset
128    
129                    searchContext.setAssetCategoryIds(assetCategoryIds);
130                    searchContext.setAssetTagNames(assetTagNames);
131    
132                    // Keywords
133    
134                    searchContext.setKeywords(keywords);
135    
136                    // Query config
137    
138                    QueryConfig queryConfig = searchContext.getQueryConfig();
139    
140                    queryConfig.setLocale(locale);
141    
142                    return searchContext;
143            }
144    
145    }