001/*
002 * (C) Copyright 2009-2016 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.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.platform.forms.layout.api.BuiltinModes;
029import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
030import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
031import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeConfiguration;
032
033public class WidgetTypeConfigurationImpl implements WidgetTypeConfiguration {
034
035    private static final long serialVersionUID = 1L;
036
037    protected String sinceVersion;
038
039    protected String deprecatedVersion;
040
041    protected String title;
042
043    protected String description;
044
045    protected String demoId;
046
047    protected boolean demoPreviewEnabled = false;
048
049    protected Map<String, Serializable> properties;
050
051    protected List<String> supportedModes;
052
053    protected boolean acceptingSubWidgets = false;
054
055    protected boolean handlingLabels = false;
056
057    /**
058     * @since 5.9.1
059     */
060    protected List<String> supportedControls;
061
062    protected boolean list = false;
063
064    protected boolean complex = false;
065
066    protected boolean containingForm = false;
067
068    protected List<String> supportedFieldTypes;
069
070    protected List<String> defaultFieldTypes;
071
072    protected List<FieldDefinition> defaultFieldDefinitions;
073
074    protected List<String> categories;
075
076    protected Map<String, List<LayoutDefinition>> propertyLayouts;
077
078    protected Map<String, Map<String, Serializable>> defaultPropertyValues;
079
080    protected Map<String, Map<String, Serializable>> defaultControlValues;
081
082    protected Map<String, List<LayoutDefinition>> fieldLayouts;
083
084    public WidgetTypeConfigurationImpl() {
085        super();
086        this.properties = Collections.emptyMap();
087        this.supportedModes = Collections.emptyList();
088        this.supportedFieldTypes = Collections.emptyList();
089        this.defaultFieldTypes = Collections.emptyList();
090        this.defaultFieldDefinitions = Collections.emptyList();
091        this.categories = Collections.emptyList();
092        this.propertyLayouts = Collections.emptyMap();
093    }
094
095    @Override
096    public String getSinceVersion() {
097        return sinceVersion;
098    }
099
100    @Override
101    public String getDeprecatedVersion() {
102        return deprecatedVersion;
103    }
104
105    public String getTitle() {
106        return title;
107    }
108
109    public String getDescription() {
110        return description;
111    }
112
113    @Override
114    public String getDemoId() {
115        return demoId;
116    }
117
118    @Override
119    public boolean isDemoPreviewEnabled() {
120        return demoPreviewEnabled;
121    }
122
123    @Override
124    public Map<String, Serializable> getConfProperties() {
125        return properties;
126    }
127
128    @Override
129    public Serializable getConfProperty(String propName) {
130        if (properties == null) {
131            return null;
132        }
133        return properties.get(propName);
134    }
135
136    @Override
137    public List<String> getSupportedModes() {
138        return supportedModes;
139    }
140
141    @Override
142    public boolean isAcceptingSubWidgets() {
143        return acceptingSubWidgets;
144    }
145
146    public boolean isList() {
147        return list;
148    }
149
150    public boolean isComplex() {
151        return complex;
152    }
153
154    public boolean isContainingForm() {
155        return containingForm;
156    }
157
158    public List<String> getSupportedFieldTypes() {
159        return supportedFieldTypes;
160    }
161
162    public List<String> getDefaultFieldTypes() {
163        return defaultFieldTypes;
164    }
165
166    public List<FieldDefinition> getDefaultFieldDefinitions() {
167        return defaultFieldDefinitions;
168    }
169
170    public List<String> getCategories() {
171        return categories;
172    }
173
174    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
175        return propertyLayouts;
176    }
177
178    public List<LayoutDefinition> getLayouts(Map<String, List<LayoutDefinition>> allLayouts, String mode,
179            String additionalMode) {
180        if (allLayouts != null) {
181            List<LayoutDefinition> res = new ArrayList<>();
182            if (additionalMode != null) {
183                List<LayoutDefinition> defaultLayouts = allLayouts.get(additionalMode);
184                if (defaultLayouts != null) {
185                    res.addAll(defaultLayouts);
186                }
187            }
188            List<LayoutDefinition> modeLayouts = allLayouts.get(mode);
189            if (modeLayouts != null) {
190                res.addAll(modeLayouts);
191            }
192            return res;
193        }
194        return null;
195    }
196
197    public List<LayoutDefinition> getPropertyLayouts(String mode, String additionalMode) {
198        return getLayouts(propertyLayouts, mode, additionalMode);
199    }
200
201    /**
202     * @since 5.6
203     */
204    public void setSinceVersion(String sinceVersion) {
205        this.sinceVersion = sinceVersion;
206    }
207
208    /**
209     * @since 5.6
210     */
211    public void setDeprecatedVersion(String deprecatedVersion) {
212        this.deprecatedVersion = deprecatedVersion;
213    }
214
215    /**
216     * @since 5.6
217     */
218    public void setTitle(String title) {
219        this.title = title;
220    }
221
222    /**
223     * @since 5.6
224     */
225    public void setDescription(String description) {
226        this.description = description;
227    }
228
229    /**
230     * @since 5.6
231     */
232    public void setDemoId(String demoId) {
233        this.demoId = demoId;
234    }
235
236    /**
237     * @since 5.6
238     */
239    public void setDemoPreviewEnabled(boolean demoPreviewEnabled) {
240        this.demoPreviewEnabled = demoPreviewEnabled;
241    }
242
243    /**
244     * @since 5.6
245     */
246    public void setProperties(Map<String, Serializable> properties) {
247        this.properties = properties;
248    }
249
250    /**
251     * @since 5.6
252     */
253    public void setSupportedModes(List<String> supportedModes) {
254        this.supportedModes = supportedModes;
255    }
256
257    /**
258     * @since 5.6
259     */
260    public void setAcceptingSubWidgets(boolean acceptingSubWidgets) {
261        this.acceptingSubWidgets = acceptingSubWidgets;
262    }
263
264    /**
265     * @since 5.6
266     */
267    public void setList(boolean list) {
268        this.list = list;
269    }
270
271    /**
272     * @since 5.6
273     */
274    public void setComplex(boolean complex) {
275        this.complex = complex;
276    }
277
278    /**
279     * @since 5.6
280     */
281    public void setContainingForm(boolean containingForm) {
282        this.containingForm = containingForm;
283    }
284
285    /**
286     * @since 5.6
287     */
288    public void setSupportedFieldTypes(List<String> supportedFieldTypes) {
289        this.supportedFieldTypes = supportedFieldTypes;
290    }
291
292    /**
293     * @since 5.6
294     */
295    public void setDefaultFieldTypes(List<String> defaultFieldTypes) {
296        this.defaultFieldTypes = defaultFieldTypes;
297    }
298
299    /**
300     * @since 5.6
301     */
302    public void setDefaultFieldDefinitions(List<FieldDefinition> defaultFieldDefinitions) {
303        this.defaultFieldDefinitions = defaultFieldDefinitions;
304    }
305
306    /**
307     * @since 5.6
308     */
309    public void setCategories(List<String> categories) {
310        this.categories = categories;
311    }
312
313    /**
314     * @since 5.6
315     */
316    public void setPropertyLayouts(Map<String, List<LayoutDefinition>> propertyLayouts) {
317        this.propertyLayouts = propertyLayouts;
318    }
319
320    /**
321     * @since 5.6
322     */
323    public boolean isHandlingLabels() {
324        return handlingLabels;
325    }
326
327    /**
328     * @since 5.6
329     */
330    public void setHandlingLabels(boolean handlingLabels) {
331        this.handlingLabels = handlingLabels;
332    }
333
334    /**
335     * @since 5.7.3
336     */
337    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
338        return defaultPropertyValues;
339    }
340
341    /**
342     * @since 5.7.3
343     */
344    public Map<String, Serializable> getDefaultPropertyValues(String mode) {
345        if (defaultPropertyValues != null) {
346            Map<String, Serializable> res = new HashMap<>();
347            Map<String, Serializable> anyProps = defaultPropertyValues.get(BuiltinModes.ANY);
348            if (anyProps != null) {
349                res.putAll(anyProps);
350            }
351            Map<String, Serializable> modeProps = defaultPropertyValues.get(mode);
352            if (modeProps != null) {
353                res.putAll(modeProps);
354            }
355            return res;
356        }
357        return null;
358    }
359
360    /**
361     * @since 5.7.3
362     */
363    public void setDefaultPropertyValues(Map<String, Map<String, Serializable>> values) {
364        this.defaultPropertyValues = values;
365    }
366
367    /**
368     * @since 6.0
369     */
370    public Map<String, Map<String, Serializable>> getDefaultControlValues() {
371        return defaultControlValues;
372    }
373
374    /**
375     * @since 6.0
376     */
377    public Map<String, Serializable> getDefaultControlValues(String mode) {
378        if (defaultControlValues != null) {
379            Map<String, Serializable> res = new HashMap<>();
380            Map<String, Serializable> anyProps = defaultControlValues.get(BuiltinModes.ANY);
381            if (anyProps != null) {
382                res.putAll(anyProps);
383            }
384            Map<String, Serializable> modeProps = defaultControlValues.get(mode);
385            if (modeProps != null) {
386                res.putAll(modeProps);
387            }
388            return res;
389        }
390        return null;
391    }
392
393    /**
394     * @since 6.0
395     */
396    public void setDefaultControlValues(Map<String, Map<String, Serializable>> values) {
397        this.defaultControlValues = values;
398    }
399
400    @Override
401    public Map<String, List<LayoutDefinition>> getFieldLayouts() {
402        return fieldLayouts;
403    }
404
405    @Override
406    public List<LayoutDefinition> getFieldLayouts(String mode, String additionalMode) {
407        return getLayouts(fieldLayouts, mode, additionalMode);
408    }
409
410    /**
411     * @since 5.7.3
412     */
413    public void setFieldLayouts(Map<String, List<LayoutDefinition>> fieldLayouts) {
414        this.fieldLayouts = fieldLayouts;
415    }
416
417    /**
418     * @since 5.9.1
419     */
420    @Override
421    public List<String> getSupportedControls() {
422        return supportedControls;
423    }
424
425    /**
426     * @since 5.9.1
427     */
428    public void setSupportedControls(List<String> supportedControls) {
429        this.supportedControls = supportedControls;
430    }
431
432    /**
433     * @since 7.2
434     */
435    @Override
436    public boolean equals(Object obj) {
437        if (!(obj instanceof WidgetTypeConfigurationImpl)) {
438            return false;
439        }
440        if (obj == this) {
441            return true;
442        }
443        WidgetTypeConfigurationImpl wc = (WidgetTypeConfigurationImpl) obj;
444        return new EqualsBuilder().append(sinceVersion, wc.sinceVersion)
445                                  .append(deprecatedVersion, wc.deprecatedVersion)
446                                  .append(title, wc.title)
447                                  .append(description, wc.description)
448                                  .append(demoId, wc.demoId)
449                                  .append(demoPreviewEnabled, wc.demoPreviewEnabled)
450                                  .append(properties, wc.properties)
451                                  .append(supportedModes, wc.supportedModes)
452                                  .append(acceptingSubWidgets, wc.acceptingSubWidgets)
453                                  .append(handlingLabels, wc.handlingLabels)
454                                  .append(supportedControls, wc.supportedControls)
455                                  .append(list, wc.list)
456                                  .append(complex, wc.complex)
457                                  .append(containingForm, wc.containingForm)
458                                  .append(supportedFieldTypes, wc.supportedFieldTypes)
459                                  .append(defaultFieldTypes, wc.defaultFieldTypes)
460                                  .append(defaultFieldDefinitions, wc.defaultFieldDefinitions)
461                                  .append(categories, wc.categories)
462                                  .append(propertyLayouts, wc.propertyLayouts)
463                                  .append(defaultPropertyValues, wc.defaultPropertyValues)
464                                  .append(defaultControlValues, wc.defaultControlValues)
465                                  .append(fieldLayouts, wc.fieldLayouts)
466                                  .isEquals();
467    }
468
469}