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.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    import com.liferay.portal.model.Layout;
023    
024    import java.io.Serializable;
025    
026    import java.util.Collections;
027    import java.util.HashMap;
028    import java.util.HashSet;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    import java.util.Set;
033    import java.util.TimeZone;
034    import java.util.concurrent.ConcurrentHashMap;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Julio Camarero
039     */
040    public class SearchContext implements Serializable {
041    
042            public void addFacet(Facet facet) {
043                    if (facet == null) {
044                            return;
045                    }
046    
047                    _facets.put(facet.getFieldName(), facet);
048            }
049    
050            public void addFullQueryEntryClassName(String entryClassName) {
051                    if (_fullQueryEntryClassNames == null) {
052                            _fullQueryEntryClassNames = new HashSet<>();
053                    }
054    
055                    _fullQueryEntryClassNames.add(entryClassName);
056            }
057    
058            public void addStats(Stats stats) {
059                    _stats.put(stats.getField(), stats);
060            }
061    
062            public void clearFullQueryEntryClassNames() {
063                    _fullQueryEntryClassNames = null;
064            }
065    
066            public long[] getAssetCategoryIds() {
067                    return _assetCategoryIds;
068            }
069    
070            public String[] getAssetTagNames() {
071                    return _assetTagNames;
072            }
073    
074            public Serializable getAttribute(String name) {
075                    if (_attributes == null) {
076                            return null;
077                    }
078    
079                    return _attributes.get(name);
080            }
081    
082            public Map<String, Serializable> getAttributes() {
083                    if (_attributes == null) {
084                            _attributes = new HashMap<>();
085                    }
086    
087                    return _attributes;
088            }
089    
090            public BooleanClause<Query>[] getBooleanClauses() {
091                    return _booleanClauses;
092            }
093    
094            public long[] getCategoryIds() {
095                    return _categoryIds;
096            }
097    
098            public long[] getClassTypeIds() {
099                    return _classTypeIds;
100            }
101    
102            public long getCompanyId() {
103                    return _companyId;
104            }
105    
106            public int getEnd() {
107                    return _end;
108            }
109    
110            public String[] getEntryClassNames() {
111                    if (_entryClassNames == null) {
112                            _entryClassNames = new String[0];
113                    }
114    
115                    return _entryClassNames;
116            }
117    
118            public Facet getFacet(String fieldName) {
119                    return _facets.get(fieldName);
120            }
121    
122            public Map<String, Facet> getFacets() {
123                    return _facets;
124            }
125    
126            public long[] getFolderIds() {
127                    return _folderIds;
128            }
129    
130            public String[] getFullQueryEntryClassNames() {
131                    if (_fullQueryEntryClassNames == null) {
132                            return new String[0];
133                    }
134    
135                    return _fullQueryEntryClassNames.toArray(
136                            new String[_fullQueryEntryClassNames.size()]);
137            }
138    
139            public GroupBy getGroupBy() {
140                    return _groupBy;
141            }
142    
143            public long[] getGroupIds() {
144                    return _groupIds;
145            }
146    
147            public String getKeywords() {
148                    return _keywords;
149            }
150    
151            public String getLanguageId() {
152                    return LocaleUtil.toLanguageId(_locale);
153            }
154    
155            public Layout getLayout() {
156                    return _layout;
157            }
158    
159            public Locale getLocale() {
160                    return _locale;
161            }
162    
163            public long[] getNodeIds() {
164                    return _nodeIds;
165            }
166    
167            public long getOwnerUserId() {
168                    return _ownerUserId;
169            }
170    
171            public String[] getPortletIds() {
172                    return _portletIds;
173            }
174    
175            public QueryConfig getQueryConfig() {
176                    if (_queryConfig == null) {
177                            _queryConfig = new QueryConfig();
178                    }
179    
180                    return _queryConfig;
181            }
182    
183            public float getScoresThreshold() {
184                    return _scoresThreshold;
185            }
186    
187            public String getSearchEngineId() {
188                    if (Validator.isNull(_searchEngineId)) {
189                            return SearchEngineHelperUtil.getDefaultSearchEngineId();
190                    }
191    
192                    return _searchEngineId;
193            }
194    
195            public Sort[] getSorts() {
196                    return _sorts;
197            }
198    
199            public int getStart() {
200                    return _start;
201            }
202    
203            public Map<String, Stats> getStats() {
204                    return Collections.unmodifiableMap(_stats);
205            }
206    
207            public Stats getStats(String fieldName) {
208                    return _stats.get(fieldName);
209            }
210    
211            public TimeZone getTimeZone() {
212                    return _timeZone;
213            }
214    
215            public long getUserId() {
216                    return _userId;
217            }
218    
219            public boolean hasOverridenKeywords() {
220                    return Validator.isNull(_originalKeywords);
221            }
222    
223            public boolean isAndSearch() {
224                    return _andSearch;
225            }
226    
227            public boolean isCommitImmediately() {
228                    return _commitImmediately;
229            }
230    
231            public boolean isIncludeAttachments() {
232                    return _includeAttachments;
233            }
234    
235            public boolean isIncludeDiscussions() {
236                    return _includeDiscussions;
237            }
238    
239            public boolean isIncludeFolders() {
240                    return _includeFolders;
241            }
242    
243            public boolean isIncludeLiveGroups() {
244                    return _includeLiveGroups;
245            }
246    
247            public boolean isIncludeStagingGroups() {
248                    return _includeStagingGroups;
249            }
250    
251            public boolean isLike() {
252                    return _like;
253            }
254    
255            public boolean isScopeStrict() {
256                    return _scopeStrict;
257            }
258    
259            public void overrideKeywords(String keywords) {
260                    _originalKeywords = _keywords;
261    
262                    _keywords = keywords;
263            }
264    
265            public void setAndSearch(boolean andSearch) {
266                    _andSearch = andSearch;
267            }
268    
269            public void setAssetCategoryIds(long[] assetCategoryIds) {
270                    _assetCategoryIds = assetCategoryIds;
271            }
272    
273            public void setAssetTagNames(String[] assetTagNames) {
274                    _assetTagNames = assetTagNames;
275            }
276    
277            public void setAttribute(String name, Serializable value) {
278                    if (_attributes == null) {
279                            _attributes = new HashMap<>();
280                    }
281    
282                    _attributes.put(name, value);
283            }
284    
285            public void setAttributes(Map<String, Serializable> attributes) {
286                    _attributes = attributes;
287            }
288    
289            public void setBooleanClauses(BooleanClause<Query>[] booleanClauses) {
290                    _booleanClauses = booleanClauses;
291            }
292    
293            public void setCategoryIds(long[] categoryIds) {
294                    _categoryIds = categoryIds;
295            }
296    
297            public void setClassTypeIds(long[] classTypeIds) {
298                    _classTypeIds = classTypeIds;
299            }
300    
301            public void setCommitImmediately(boolean commitImmediately) {
302                    _commitImmediately = commitImmediately;
303            }
304    
305            public void setCompanyId(long companyId) {
306                    _companyId = companyId;
307            }
308    
309            public void setEnd(int end) {
310                    _end = end;
311            }
312    
313            public void setEntryClassNames(String[] entryClassNames) {
314                    _entryClassNames = entryClassNames;
315            }
316    
317            public void setFacets(List<Facet> facets) {
318                    for (Facet facet : facets) {
319                            _facets.put(facet.getFieldName(), facet);
320                    }
321            }
322    
323            public void setFolderIds(List<Long> folderIds) {
324                    _folderIds = ArrayUtil.toArray(
325                            folderIds.toArray(new Long[folderIds.size()]));
326            }
327    
328            public void setFolderIds(long[] folderIds) {
329                    _folderIds = folderIds;
330            }
331    
332            public void setGroupBy(GroupBy groupBy) {
333                    _groupBy = groupBy;
334            }
335    
336            public void setGroupIds(long[] groupIds) {
337                    _groupIds = groupIds;
338            }
339    
340            public void setIncludeAttachments(boolean includeAttachments) {
341                    _includeAttachments = includeAttachments;
342            }
343    
344            public void setIncludeDiscussions(boolean includeDiscussions) {
345                    _includeDiscussions = includeDiscussions;
346            }
347    
348            public void setIncludeFolders(boolean includeFolders) {
349                    _includeFolders = includeFolders;
350            }
351    
352            public void setIncludeLiveGroups(boolean includeLiveGroups) {
353                    _includeLiveGroups = includeLiveGroups;
354            }
355    
356            public void setIncludeStagingGroups(boolean includeStagingGroups) {
357                    _includeStagingGroups = includeStagingGroups;
358            }
359    
360            public void setKeywords(String keywords) {
361                    _keywords = keywords;
362            }
363    
364            public void setLayout(Layout layout) {
365                    _layout = layout;
366            }
367    
368            public void setLike(boolean like) {
369                    _like = like;
370            }
371    
372            public void setLocale(Locale locale) {
373                    if (locale != null) {
374                            _locale = locale;
375                    }
376            }
377    
378            public void setNodeIds(long[] nodeIds) {
379                    _nodeIds = nodeIds;
380            }
381    
382            public void setOwnerUserId(long ownerUserId) {
383                    _ownerUserId = ownerUserId;
384            }
385    
386            public void setPortletIds(String[] portletIds) {
387                    _portletIds = portletIds;
388            }
389    
390            public void setQueryConfig(QueryConfig queryConfig) {
391                    _queryConfig = queryConfig;
392            }
393    
394            public void setScopeStrict(boolean scopeStrict) {
395                    _scopeStrict = scopeStrict;
396            }
397    
398            public void setScoresThreshold(float scoresThreshold) {
399                    _scoresThreshold = scoresThreshold;
400            }
401    
402            public void setSearchEngineId(String searchEngineId) {
403                    if (_searchEngineId == null) {
404                            _searchEngineId = searchEngineId;
405                    }
406            }
407    
408            public void setSorts(Sort... sorts) {
409                    _sorts = sorts;
410            }
411    
412            public void setStart(int start) {
413                    _start = start;
414            }
415    
416            public void setTimeZone(TimeZone timeZone) {
417                    _timeZone = timeZone;
418            }
419    
420            public void setUserId(long userId) {
421                    _userId = userId;
422            }
423    
424            private boolean _andSearch;
425            private long[] _assetCategoryIds;
426            private String[] _assetTagNames;
427            private Map<String, Serializable> _attributes;
428            private BooleanClause<Query>[] _booleanClauses;
429            private long[] _categoryIds;
430            private long[] _classTypeIds;
431            private boolean _commitImmediately;
432            private long _companyId;
433            private int _end = QueryUtil.ALL_POS;
434            private String[] _entryClassNames;
435            private final Map<String, Facet> _facets = new ConcurrentHashMap<>();
436            private long[] _folderIds;
437            private Set<String> _fullQueryEntryClassNames;
438            private GroupBy _groupBy;
439            private long[] _groupIds;
440            private boolean _includeAttachments;
441            private boolean _includeDiscussions;
442            private boolean _includeFolders = true;
443            private boolean _includeLiveGroups = true;
444            private boolean _includeStagingGroups = true;
445            private String _keywords;
446            private Layout _layout;
447            private boolean _like;
448            private Locale _locale = LocaleUtil.getMostRelevantLocale();
449            private long[] _nodeIds;
450            private String _originalKeywords;
451            private long _ownerUserId;
452            private String[] _portletIds;
453            private QueryConfig _queryConfig;
454            private boolean _scopeStrict = true;
455            private float _scoresThreshold;
456            private String _searchEngineId;
457            private Sort[] _sorts;
458            private int _start = QueryUtil.ALL_POS;
459            private final Map<String, Stats> _stats = new ConcurrentHashMap<>();
460            private TimeZone _timeZone;
461            private long _userId;
462    
463    }