001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    
022    import java.io.Serializable;
023    
024    import java.util.HashMap;
025    import java.util.Locale;
026    import java.util.Map;
027    
028    /**
029     * @author Michael C. Han
030     */
031    public class QueryConfig implements Serializable {
032    
033            public static final String LOCALE = "locale";
034    
035            public static final String SEARCH_SUBFOLDERS = "search.subfolders";
036    
037            public Serializable getAttribute(String name) {
038                    return _attributes.get(name);
039            }
040    
041            public Map<String, Serializable> getAttributes() {
042                    return _attributes;
043            }
044    
045            public int getCollatedSpellCheckResultScoresThreshold() {
046                    return GetterUtil.getInteger(
047                            _attributes.get(
048                                    PropsKeys.
049                                            INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
050                                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD);
051            }
052    
053            public int getHighlightFragmentSize() {
054                    return GetterUtil.getInteger(
055                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE),
056                            _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE);
057            }
058    
059            public int getHighlightSnippetSize() {
060                    return GetterUtil.getInteger(
061                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE),
062                            _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE);
063            }
064    
065            public Locale getLocale() {
066                    Locale locale = (Locale)_attributes.get(LOCALE);
067    
068                    if (locale == null) {
069                            locale = LocaleUtil.getMostRelevantLocale();
070                    }
071    
072                    return locale;
073            }
074    
075            public int getQueryIndexingThreshold() {
076                    return GetterUtil.getInteger(
077                            _attributes.get(
078                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
079                                    _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD);
080            }
081    
082            public int getQuerySuggestionMax() {
083                    return GetterUtil.getInteger(
084                            _attributes.get(
085                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX),
086                                    _INDEX_SEARCH_QUERY_SUGGESTION_MAX);
087            }
088    
089            public int getQuerySuggestionScoresThreshold() {
090                    return GetterUtil.getInteger(
091                            _attributes.get(
092                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
093                                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD);
094            }
095    
096            public boolean isCollatedSpellCheckResultEnabled() {
097                    return GetterUtil.getBoolean(
098                            _attributes.get(
099                                    PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
100                                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED);
101            }
102    
103            public boolean isHighlightEnabled() {
104                    return GetterUtil.getBoolean(
105                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED),
106                            _INDEX_SEARCH_HIGHLIGHT_ENABLED);
107            }
108    
109            public boolean isHitsProcessingEnabled() {
110                    return GetterUtil.getBoolean(
111                            _attributes.get(_HITS_PROCESSING_ENABLED), true);
112            }
113    
114            public boolean isQueryIndexingEnabled() {
115                    return GetterUtil.getBoolean(
116                            _attributes.get(
117                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED),
118                                    _INDEX_SEARCH_QUERY_INDEXING_ENABLED);
119            }
120    
121            public boolean isQuerySuggestionEnabled() {
122                    return GetterUtil.getBoolean(
123                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
124                            _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED);
125            }
126    
127            public boolean isScoreEnabled() {
128                    return GetterUtil.getBoolean(
129                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
130                            _INDEX_SEARCH_SCORING_ENABLED);
131            }
132    
133            public boolean isSearchSubfolders() {
134                    return GetterUtil.getBoolean(_attributes.get(SEARCH_SUBFOLDERS));
135            }
136    
137            public Serializable removeAttribute(String name) {
138                    return _attributes.remove(name);
139            }
140    
141            public void setAttribute(String name, Serializable value) {
142                    _attributes.put(name, value);
143            }
144    
145            public void setCollatedSpellCheckResultEnabled(
146                    boolean collatedSpellCheckResultEnabled) {
147    
148                    _attributes.put(
149                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED,
150                            collatedSpellCheckResultEnabled);
151            }
152    
153            public void setCollatedSpellCheckResultScoresThreshold(
154                    int collatedSpellCheckResultScoresThreshold) {
155    
156                    _attributes.put(
157                            PropsKeys.
158                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD,
159                            collatedSpellCheckResultScoresThreshold);
160            }
161    
162            public void setHighlightEnabled(boolean highlightEnabled) {
163                    if (_INDEX_SEARCH_HIGHLIGHT_ENABLED) {
164                            _attributes.put(
165                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
166                    }
167                    else {
168                            _attributes.put(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, false);
169                    }
170            }
171    
172            public void setHighlightFragmentSize(int highlightFragmentSize) {
173                    _attributes.put(
174                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
175                            highlightFragmentSize);
176            }
177    
178            public void setHighlightSnippetSize(int highlightSnippetSize) {
179                    _attributes.put(
180                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
181                            highlightSnippetSize);
182            }
183    
184            public void setHitsProcessingEnabled(boolean hitsProcessingEnabled) {
185                    _attributes.put(_HITS_PROCESSING_ENABLED, hitsProcessingEnabled);
186            }
187    
188            public void setLocale(Locale locale) {
189                    _attributes.put(LOCALE, locale);
190            }
191    
192            public void setQueryIndexingEnabled(boolean enabled) {
193                    _attributes.put(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED, enabled);
194            }
195    
196            public void setQueryIndexingThreshold(int queryIndexingThreshold) {
197                    _attributes.put(
198                            PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD,
199                            queryIndexingThreshold);
200            }
201    
202            public void setQuerySuggestionEnabled(boolean querySuggestionEnabled) {
203                    _attributes.put(
204                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED,
205                            querySuggestionEnabled);
206            }
207    
208            public void setQuerySuggestionScoresThreshold(
209                    int querySuggestionScoresThreshold) {
210                            _attributes.put(
211                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD,
212                                    querySuggestionScoresThreshold);
213            }
214    
215            public void setQuerySuggestionsMax(int querySuggestionMax) {
216                    _attributes.put(
217                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX, querySuggestionMax);
218            }
219    
220            public void setScoreEnabled(boolean scoreEnabled) {
221                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
222            }
223    
224            public void setSearchSubfolders(boolean searchSubfolders) {
225                    _attributes.put(SEARCH_SUBFOLDERS, searchSubfolders);
226            }
227    
228            private static final String _HITS_PROCESSING_ENABLED =
229                    "hitsProcessingEnabled";
230    
231            private static final boolean
232                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED =
233                            GetterUtil.getBoolean(
234                                    PropsUtil.get(
235                                    PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
236                                    true);
237    
238            private static final int
239                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD =
240                            GetterUtil.getInteger(
241                                    PropsUtil.get(
242                                            PropsKeys.
243                                            INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
244                                            50);
245    
246            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
247                    GetterUtil.getBoolean(
248                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
249    
250            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
251                    GetterUtil.getInteger(
252                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
253    
254            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
255                    GetterUtil.getInteger(
256                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
257    
258            private static final boolean _INDEX_SEARCH_QUERY_INDEXING_ENABLED =
259                    GetterUtil.getBoolean(
260                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED), true);
261    
262            private static final int _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD =
263                    GetterUtil.getInteger(
264                            PropsUtil.get(
265                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
266                            50);
267    
268            private static final boolean _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED =
269                    GetterUtil.getBoolean(
270                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
271                            true);
272    
273            private static final int _INDEX_SEARCH_QUERY_SUGGESTION_MAX =
274                    GetterUtil.getInteger(
275                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX), 5);
276    
277            private static final int
278                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD = GetterUtil.getInteger(
279                            PropsUtil.get(
280                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD), 50);
281    
282            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
283                    GetterUtil.getBoolean(
284                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
285    
286            private Map<String, Serializable> _attributes =
287                    new HashMap<String, Serializable>();
288    
289    }