001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.api.impl;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
028import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
029import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration;
030
031/**
032 * @since 6.0
033 */
034public class LayoutTypeConfigurationImpl implements LayoutTypeConfiguration {
035
036    private static final long serialVersionUID = 1L;
037
038    protected String sinceVersion;
039
040    protected String deprecatedVersion;
041
042    protected String title;
043
044    protected String description;
045
046    protected String demoId;
047
048    protected boolean demoPreviewEnabled = false;
049
050    protected List<String> supportedModes;
051
052    protected boolean handlingLabels = false;
053
054    protected List<String> supportedControls;
055
056    protected boolean containingForm = false;
057
058    protected List<String> categories;
059
060    protected Map<String, List<LayoutDefinition>> propertyLayouts;
061
062    protected Map<String, Map<String, Serializable>> defaultPropertyValues;
063
064    protected Map<String, List<LayoutDefinition>> fieldLayouts;
065
066    public LayoutTypeConfigurationImpl() {
067        super();
068    }
069
070    @Override
071    public String getSinceVersion() {
072        return sinceVersion;
073    }
074
075    public void setSinceVersion(String sinceVersion) {
076        this.sinceVersion = sinceVersion;
077    }
078
079    @Override
080    public String getDeprecatedVersion() {
081        return deprecatedVersion;
082    }
083
084    public void setDeprecatedVersion(String deprecatedVersion) {
085        this.deprecatedVersion = deprecatedVersion;
086    }
087
088    @Override
089    public String getTitle() {
090        return title;
091    }
092
093    public void setTitle(String title) {
094        this.title = title;
095    }
096
097    @Override
098    public String getDescription() {
099        return description;
100    }
101
102    public void setDescription(String description) {
103        this.description = description;
104    }
105
106    @Override
107    public String getDemoId() {
108        return demoId;
109    }
110
111    public void setDemoId(String demoId) {
112        this.demoId = demoId;
113    }
114
115    @Override
116    public boolean isDemoPreviewEnabled() {
117        return demoPreviewEnabled;
118    }
119
120    public void setDemoPreviewEnabled(boolean demoPreviewEnabled) {
121        this.demoPreviewEnabled = demoPreviewEnabled;
122    }
123
124    @Override
125    public List<String> getSupportedModes() {
126        return supportedModes;
127    }
128
129    public void setSupportedModes(List<String> supportedModes) {
130        this.supportedModes = supportedModes;
131    }
132
133    @Override
134    public boolean isHandlingLabels() {
135        return handlingLabels;
136    }
137
138    public void setHandlingLabels(boolean handlingLabels) {
139        this.handlingLabels = handlingLabels;
140    }
141
142    @Override
143    public List<String> getSupportedControls() {
144        return supportedControls;
145    }
146
147    public void setSupportedControls(List<String> supportedControls) {
148        this.supportedControls = supportedControls;
149    }
150
151    @Override
152    public boolean isContainingForm() {
153        return containingForm;
154    }
155
156    public void setContainingForm(boolean containingForm) {
157        this.containingForm = containingForm;
158    }
159
160    @Override
161    public List<String> getCategories() {
162        return categories;
163    }
164
165    public void setCategories(List<String> categories) {
166        this.categories = categories;
167    }
168
169    @Override
170    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
171        return propertyLayouts;
172    }
173
174    @Override
175    public List<LayoutDefinition> getPropertyLayouts(String mode, String additionalMode) {
176        return getLayouts(propertyLayouts, mode, additionalMode);
177    }
178
179    public void setPropertyLayouts(Map<String, List<LayoutDefinition>> propertyLayouts) {
180        this.propertyLayouts = propertyLayouts;
181    }
182
183    @Override
184    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
185        return defaultPropertyValues;
186    }
187
188    @Override
189    public Map<String, Serializable> getDefaultPropertyValues(String mode) {
190        if (defaultPropertyValues != null) {
191            Map<String, Serializable> res = new HashMap<>();
192            Map<String, Serializable> anyProps = defaultPropertyValues.get(BuiltinModes.ANY);
193            if (anyProps != null) {
194                res.putAll(anyProps);
195            }
196            Map<String, Serializable> modeProps = defaultPropertyValues.get(mode);
197            if (modeProps != null) {
198                res.putAll(modeProps);
199            }
200            return res;
201        }
202        return null;
203    }
204
205    public void setDefaultPropertyValues(Map<String, Map<String, Serializable>> defaultPropertyValues) {
206        this.defaultPropertyValues = defaultPropertyValues;
207    }
208
209    protected List<LayoutDefinition> getLayouts(Map<String, List<LayoutDefinition>> allLayouts, String mode,
210            String additionalMode) {
211        if (allLayouts != null) {
212            List<LayoutDefinition> res = new ArrayList<>();
213            if (additionalMode != null) {
214                List<LayoutDefinition> defaultLayouts = allLayouts.get(additionalMode);
215                if (defaultLayouts != null) {
216                    res.addAll(defaultLayouts);
217                }
218            }
219            List<LayoutDefinition> modeLayouts = allLayouts.get(mode);
220            if (modeLayouts != null) {
221                res.addAll(modeLayouts);
222            }
223            return res;
224        }
225        return null;
226    }
227
228    /**
229     * @since 7.2
230     */
231    @Override
232    public boolean equals(Object obj) {
233        if (!(obj instanceof LayoutTypeConfigurationImpl)) {
234            return false;
235        }
236        if (obj == this) {
237            return true;
238        }
239        LayoutTypeConfigurationImpl lc = (LayoutTypeConfigurationImpl) obj;
240        return new EqualsBuilder().append(sinceVersion, lc.sinceVersion).append(deprecatedVersion, lc.deprecatedVersion).append(
241                title, lc.title).append(description, lc.description).append(demoId, lc.demoId).append(
242                demoPreviewEnabled, lc.demoPreviewEnabled).append(supportedModes, lc.supportedModes).append(
243                handlingLabels, lc.handlingLabels).append(supportedControls, lc.supportedControls).append(
244                containingForm, lc.containingForm).append(categories, lc.categories).append(propertyLayouts,
245                lc.propertyLayouts).append(defaultPropertyValues, lc.defaultPropertyValues).append(fieldLayouts,
246                lc.fieldLayouts).isEquals();
247    }
248
249}