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.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.util.Map;
021    
022    /**
023     * @author Iv??n Zaera
024     */
025    public class ParameterMapSettings extends BaseSettings {
026    
027            public static final String PREFERENCES_PREFIX = "preferences--";
028    
029            public static final String SETTINGS_PREFIX = "settings--";
030    
031            public ParameterMapSettings(
032                    Map<String, String[]> parameterMap, Settings parentSettings) {
033    
034                    super(parentSettings);
035    
036                    _parameterMap = parameterMap;
037            }
038    
039            public String getParameterNamePrefix() {
040                    return _parameterNamePrefix;
041            }
042    
043            public void setParameterNamePrefix(String parameterNamePrefix) {
044                    _parameterNamePrefix = parameterNamePrefix;
045            }
046    
047            @Override
048            protected String doGetValue(String key) {
049                    String[] values = doGetValues(key);
050    
051                    if (values == null) {
052                            return null;
053                    }
054    
055                    return values[0];
056            }
057    
058            @Override
059            protected String[] doGetValues(String key) {
060                    String[] values = null;
061    
062                    if (Validator.isNotNull(_parameterNamePrefix)) {
063                            values = _parameterMap.get(_parameterNamePrefix + key);
064                    }
065    
066                    if (values == null) {
067                            values = _parameterMap.get(key);
068                    }
069    
070                    if (values == null) {
071                            values = _parameterMap.get(
072                                    PREFERENCES_PREFIX + key + StringPool.DOUBLE_DASH);
073                    }
074    
075                    if (values == null) {
076                            values = _parameterMap.get(
077                                    SETTINGS_PREFIX + key + StringPool.DOUBLE_DASH);
078                    }
079    
080                    return values;
081            }
082    
083            private final Map<String, String[]> _parameterMap;
084            private String _parameterNamePrefix;
085    
086    }