001    /**
002     * Copyright (c) 2000-2013 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.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 Serializable getAttribute(String name) {
036                    return _attributes.get(name);
037            }
038    
039            public Map<String, Serializable> getAttributes() {
040                    return _attributes;
041            }
042    
043            public int getCollatedSpellCheckResultScoresThreshold() {
044                    return GetterUtil.getInteger(
045                            _attributes.get(
046                                    PropsKeys.
047                                            INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
048                                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD);
049            }
050    
051            public int getHighlightFragmentSize() {
052                    return GetterUtil.getInteger(
053                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE),
054                            _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE);
055            }
056    
057            public int getHighlightSnippetSize() {
058                    return GetterUtil.getInteger(
059                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE),
060                            _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE);
061            }
062    
063            public Locale getLocale() {
064                    Locale locale = (Locale)_attributes.get(LOCALE);
065    
066                    if (locale == null) {
067                            locale = LocaleUtil.getMostRelevantLocale();
068                    }
069    
070                    return locale;
071            }
072    
073            public int getQueryIndexingThreshold() {
074                    return GetterUtil.getInteger(
075                            _attributes.get(
076                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
077                                    _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD);
078            }
079    
080            public int getQuerySuggestionMax() {
081                    return GetterUtil.getInteger(
082                            _attributes.get(
083                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX),
084                                    _INDEX_SEARCH_QUERY_SUGGESTION_MAX);
085            }
086    
087            public int getQuerySuggestionScoresThreshold() {
088                    return GetterUtil.getInteger(
089                            _attributes.get(
090                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
091                                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD);
092            }
093    
094            public boolean isCollatedSpellCheckResultEnabled() {
095                    return GetterUtil.getBoolean(
096                            _attributes.get(
097                                    PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
098                                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED);
099            }
100    
101            public boolean isHighlightEnabled() {
102                    return GetterUtil.getBoolean(
103                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED), false);
104            }
105    
106            public boolean isHitsProcessingEnabled() {
107                    return GetterUtil.getBoolean(
108                            _attributes.get(_HITS_PROCESSING_ENABLED), true);
109            }
110    
111            public boolean isQueryIndexingEnabled() {
112                    return GetterUtil.getBoolean(
113                            _attributes.get(
114                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED),
115                                    _INDEX_SEARCH_QUERY_INDEXING_ENABLED);
116            }
117    
118            public boolean isQuerySuggestionEnabled() {
119                    return GetterUtil.getBoolean(
120                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
121                            _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED);
122            }
123    
124            public boolean isScoreEnabled() {
125                    return GetterUtil.getBoolean(
126                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
127                            _INDEX_SEARCH_SCORING_ENABLED);
128            }
129    
130            public Serializable removeAttribute(String name) {
131                    return _attributes.remove(name);
132            }
133    
134            public void setAttribute(String name, Serializable value) {
135                    _attributes.put(name, value);
136            }
137    
138            public void setCollatedSpellCheckResultEnabled(
139                    boolean collatedSpellCheckResultEnabled) {
140    
141                    _attributes.put(
142                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED,
143                            collatedSpellCheckResultEnabled);
144            }
145    
146            public void setCollatedSpellCheckResultScoresThreshold(
147                    int collatedSpellCheckResultScoresThreshold) {
148    
149                    _attributes.put(
150                            PropsKeys.
151                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD,
152                            collatedSpellCheckResultScoresThreshold);
153            }
154    
155            public void setHighlightEnabled(boolean highlightEnabled) {
156                    if (_INDEX_SEARCH_HIGHLIGHT_ENABLED) {
157                            _attributes.put(
158                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
159                    }
160                    else {
161                            _attributes.put(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, false);
162                    }
163            }
164    
165            public void setHighlightFragmentSize(int highlightFragmentSize) {
166                    _attributes.put(
167                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
168                            highlightFragmentSize);
169            }
170    
171            public void setHighlightSnippetSize(int highlightSnippetSize) {
172                    _attributes.put(
173                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
174                            highlightSnippetSize);
175            }
176    
177            public void setHitsProcessingEnabled(boolean hitsProcessingEnabled) {
178                    _attributes.put(_HITS_PROCESSING_ENABLED, hitsProcessingEnabled);
179            }
180    
181            public void setLocale(Locale locale) {
182                    _attributes.put(LOCALE, locale);
183            }
184    
185            public void setQueryIndexingEnabled(boolean enabled) {
186                    _attributes.put(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED, enabled);
187            }
188    
189            public void setQueryIndexingThreshold(int queryIndexingThreshold) {
190                    _attributes.put(
191                            PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD,
192                            queryIndexingThreshold);
193            }
194    
195            public void setQuerySuggestionEnabled(boolean querySuggestionEnabled) {
196                    _attributes.put(
197                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED,
198                            querySuggestionEnabled);
199            }
200    
201            public void setQuerySuggestionScoresThreshold(
202                    int querySuggestionScoresThreshold) {
203                            _attributes.put(
204                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD,
205                                    querySuggestionScoresThreshold);
206            }
207    
208            public void setQuerySuggestionsMax(int querySuggestionMax) {
209                    _attributes.put(
210                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX, querySuggestionMax);
211            }
212    
213            public void setScoreEnabled(boolean scoreEnabled) {
214                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
215            }
216    
217            private static final String _HITS_PROCESSING_ENABLED =
218                    "hitsProcessingEnabled";
219    
220            private static final boolean
221                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED =
222                            GetterUtil.getBoolean(
223                                    PropsUtil.get(
224                                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
225                                    true);
226    
227            private static final int
228                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD =
229                            GetterUtil.getInteger(
230                                    PropsUtil.get(
231                                            PropsKeys.
232                                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
233                                            50);
234    
235            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
236                    GetterUtil.getBoolean(
237                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
238    
239            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
240                    GetterUtil.getInteger(
241                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
242    
243            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
244                    GetterUtil.getInteger(
245                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
246    
247            private static final boolean _INDEX_SEARCH_QUERY_INDEXING_ENABLED =
248                    GetterUtil.getBoolean(
249                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED), true);
250    
251            private static final int _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD =
252                    GetterUtil.getInteger(
253                            PropsUtil.get(
254                                    PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
255                            50);
256    
257            private static final boolean _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED =
258                    GetterUtil.getBoolean(
259                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
260                            true);
261    
262            private static final int _INDEX_SEARCH_QUERY_SUGGESTION_MAX =
263                    GetterUtil.getInteger(
264                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX), 5);
265    
266            private static final int
267                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD = GetterUtil.getInteger(
268                            PropsUtil.get(
269                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD), 50);
270    
271            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
272                    GetterUtil.getBoolean(
273                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
274    
275            private Map<String, Serializable> _attributes =
276                    new HashMap<String, Serializable>();
277    
278    }