001    /**
002     * Copyright (c) 2000-2012 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.search.facet.Facet;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.Serializable;
024    
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Locale;
028    import java.util.Map;
029    import java.util.TimeZone;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Julio Camarero
035     */
036    public class SearchContext implements Serializable {
037    
038            public void addFacet(Facet facet) {
039                    if (facet == null) {
040                            return;
041                    }
042    
043                    _facets.put(facet.getFieldName(), facet);
044            }
045    
046            public long[] getAssetCategoryIds() {
047                    return _assetCategoryIds;
048            }
049    
050            public String[] getAssetTagNames() {
051                    return _assetTagNames;
052            }
053    
054            public Serializable getAttribute(String name) {
055                    if (_attributes == null) {
056                            return null;
057                    }
058    
059                    return _attributes.get(name);
060            }
061    
062            public Map<String, Serializable> getAttributes() {
063                    if (_attributes == null) {
064                            _attributes = new HashMap<String, Serializable>();
065                    }
066    
067                    return _attributes;
068            }
069    
070            public BooleanClause[] getBooleanClauses() {
071                    return _booleanClauses;
072            }
073    
074            public long[] getCategoryIds() {
075                    return _categoryIds;
076            }
077    
078            public long getCompanyId() {
079                    return _companyId;
080            }
081    
082            public int getEnd() {
083                    return _end;
084            }
085    
086            public String[] getEntryClassNames() {
087                    if (_entryClassNames == null) {
088                            _entryClassNames = new String[0];
089                    }
090    
091                    return _entryClassNames;
092            }
093    
094            public Facet getFacet(String fieldName) {
095                    return _facets.get(fieldName);
096            }
097    
098            public Map<String, Facet> getFacets() {
099                    return _facets;
100            }
101    
102            public long[] getFolderIds() {
103                    return _folderIds;
104            }
105    
106            public long[] getGroupIds() {
107                    return _groupIds;
108            }
109    
110            public String getKeywords() {
111                    return _keywords;
112            }
113    
114            public Locale getLocale() {
115                    return _locale;
116            }
117    
118            public long[] getNodeIds() {
119                    return _nodeIds;
120            }
121    
122            public long getOwnerUserId() {
123                    return _ownerUserId;
124            }
125    
126            public String[] getPortletIds() {
127                    return _portletIds;
128            }
129    
130            public QueryConfig getQueryConfig() {
131                    if (_queryConfig == null) {
132                            _queryConfig = new QueryConfig();
133                    }
134    
135                    return _queryConfig;
136            }
137    
138            public String getSearchEngineId() {
139                    if (Validator.isNull(_searchEngineId)) {
140                            return SearchEngineUtil.getDefaultSearchEngineId();
141                    }
142    
143                    return _searchEngineId;
144            }
145    
146            public Sort[] getSorts() {
147                    return _sorts;
148            }
149    
150            public int getStart() {
151                    return _start;
152            }
153    
154            public TimeZone getTimeZone() {
155                    return _timeZone;
156            }
157    
158            public long getUserId() {
159                    return _userId;
160            }
161    
162            public boolean hasOverridenKeywords() {
163                    return Validator.isNull(_originalKeywords);
164            }
165    
166            public boolean isAndSearch() {
167                    return _andSearch;
168            }
169    
170            public boolean isIncludeAttachments() {
171                    return _includeAttachments;
172            }
173    
174            public boolean isIncludeDiscussions() {
175                    return _includeDiscussions;
176            }
177    
178            public boolean isIncludeLiveGroups() {
179                    return _includeLiveGroups;
180            }
181    
182            public boolean isIncludeStagingGroups() {
183                    return _includeStagingGroups;
184            }
185    
186            public boolean isScopeStrict() {
187                    return _scopeStrict;
188            }
189    
190            public void overrideKeywords(String keywords) {
191                    _originalKeywords = _keywords;
192    
193                    _keywords = keywords;
194            }
195    
196            public void setAndSearch(boolean andSearch) {
197                    _andSearch = andSearch;
198            }
199    
200            public void setAssetCategoryIds(long[] assetCategoryIds) {
201                    _assetCategoryIds = assetCategoryIds;
202            }
203    
204            public void setAssetTagNames(String[] assetTagNames) {
205                    _assetTagNames = assetTagNames;
206            }
207    
208            public void setAttribute(String name, Serializable value) {
209                    if (_attributes == null) {
210                            _attributes = new HashMap<String, Serializable>();
211                    }
212    
213                    _attributes.put(name, value);
214            }
215    
216            public void setAttributes(Map<String, Serializable> attributes) {
217                    _attributes = attributes;
218            }
219    
220            public void setBooleanClauses(BooleanClause[] booleanClauses) {
221                    _booleanClauses = booleanClauses;
222            }
223    
224            public void setCategoryIds(long[] categoryIds) {
225                    _categoryIds = categoryIds;
226            }
227    
228            public void setCompanyId(long companyId) {
229                    _companyId = companyId;
230            }
231    
232            public void setEnd(int end) {
233                    _end = end;
234            }
235    
236            public void setEntryClassNames(String[] entryClassNames) {
237                    _entryClassNames = entryClassNames;
238            }
239    
240            public void setFacets(List<Facet> facets) {
241                    for (Facet facet : facets) {
242                            _facets.put(facet.getFieldName(), facet);
243                    }
244            }
245    
246            public void setFolderIds(List<Long> folderIds) {
247                    _folderIds = ArrayUtil.toArray(
248                            folderIds.toArray(new Long[folderIds.size()]));
249            }
250    
251            public void setFolderIds(long[] folderIds) {
252                    _folderIds = folderIds;
253            }
254    
255            public void setGroupIds(long[] groupIds) {
256                    _groupIds = groupIds;
257            }
258    
259            public void setIncludeAttachments(boolean includeAttachments) {
260                    _includeAttachments = includeAttachments;
261            }
262    
263            public void setIncludeDiscussions(boolean includeDiscussions) {
264                    _includeDiscussions = includeDiscussions;
265            }
266    
267            public void setIncludeLiveGroups(boolean includeLiveGroups) {
268                    _includeLiveGroups = includeLiveGroups;
269            }
270    
271            public void setIncludeStagingGroups(boolean includeStagingGroups) {
272                    _includeStagingGroups = includeStagingGroups;
273            }
274    
275            public void setKeywords(String keywords) {
276                    _keywords = keywords;
277            }
278    
279            public void setLocale(Locale locale) {
280                    if (locale != null) {
281                            _locale = locale;
282                    }
283            }
284    
285            public void setNodeIds(long[] nodeIds) {
286                    _nodeIds = nodeIds;
287            }
288    
289            public void setOwnerUserId(long ownerUserId) {
290                    _ownerUserId = ownerUserId;
291            }
292    
293            public void setPortletIds(String[] portletIds) {
294                    _portletIds = portletIds;
295            }
296    
297            public void setQueryConfig(QueryConfig queryConfig) {
298                    _queryConfig = queryConfig;
299            }
300    
301            public void setScopeStrict(boolean scopeStrict) {
302                    _scopeStrict = scopeStrict;
303            }
304    
305            public void setSearchEngineId(String searchEngineId) {
306                    if (_searchEngineId == null) {
307                            _searchEngineId = searchEngineId;
308                    }
309            }
310    
311            public void setSorts(Sort[] sorts) {
312                    _sorts = sorts;
313            }
314    
315            public void setStart(int start) {
316                    _start = start;
317            }
318    
319            public void setTimeZone(TimeZone timeZone) {
320                    _timeZone = timeZone;
321            }
322    
323            public void setUserId(long userId) {
324                    _userId = userId;
325            }
326    
327            private boolean _andSearch;
328            private long[] _assetCategoryIds;
329            private String[] _assetTagNames;
330            private Map<String, Serializable> _attributes;
331            private BooleanClause[] _booleanClauses;
332            private long[] _categoryIds;
333            private long _companyId;
334            private int _end = QueryUtil.ALL_POS;
335            private String[] _entryClassNames;
336            private Map<String, Facet> _facets = new ConcurrentHashMap<String, Facet>();
337            private long[] _folderIds;
338            private long[] _groupIds;
339            private boolean _includeAttachments;
340            private boolean _includeDiscussions;
341            private boolean _includeLiveGroups = true;
342            private boolean _includeStagingGroups = true;
343            private String _keywords;
344            private Locale _locale = LocaleUtil.getMostRelevantLocale();
345            private long[] _nodeIds;
346            private String _originalKeywords;
347            private long _ownerUserId;
348            private String[] _portletIds;
349            private QueryConfig _queryConfig;
350            private boolean _scopeStrict = true;
351            private String _searchEngineId;
352            private Sort[] _sorts;
353            private int _start = QueryUtil.ALL_POS;
354            private TimeZone _timeZone;
355            private long _userId;
356    
357    }