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 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 boolean isHighlightEnabled() {
066                    return GetterUtil.getBoolean(
067                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED),
068                            _INDEX_SEARCH_HIGHLIGHT_ENABLED);
069            }
070    
071            public boolean isScoreEnabled() {
072                    return GetterUtil.getBoolean(
073                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
074                            _INDEX_SEARCH_SCORING_ENABLED);
075            }
076    
077            public Serializable removeAttribute(String name) {
078                    return _attributes.remove(name);
079            }
080    
081            public void setAttribute(String name, Serializable value) {
082                    _attributes.put(name, value);
083            }
084    
085            public void setHighlightEnabled(boolean highlightEnabled) {
086                    _attributes.put(
087                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
088            }
089    
090            public void setHighlightFragmentSize(int highlightFragmentSize) {
091                    _attributes.put(
092                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
093                            highlightFragmentSize);
094            }
095    
096            public void setHighlightSnippetSize(int highlightSnippetSize) {
097                    _attributes.put(
098                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
099                            highlightSnippetSize);
100            }
101    
102            public void setLocale(Locale locale) {
103                    _attributes.put(LOCALE, locale);
104            }
105    
106            public void setScoreEnabled(boolean scoreEnabled) {
107                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
108            }
109    
110            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
111                    GetterUtil.getBoolean(
112                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
113    
114            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
115                    GetterUtil.getInteger(
116                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
117    
118            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
119                    GetterUtil.getInteger(
120                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
121    
122            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
123                    GetterUtil.getBoolean(
124                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
125    
126            private Map<String, Serializable> _attributes =
127                    new HashMap<String, Serializable>();
128    
129    }