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.settings;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.Locale;
025    
026    /**
027     * @author Iv??n Zaera
028     * @author Jorge Ferrer
029     */
030    public class TypedSettings {
031    
032            public TypedSettings(Settings settings) {
033                    this(
034                            settings, LocaleUtil.getSiteDefault(),
035                            LanguageUtil.getAvailableLocales());
036            }
037    
038            public TypedSettings(
039                    Settings settings, Locale defaultLocale, Locale... availableLocales) {
040    
041                    _settings = settings;
042                    _defaultLocale = defaultLocale;
043                    _availableLocales = availableLocales;
044            }
045    
046            public boolean getBooleanValue(String key) {
047                    return getBooleanValue(key, false);
048            }
049    
050            public boolean getBooleanValue(String key, boolean defaultValue) {
051                    String value = getValue(key, null);
052    
053                    return GetterUtil.getBoolean(value, defaultValue);
054            }
055    
056            public double getDoubleValue(String key) {
057                    return getDoubleValue(key, 0);
058            }
059    
060            public double getDoubleValue(String key, double defaultValue) {
061                    String value = getValue(key, null);
062    
063                    return GetterUtil.getDouble(value, defaultValue);
064            }
065    
066            public float getFloatValue(String key) {
067                    return getFloatValue(key, 0);
068            }
069    
070            public float getFloatValue(String key, float defaultValue) {
071                    String value = getValue(key, null);
072    
073                    return GetterUtil.getFloat(value, defaultValue);
074            }
075    
076            public int getIntegerValue(String key) {
077                    return getIntegerValue(key, 0);
078            }
079    
080            public int getIntegerValue(String key, int defaultValue) {
081                    String value = getValue(key, null);
082    
083                    return GetterUtil.getInteger(value, defaultValue);
084            }
085    
086            public LocalizedValuesMap getLocalizedValuesMap(String key) {
087                    LocalizedValuesMap localizedValuesMap = new LocalizedValuesMap(
088                            key, _defaultLocale, _availableLocales);
089    
090                    for (Locale locale : _availableLocales) {
091                            String localizedPreference = LocalizationUtil.getLocalizedName(
092                                    key, LocaleUtil.toLanguageId(locale));
093    
094                            localizedValuesMap.put(locale, getValue(localizedPreference, null));
095                    }
096    
097                    String defaultValue = localizedValuesMap.get(_defaultLocale);
098    
099                    if (Validator.isNotNull(defaultValue)) {
100                            return localizedValuesMap;
101                    }
102    
103                    localizedValuesMap.put(_defaultLocale, getValue(key, null));
104    
105                    return localizedValuesMap;
106            }
107    
108            public long getLongValue(String key) {
109                    return getLongValue(key, 0);
110            }
111    
112            public long getLongValue(String key, long defaultValue) {
113                    String value = getValue(key, null);
114    
115                    return GetterUtil.getLong(value, defaultValue);
116            }
117    
118            public String getValue(String key) {
119                    return getValue(key, StringPool.BLANK);
120            }
121    
122            public String getValue(String key, String defaultValue) {
123                    return _settings.getValue(key, defaultValue);
124            }
125    
126            public String[] getValues(String key) {
127                    return getValues(key, StringPool.EMPTY_ARRAY);
128            }
129    
130            public String[] getValues(String key, String[] defaultValue) {
131                    return _settings.getValues(key, defaultValue);
132            }
133    
134            public Settings getWrappedSettings() {
135                    return _settings;
136            }
137    
138            public void reset(String key) {
139                    ModifiableSettings modifiableSettings =
140                            _settings.getModifiableSettings();
141    
142                    modifiableSettings.reset(key);
143            }
144    
145            public void setBooleanValue(String key, boolean value) {
146                    setValue(key, String.valueOf(value));
147            }
148    
149            public void setIntegerValue(String key, int value) {
150                    setValue(key, String.valueOf(value));
151            }
152    
153            public void setLongValue(String key, long value) {
154                    setValue(key, String.valueOf(value));
155            }
156    
157            public void setValue(String key, String value) {
158                    ModifiableSettings modifiableSettings =
159                            _settings.getModifiableSettings();
160    
161                    modifiableSettings.setValue(key, value);
162            }
163    
164            public void setValues(String key, String[] values) {
165                    ModifiableSettings modifiableSettings =
166                            _settings.getModifiableSettings();
167    
168                    modifiableSettings.setValues(key, values);
169            }
170    
171            private final Locale[] _availableLocales;
172            private final Locale _defaultLocale;
173            private final Settings _settings;
174    
175    }