001
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
036 public class SearchContextFactory {
037
038 public static SearchContext getInstance(HttpServletRequest request) {
039 SearchContext searchContext = new SearchContext();
040
041
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
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
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
087
088 String keywords = ParamUtil.getString(request, "keywords");
089
090 searchContext.setKeywords(keywords);
091
092
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
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
119
120 if (attributes != null) {
121 searchContext.setAttributes(attributes);
122 }
123 else {
124 searchContext.setAttributes(new HashMap<String, Serializable>());
125 }
126
127
128
129 searchContext.setAssetCategoryIds(assetCategoryIds);
130 searchContext.setAssetTagNames(assetTagNames);
131
132
133
134 searchContext.setKeywords(keywords);
135
136
137
138 QueryConfig queryConfig = searchContext.getQueryConfig();
139
140 queryConfig.setLocale(locale);
141
142 return searchContext;
143 }
144
145 }