001/*
002 * (C) Copyright 2010 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.descriptors;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.apache.xml.serialize.OutputFormat;
031import org.nuxeo.common.xmap.DOMSerializer;
032import org.nuxeo.common.xmap.annotation.XContent;
033import org.nuxeo.common.xmap.annotation.XNode;
034import org.nuxeo.common.xmap.annotation.XNodeList;
035import org.nuxeo.common.xmap.annotation.XNodeMap;
036import org.nuxeo.common.xmap.annotation.XObject;
037import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
038import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
039import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeConfiguration;
040import org.nuxeo.ecm.platform.forms.layout.api.impl.WidgetTypeConfigurationImpl;
041import org.w3c.dom.DocumentFragment;
042
043/**
044 * Descriptor for a widget type configuration
045 *
046 * @author Anahide Tchertchian
047 * @since 5.4
048 */
049@XObject("configuration")
050@SuppressWarnings("deprecation")
051public class WidgetTypeConfigurationDescriptor {
052
053    private static final Log log = LogFactory.getLog(WidgetTypeConfigurationDescriptor.class);
054
055    @XNode("sinceVersion")
056    String sinceVersion;
057
058    /**
059     * @since 5.6
060     */
061    @XNode("deprecatedVersion")
062    String deprecatedVersion;
063
064    @XNode("title")
065    String title;
066
067    // retrieve HTML tags => introspect DOM on setter
068    String description;
069
070    @XNode("demo@id")
071    String demoId;
072
073    @XNode("demo@previewEnabled")
074    boolean demoPreviewEnabled = false;
075
076    @XNodeList(value = "supportedModes/mode", type = ArrayList.class, componentType = String.class)
077    List<String> supportedModes;
078
079    @XNode("acceptingSubWidgets")
080    boolean acceptingSubWidgets = false;
081
082    /**
083     * @since 5.6
084     */
085    @XNode("handlingLabels")
086    boolean handlingLabels = false;
087
088    /**
089     * List of supported controls (controls checked on subwidgets configuration).
090     *
091     * @since 5.9.1
092     */
093    @XNodeList(value = "supportedControls/control", type = ArrayList.class, componentType = String.class)
094    List<String> supportedControls;
095
096    @XNode("fields/list")
097    boolean list = false;
098
099    @XNode("fields/complex")
100    boolean complex = false;
101
102    @XNode("containingForm")
103    boolean containingForm = false;
104
105    @XNodeList(value = "fields/supportedTypes/type", type = ArrayList.class, componentType = String.class)
106    List<String> supportedFieldTypes;
107
108    @XNodeList(value = "fields/defaultTypes/type", type = ArrayList.class, componentType = String.class)
109    List<String> defaultFieldTypes;
110
111    @XNodeList(value = "fields/defaultConfiguration/field", type = ArrayList.class, componentType = FieldDescriptor.class)
112    List<FieldDescriptor> defaultFieldDefinitions;
113
114    /**
115     * Layouts for accepted field mappings.
116     *
117     * @since 5.7.3
118     */
119    @XNodeMap(value = "fields/layouts", key = "@mode", type = HashMap.class, componentType = LayoutDescriptors.class)
120    Map<String, LayoutDescriptors> fieldLayouts;
121
122    @XNodeList(value = "categories/category", type = ArrayList.class, componentType = String.class)
123    List<String> categories;
124
125    @XNodeMap(value = "properties/layouts", key = "@mode", type = HashMap.class, componentType = LayoutDescriptors.class)
126    Map<String, LayoutDescriptors> propertyLayouts;
127
128    /**
129     * @since 5.7.3
130     */
131    @XNodeMap(value = "properties/defaultValues", key = "@mode", type = HashMap.class, componentType = PropertiesDescriptor.class)
132    Map<String, PropertiesDescriptor> defaultPropertyValues;
133
134    /**
135     * @since 5.7.3
136     */
137    @XNodeMap(value = "controls/defaultValues", key = "@mode", type = HashMap.class, componentType = ControlsDescriptor.class)
138    Map<String, ControlsDescriptor> defaultControlValues;
139
140    Map<String, Serializable> properties;
141
142    public List<String> getCategories() {
143        return categories;
144    }
145
146    public String getDescription() {
147        return description;
148    }
149
150    @XContent("description")
151    public void setDescription(DocumentFragment descriptionDOM) {
152        try {
153            OutputFormat of = new OutputFormat();
154            of.setOmitXMLDeclaration(true);
155            this.description = DOMSerializer.toString(descriptionDOM, of).trim();
156        } catch (IOException e) {
157            log.error(e, e);
158        }
159    }
160
161    public String getTitle() {
162        return title;
163    }
164
165    public String getDemoId() {
166        return demoId;
167    }
168
169    public boolean isDemoPreviewEnabled() {
170        return demoPreviewEnabled;
171    }
172
173    public boolean isAcceptingSubWidgets() {
174        return acceptingSubWidgets;
175    }
176
177    /**
178     * @since 5.6
179     */
180    public boolean isHandlingLabels() {
181        return handlingLabels;
182    }
183
184    /**
185     * @since 5.9.1
186     */
187    public List<String> getSupportedControls() {
188        return supportedControls;
189    }
190
191    public boolean isComplex() {
192        return complex;
193    }
194
195    public boolean isList() {
196        return list;
197    }
198
199    public boolean isContainingForm() {
200        return containingForm;
201    }
202
203    public List<String> getDefaultFieldTypes() {
204        return defaultFieldTypes;
205    }
206
207    public List<String> getSupportedFieldTypes() {
208        return supportedFieldTypes;
209    }
210
211    public List<FieldDefinition> getDefaultFieldDefinitions() {
212        if (defaultFieldDefinitions == null) {
213            return null;
214        }
215        List<FieldDefinition> res = new ArrayList<>();
216        for (int i = 0; i < defaultFieldDefinitions.size(); i++) {
217            res.add(defaultFieldDefinitions.get(i).getFieldDefinition());
218        }
219        return res;
220    }
221
222    public Map<String, Serializable> getConfProperties() {
223        return properties;
224    }
225
226    public Serializable getConfProperty(String propName) {
227        if (properties == null) {
228            return null;
229        }
230        return properties.get(propName);
231    }
232
233    @XNode("confProperties")
234    public void setConfProperties(PropertiesDescriptor propsDesc) {
235        properties = propsDesc.getProperties();
236    }
237
238    protected List<LayoutDefinition> getLayouts(Map<String, LayoutDescriptors> descs, String mode, String additionalMode) {
239        if (descs != null) {
240            List<LayoutDefinition> res = new ArrayList<>();
241            if (additionalMode != null) {
242                LayoutDescriptors defaultLayouts = descs.get(additionalMode);
243                if (defaultLayouts != null) {
244                    List<LayoutDefinition> defaultLayoutsList = defaultLayouts.getLayouts();
245                    if (defaultLayoutsList != null) {
246                        res.addAll(defaultLayoutsList);
247                    }
248                }
249            }
250            LayoutDescriptors modeLayouts = descs.get(mode);
251            if (modeLayouts != null) {
252                List<LayoutDefinition> modeLayoutsList = modeLayouts.getLayouts();
253                if (modeLayoutsList != null) {
254                    res.addAll(modeLayoutsList);
255                }
256            }
257            return res;
258        }
259        return null;
260    }
261
262    protected Map<String, List<LayoutDefinition>> getLayouts(Map<String, LayoutDescriptors> descs) {
263        if (descs != null) {
264            Map<String, List<LayoutDefinition>> res = new HashMap<>();
265            for (Map.Entry<String, LayoutDescriptors> entry : descs.entrySet()) {
266                res.put(entry.getKey(), entry.getValue().getLayouts());
267            }
268            return res;
269        }
270        return null;
271    }
272
273    public List<LayoutDefinition> getPropertyLayouts(String mode, String additionalMode) {
274        return getLayouts(propertyLayouts, mode, additionalMode);
275    }
276
277    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
278        return getLayouts(propertyLayouts);
279    }
280
281    public List<LayoutDefinition> getFieldLayouts(String mode, String additionalMode) {
282        return getLayouts(fieldLayouts, mode, additionalMode);
283    }
284
285    public Map<String, List<LayoutDefinition>> getFieldLayouts() {
286        return getLayouts(fieldLayouts);
287    }
288
289    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
290        if (defaultPropertyValues != null) {
291            Map<String, Map<String, Serializable>> res = new HashMap<>();
292            for (Map.Entry<String, PropertiesDescriptor> entry : defaultPropertyValues.entrySet()) {
293                res.put(entry.getKey(), entry.getValue().getProperties());
294            }
295            return res;
296        }
297        return null;
298    }
299
300    public Map<String, Map<String, Serializable>> getDefaultControlValues() {
301        if (defaultControlValues != null) {
302            Map<String, Map<String, Serializable>> res = new HashMap<>();
303            for (Map.Entry<String, ControlsDescriptor> entry : defaultControlValues.entrySet()) {
304                res.put(entry.getKey(), entry.getValue().getControls());
305            }
306            return res;
307        }
308        return null;
309    }
310
311    public String getSinceVersion() {
312        return sinceVersion;
313    }
314
315    /**
316     * @since 5.6
317     */
318    public String getDeprecatedVersion() {
319        return deprecatedVersion;
320    }
321
322    public List<String> getSupportedModes() {
323        return supportedModes;
324    }
325
326    public WidgetTypeConfiguration getWidgetTypeConfiguration() {
327        WidgetTypeConfigurationImpl res = new WidgetTypeConfigurationImpl();
328        res.setSinceVersion(getSinceVersion());
329        res.setDeprecatedVersion(getDeprecatedVersion());
330        res.setTitle(getTitle());
331        res.setDescription(getDescription());
332        res.setDemoId(getDemoId());
333        res.setDemoPreviewEnabled(isDemoPreviewEnabled());
334        res.setProperties(getConfProperties());
335        res.setSupportedModes(getSupportedModes());
336        res.setAcceptingSubWidgets(isAcceptingSubWidgets());
337        res.setHandlingLabels(isHandlingLabels());
338        res.setList(isList());
339        res.setComplex(isComplex());
340        res.setContainingForm(isContainingForm());
341        res.setSupportedFieldTypes(getSupportedFieldTypes());
342        res.setDefaultFieldTypes(getDefaultFieldTypes());
343        res.setDefaultFieldDefinitions(getDefaultFieldDefinitions());
344        res.setCategories(getCategories());
345        res.setPropertyLayouts(getPropertyLayouts());
346        res.setDefaultPropertyValues(getDefaultPropertyValues());
347        res.setDefaultControlValues(getDefaultControlValues());
348        res.setFieldLayouts(getFieldLayouts());
349        res.setSupportedControls(getSupportedControls());
350        return res;
351    }
352}