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