001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.api.impl;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
026import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
027import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration;
028
029/**
030 * @since 6.0
031 */
032public class LayoutTypeConfigurationImpl implements LayoutTypeConfiguration {
033
034    private static final long serialVersionUID = 1L;
035
036    protected String sinceVersion;
037
038    protected String deprecatedVersion;
039
040    protected String title;
041
042    protected String description;
043
044    protected String demoId;
045
046    protected boolean demoPreviewEnabled = false;
047
048    protected List<String> supportedModes;
049
050    protected boolean handlingLabels = false;
051
052    protected List<String> supportedControls;
053
054    protected boolean containingForm = false;
055
056    protected List<String> categories;
057
058    protected Map<String, List<LayoutDefinition>> propertyLayouts;
059
060    protected Map<String, Map<String, Serializable>> defaultPropertyValues;
061
062    protected Map<String, List<LayoutDefinition>> fieldLayouts;
063
064    public LayoutTypeConfigurationImpl() {
065        super();
066    }
067
068    public String getSinceVersion() {
069        return sinceVersion;
070    }
071
072    public void setSinceVersion(String sinceVersion) {
073        this.sinceVersion = sinceVersion;
074    }
075
076    public String getDeprecatedVersion() {
077        return deprecatedVersion;
078    }
079
080    public void setDeprecatedVersion(String deprecatedVersion) {
081        this.deprecatedVersion = deprecatedVersion;
082    }
083
084    public String getTitle() {
085        return title;
086    }
087
088    public void setTitle(String title) {
089        this.title = title;
090    }
091
092    public String getDescription() {
093        return description;
094    }
095
096    public void setDescription(String description) {
097        this.description = description;
098    }
099
100    public String getDemoId() {
101        return demoId;
102    }
103
104    public void setDemoId(String demoId) {
105        this.demoId = demoId;
106    }
107
108    public boolean isDemoPreviewEnabled() {
109        return demoPreviewEnabled;
110    }
111
112    public void setDemoPreviewEnabled(boolean demoPreviewEnabled) {
113        this.demoPreviewEnabled = demoPreviewEnabled;
114    }
115
116    public List<String> getSupportedModes() {
117        return supportedModes;
118    }
119
120    public void setSupportedModes(List<String> supportedModes) {
121        this.supportedModes = supportedModes;
122    }
123
124    public boolean isHandlingLabels() {
125        return handlingLabels;
126    }
127
128    public void setHandlingLabels(boolean handlingLabels) {
129        this.handlingLabels = handlingLabels;
130    }
131
132    public List<String> getSupportedControls() {
133        return supportedControls;
134    }
135
136    public void setSupportedControls(List<String> supportedControls) {
137        this.supportedControls = supportedControls;
138    }
139
140    public boolean isContainingForm() {
141        return containingForm;
142    }
143
144    public void setContainingForm(boolean containingForm) {
145        this.containingForm = containingForm;
146    }
147
148    public List<String> getCategories() {
149        return categories;
150    }
151
152    public void setCategories(List<String> categories) {
153        this.categories = categories;
154    }
155
156    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
157        return propertyLayouts;
158    }
159
160    public List<LayoutDefinition> getPropertyLayouts(String mode, String additionalMode) {
161        return getLayouts(propertyLayouts, mode, additionalMode);
162    }
163
164    public void setPropertyLayouts(Map<String, List<LayoutDefinition>> propertyLayouts) {
165        this.propertyLayouts = propertyLayouts;
166    }
167
168    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
169        return defaultPropertyValues;
170    }
171
172    public Map<String, Serializable> getDefaultPropertyValues(String mode) {
173        if (defaultPropertyValues != null) {
174            Map<String, Serializable> res = new HashMap<String, Serializable>();
175            Map<String, Serializable> anyProps = defaultPropertyValues.get(BuiltinModes.ANY);
176            if (anyProps != null) {
177                res.putAll(anyProps);
178            }
179            Map<String, Serializable> modeProps = defaultPropertyValues.get(mode);
180            if (modeProps != null) {
181                res.putAll(modeProps);
182            }
183            return res;
184        }
185        return null;
186    }
187
188    public void setDefaultPropertyValues(Map<String, Map<String, Serializable>> defaultPropertyValues) {
189        this.defaultPropertyValues = defaultPropertyValues;
190    }
191
192    protected List<LayoutDefinition> getLayouts(Map<String, List<LayoutDefinition>> allLayouts, String mode,
193            String additionalMode) {
194        if (allLayouts != null) {
195            List<LayoutDefinition> res = new ArrayList<LayoutDefinition>();
196            if (additionalMode != null) {
197                List<LayoutDefinition> defaultLayouts = allLayouts.get(additionalMode);
198                if (defaultLayouts != null) {
199                    res.addAll(defaultLayouts);
200                }
201            }
202            List<LayoutDefinition> modeLayouts = allLayouts.get(mode);
203            if (modeLayouts != null) {
204                res.addAll(modeLayouts);
205            }
206            return res;
207        }
208        return null;
209    }
210
211    /**
212     * @since 7.2
213     */
214    @Override
215    public boolean equals(Object obj) {
216        if (!(obj instanceof LayoutTypeConfigurationImpl)) {
217            return false;
218        }
219        if (obj == this) {
220            return true;
221        }
222        LayoutTypeConfigurationImpl lc = (LayoutTypeConfigurationImpl) obj;
223        return new EqualsBuilder().append(sinceVersion, lc.sinceVersion).append(deprecatedVersion, lc.deprecatedVersion).append(
224                title, lc.title).append(description, lc.description).append(demoId, lc.demoId).append(
225                demoPreviewEnabled, lc.demoPreviewEnabled).append(supportedModes, lc.supportedModes).append(
226                handlingLabels, lc.handlingLabels).append(supportedControls, lc.supportedControls).append(
227                containingForm, lc.containingForm).append(categories, lc.categories).append(propertyLayouts,
228                lc.propertyLayouts).append(defaultPropertyValues, lc.defaultPropertyValues).append(fieldLayouts,
229                lc.fieldLayouts).isEquals();
230    }
231
232}