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.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 getHighlightFragmentSize() {
044                    return GetterUtil.getInteger(
045                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE),
046                            _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE);
047            }
048    
049            public int getHighlightSnippetSize() {
050                    return GetterUtil.getInteger(
051                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE),
052                            _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE);
053            }
054    
055            public Locale getLocale() {
056                    Locale locale = (Locale)_attributes.get(LOCALE);
057    
058                    if (locale == null) {
059                            locale = LocaleUtil.getMostRelevantLocale();
060                    }
061    
062                    return locale;
063            }
064    
065            public float getQuerySuggestionScoringThreshold() {
066                    return GetterUtil.getFloat(
067                            _attributes.get(
068                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORING_THRESHOLD),
069                            _INDEX_SEARCH_QUERY_SUGGESTION_SCORING_THRESHOLD);
070            }
071    
072            public boolean isHighlightEnabled() {
073                    return GetterUtil.getBoolean(
074                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED), false);
075            }
076    
077            public boolean isHitsProcessingEnabled() {
078                    return GetterUtil.getBoolean(
079                            _attributes.get(HITS_PROCESSING_ENABLED), true);
080            }
081    
082            public boolean isQuerySuggestionEnabled() {
083                    return GetterUtil.getBoolean(
084                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
085                            _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED);
086            }
087    
088            public boolean isScoreEnabled() {
089                    return GetterUtil.getBoolean(
090                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
091                            _INDEX_SEARCH_SCORING_ENABLED);
092            }
093    
094            public Serializable removeAttribute(String name) {
095                    return _attributes.remove(name);
096            }
097    
098            public void setAttribute(String name, Serializable value) {
099                    _attributes.put(name, value);
100            }
101    
102            public void setHighlightEnabled(boolean highlightEnabled) {
103                    if (_INDEX_SEARCH_HIGHLIGHT_ENABLED) {
104                            _attributes.put(
105                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
106                    }
107                    else {
108                            _attributes.put(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, false);
109                    }
110            }
111    
112            public void setHighlightFragmentSize(int highlightFragmentSize) {
113                    _attributes.put(
114                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
115                            highlightFragmentSize);
116            }
117    
118            public void setHighlightSnippetSize(int highlightSnippetSize) {
119                    _attributes.put(
120                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
121                            highlightSnippetSize);
122            }
123    
124            public void setHitsProcessingEnabled(boolean hitsProcessingEnabled) {
125                    _attributes.put(HITS_PROCESSING_ENABLED, hitsProcessingEnabled);
126            }
127    
128            public void setLocale(Locale locale) {
129                    _attributes.put(LOCALE, locale);
130            }
131    
132            public void setScoreEnabled(boolean scoreEnabled) {
133                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
134            }
135    
136            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
137                    GetterUtil.getBoolean(
138                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
139    
140            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
141                    GetterUtil.getInteger(
142                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
143    
144            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
145                    GetterUtil.getInteger(
146                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
147    
148            private static final boolean _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED =
149                    GetterUtil.getBoolean(
150                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED));
151    
152            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
153                    GetterUtil.getBoolean(
154                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
155    
156            private static final String HITS_PROCESSING_ENABLED =
157                    "hitsProcessingEnabled";
158    
159            private static final float
160                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORING_THRESHOLD =
161                            GetterUtil.getFloat(
162                                    PropsUtil.get(
163                                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORING_THRESHOLD));
164    
165            private Map<String, Serializable> _attributes =
166                    new HashMap<String, Serializable>();
167    
168    }