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 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 DDMForm() {
033            }
034    
035            public DDMForm(DDMForm ddmForm) {
036                    _availableLocales = new LinkedHashSet<>(ddmForm._availableLocales);
037                    _defaultLocale = ddmForm._defaultLocale;
038    
039                    for (DDMFormField ddmFormField : ddmForm._ddmFormFields) {
040                            addDDMFormField(new DDMFormField(ddmFormField));
041                    }
042            }
043    
044            public void addAvailableLocale(Locale locale) {
045                    _availableLocales.add(locale);
046            }
047    
048            public void addDDMFormField(DDMFormField ddmFormField) {
049                    ddmFormField.setDDMForm(this);
050    
051                    _ddmFormFields.add(ddmFormField);
052            }
053    
054            public Set<Locale> getAvailableLocales() {
055                    return _availableLocales;
056            }
057    
058            public List<DDMFormField> getDDMFormFields() {
059                    return _ddmFormFields;
060            }
061    
062            public Map<String, DDMFormField> getDDMFormFieldsMap(
063                    boolean includeNestedDDMFormFields) {
064    
065                    Map<String, DDMFormField> ddmFormFieldsMap = new LinkedHashMap<>();
066    
067                    for (DDMFormField ddmFormField : _ddmFormFields) {
068                            ddmFormFieldsMap.put(ddmFormField.getName(), ddmFormField);
069    
070                            if (includeNestedDDMFormFields) {
071                                    ddmFormFieldsMap.putAll(
072                                            ddmFormField.getNestedDDMFormFieldsMap());
073                            }
074                    }
075    
076                    return ddmFormFieldsMap;
077            }
078    
079            public Locale getDefaultLocale() {
080                    return _defaultLocale;
081            }
082    
083            public void setAvailableLocales(Set<Locale> availableLocales) {
084                    _availableLocales = availableLocales;
085            }
086    
087            public void setDDMFormFields(List<DDMFormField> ddmFormFields) {
088                    for (DDMFormField ddmFormField : ddmFormFields) {
089                            ddmFormField.setDDMForm(this);
090                    }
091    
092                    _ddmFormFields = ddmFormFields;
093            }
094    
095            public void setDefaultLocale(Locale defaultLocale) {
096                    _defaultLocale = defaultLocale;
097            }
098    
099            private Set<Locale> _availableLocales = new LinkedHashSet<>();
100            private List<DDMFormField> _ddmFormFields = new ArrayList<>();
101            private Locale _defaultLocale;
102    
103    }