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.portlet.dynamicdatamapping.model;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    
019    import java.io.Serializable;
020    
021    import java.util.LinkedHashMap;
022    import java.util.Locale;
023    import java.util.Map;
024    import java.util.Set;
025    
026    /**
027     * @author Pablo Carvalho
028     */
029    public class DDMFormFieldOptions implements Serializable {
030    
031            public DDMFormFieldOptions() {
032            }
033    
034            public DDMFormFieldOptions(DDMFormFieldOptions ddmFormFieldOptions) {
035                    _defaultLocale = ddmFormFieldOptions._defaultLocale;
036    
037                    Map<String, LocalizedValue> options = ddmFormFieldOptions._options;
038    
039                    for (String optionValue : options.keySet()) {
040                            LocalizedValue localizedValue = options.get(optionValue);
041    
042                            for (Locale locale : localizedValue.getAvailableLocales()) {
043                                    addOptionLabel(
044                                            optionValue, locale, localizedValue.getString(locale));
045                            }
046                    }
047            }
048    
049            public void addOption(String value) {
050                    _options.put(value, new LocalizedValue(_defaultLocale));
051            }
052    
053            public void addOptionLabel(
054                    String optionValue, Locale locale, String label) {
055    
056                    LocalizedValue labels = _options.get(optionValue);
057    
058                    if (labels == null) {
059                            labels = new LocalizedValue(_defaultLocale);
060    
061                            _options.put(optionValue, labels);
062                    }
063    
064                    labels.addString(locale, label);
065            }
066    
067            public Locale getDefaultLocale() {
068                    return _defaultLocale;
069            }
070    
071            public LocalizedValue getOptionLabels(String optionValue) {
072                    return _options.get(optionValue);
073            }
074    
075            public Set<String> getOptionsValues() {
076                    return _options.keySet();
077            }
078    
079            public void setDefaultLocale(Locale defaultLocale) {
080                    _defaultLocale = defaultLocale;
081    
082                    for (LocalizedValue localizedValue : _options.values()) {
083                            localizedValue.setDefaultLocale(defaultLocale);
084                    }
085            }
086    
087            private Locale _defaultLocale = LocaleUtil.getDefault();
088            private final Map<String, LocalizedValue> _options = new LinkedHashMap<>();
089    
090    }