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;
016    
017    import java.io.Serializable;
018    
019    import java.util.ArrayList;
020    import java.util.LinkedHashMap;
021    import java.util.LinkedHashSet;
022    import java.util.List;
023    import java.util.Locale;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Pablo Carvalho
029     */
030    public class DDMForm implements Serializable {
031    
032            public void addAvailableLocale(Locale locale) {
033                    _availableLocales.add(locale);
034            }
035    
036            public void addDDMFormField(DDMFormField ddmFormField) {
037                    ddmFormField.setDDMForm(this);
038    
039                    _ddmFormFields.add(ddmFormField);
040            }
041    
042            public Set<Locale> getAvailableLocales() {
043                    return _availableLocales;
044            }
045    
046            public List<DDMFormField> getDDMFormFields() {
047                    return _ddmFormFields;
048            }
049    
050            public Map<String, DDMFormField> getDDMFormFieldsMap(
051                    boolean includeNestedDDMFormFields) {
052    
053                    Map<String, DDMFormField> ddmFormFieldsMap = new LinkedHashMap<>();
054    
055                    for (DDMFormField ddmFormField : _ddmFormFields) {
056                            ddmFormFieldsMap.put(ddmFormField.getName(), ddmFormField);
057    
058                            if (includeNestedDDMFormFields) {
059                                    ddmFormFieldsMap.putAll(
060                                            ddmFormField.getNestedDDMFormFieldsMap());
061                            }
062                    }
063    
064                    return ddmFormFieldsMap;
065            }
066    
067            public Locale getDefaultLocale() {
068                    return _defaultLocale;
069            }
070    
071            public void setAvailableLocales(Set<Locale> availableLocales) {
072                    _availableLocales = availableLocales;
073            }
074    
075            public void setDDMFormFields(List<DDMFormField> ddmFormFields) {
076                    for (DDMFormField ddmFormField : ddmFormFields) {
077                            ddmFormField.setDDMForm(this);
078                    }
079    
080                    _ddmFormFields = ddmFormFields;
081            }
082    
083            public void setDefaultLocale(Locale defaultLocale) {
084                    _defaultLocale = defaultLocale;
085            }
086    
087            private Set<Locale> _availableLocales = new LinkedHashSet<>();
088            private List<DDMFormField> _ddmFormFields = new ArrayList<>();
089            private Locale _defaultLocale;
090    
091    }