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.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.LayoutDefinition;
038import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration;
039import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutTypeConfigurationImpl;
040import org.w3c.dom.DocumentFragment;
041
042/**
043 * @since 6.0
044 */
045@XObject("configuration")
046public class LayoutTypeConfigurationDescriptor {
047
048    private static final Log log = LogFactory.getLog(LayoutTypeConfigurationDescriptor.class);
049
050    @XNode("sinceVersion")
051    String sinceVersion;
052
053    /**
054     * @since 5.6
055     */
056    @XNode("deprecatedVersion")
057    String deprecatedVersion;
058
059    @XNode("title")
060    String title;
061
062    // retrieve HTML tags => introspect DOM on setter
063    String description;
064
065    @XNode("demo@id")
066    String demoId;
067
068    @XNode("demo@previewEnabled")
069    boolean demoPreviewEnabled = false;
070
071    @XNodeList(value = "supportedModes/mode", type = ArrayList.class, componentType = String.class)
072    List<String> supportedModes;
073
074    @XNode("handlingLabels")
075    boolean handlingLabels = false;
076
077    /**
078     * List of supported controls (controls checked on subwidgets configuration).
079     */
080    @XNodeList(value = "supportedControls/control", type = ArrayList.class, componentType = String.class)
081    List<String> supportedControls;
082
083    @XNode("containingForm")
084    boolean containingForm = false;
085
086    @XNodeList(value = "categories/category", type = ArrayList.class, componentType = String.class)
087    List<String> categories;
088
089    @XNodeMap(value = "properties/layouts", key = "@mode", type = HashMap.class, componentType = LayoutDescriptors.class)
090    Map<String, LayoutDescriptors> propertyLayouts;
091
092    @XNodeMap(value = "properties/defaultValues", key = "@mode", type = HashMap.class, componentType = PropertiesDescriptor.class)
093    Map<String, PropertiesDescriptor> defaultPropertyValues;
094
095    Map<String, Serializable> properties;
096
097    public List<String> getCategories() {
098        return categories;
099    }
100
101    public String getDescription() {
102        return description;
103    }
104
105    @XContent("description")
106    public void setDescription(DocumentFragment descriptionDOM) {
107        try {
108            OutputFormat of = new OutputFormat();
109            of.setOmitXMLDeclaration(true);
110            this.description = DOMSerializer.toString(descriptionDOM, of).trim();
111        } catch (IOException e) {
112            log.error(e, e);
113        }
114    }
115
116    public String getTitle() {
117        return title;
118    }
119
120    public String getDemoId() {
121        return demoId;
122    }
123
124    public boolean isDemoPreviewEnabled() {
125        return demoPreviewEnabled;
126    }
127
128    /**
129     * @since 5.6
130     */
131    public boolean isHandlingLabels() {
132        return handlingLabels;
133    }
134
135    /**
136     * @since 5.9.1
137     */
138    public List<String> getSupportedControls() {
139        return supportedControls;
140    }
141
142    public boolean isContainingForm() {
143        return containingForm;
144    }
145
146    public Map<String, Serializable> getConfProperties() {
147        return properties;
148    }
149
150    public Serializable getConfProperty(String propName) {
151        if (properties == null) {
152            return null;
153        }
154        return properties.get(propName);
155    }
156
157    @XNode("confProperties")
158    public void setConfProperties(PropertiesDescriptor propsDesc) {
159        properties = propsDesc.getProperties();
160    }
161
162    protected List<LayoutDefinition> getLayouts(Map<String, LayoutDescriptors> descs, String mode, String additionalMode) {
163        if (descs != null) {
164            List<LayoutDefinition> res = new ArrayList<LayoutDefinition>();
165            if (additionalMode != null) {
166                LayoutDescriptors defaultLayouts = descs.get(additionalMode);
167                if (defaultLayouts != null) {
168                    List<LayoutDefinition> defaultLayoutsList = defaultLayouts.getLayouts();
169                    if (defaultLayoutsList != null) {
170                        res.addAll(defaultLayoutsList);
171                    }
172                }
173            }
174            LayoutDescriptors modeLayouts = descs.get(mode);
175            if (modeLayouts != null) {
176                List<LayoutDefinition> modeLayoutsList = modeLayouts.getLayouts();
177                if (modeLayoutsList != null) {
178                    res.addAll(modeLayoutsList);
179                }
180            }
181            return res;
182        }
183        return null;
184    }
185
186    protected Map<String, List<LayoutDefinition>> getLayouts(Map<String, LayoutDescriptors> descs) {
187        if (descs != null) {
188            Map<String, List<LayoutDefinition>> res = new HashMap<String, List<LayoutDefinition>>();
189            for (Map.Entry<String, LayoutDescriptors> entry : descs.entrySet()) {
190                res.put(entry.getKey(), entry.getValue().getLayouts());
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    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
202        return getLayouts(propertyLayouts);
203    }
204
205    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
206        if (defaultPropertyValues != null) {
207            Map<String, Map<String, Serializable>> res = new HashMap<String, Map<String, Serializable>>();
208            for (Map.Entry<String, PropertiesDescriptor> entry : defaultPropertyValues.entrySet()) {
209                res.put(entry.getKey(), entry.getValue().getProperties());
210            }
211            return res;
212        }
213        return null;
214    }
215
216    public String getSinceVersion() {
217        return sinceVersion;
218    }
219
220    /**
221     * @since 5.6
222     */
223    public String getDeprecatedVersion() {
224        return deprecatedVersion;
225    }
226
227    public List<String> getSupportedModes() {
228        return supportedModes;
229    }
230
231    public LayoutTypeConfiguration getLayoutTypeConfiguration() {
232        LayoutTypeConfigurationImpl res = new LayoutTypeConfigurationImpl();
233        res.setSinceVersion(getSinceVersion());
234        res.setDeprecatedVersion(getDeprecatedVersion());
235        res.setTitle(getTitle());
236        res.setDescription(getDescription());
237        res.setDemoId(getDemoId());
238        res.setDemoPreviewEnabled(isDemoPreviewEnabled());
239        res.setSupportedModes(getSupportedModes());
240        res.setHandlingLabels(isHandlingLabels());
241        res.setContainingForm(isContainingForm());
242        res.setCategories(getCategories());
243        res.setPropertyLayouts(getPropertyLayouts());
244        res.setDefaultPropertyValues(getDefaultPropertyValues());
245        res.setSupportedControls(getSupportedControls());
246        return res;
247    }
248
249}