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