001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.descriptors;
018
019import java.io.IOException;
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.apache.xml.serialize.OutputFormat;
029import org.nuxeo.common.xmap.DOMSerializer;
030import org.nuxeo.common.xmap.annotation.XContent;
031import org.nuxeo.common.xmap.annotation.XNode;
032import org.nuxeo.common.xmap.annotation.XNodeList;
033import org.nuxeo.common.xmap.annotation.XNodeMap;
034import org.nuxeo.common.xmap.annotation.XObject;
035import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
036import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration;
037import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutTypeConfigurationImpl;
038import org.w3c.dom.DocumentFragment;
039
040/**
041 * @since 6.0
042 */
043@XObject("configuration")
044public class LayoutTypeConfigurationDescriptor {
045
046    private static final Log log = LogFactory.getLog(LayoutTypeConfigurationDescriptor.class);
047
048    @XNode("sinceVersion")
049    String sinceVersion;
050
051    /**
052     * @since 5.6
053     */
054    @XNode("deprecatedVersion")
055    String deprecatedVersion;
056
057    @XNode("title")
058    String title;
059
060    // retrieve HTML tags => introspect DOM on setter
061    String description;
062
063    @XNode("demo@id")
064    String demoId;
065
066    @XNode("demo@previewEnabled")
067    boolean demoPreviewEnabled = false;
068
069    @XNodeList(value = "supportedModes/mode", type = ArrayList.class, componentType = String.class)
070    List<String> supportedModes;
071
072    @XNode("handlingLabels")
073    boolean handlingLabels = false;
074
075    /**
076     * List of supported controls (controls checked on subwidgets configuration).
077     */
078    @XNodeList(value = "supportedControls/control", type = ArrayList.class, componentType = String.class)
079    List<String> supportedControls;
080
081    @XNode("containingForm")
082    boolean containingForm = false;
083
084    @XNodeList(value = "categories/category", type = ArrayList.class, componentType = String.class)
085    List<String> categories;
086
087    @XNodeMap(value = "properties/layouts", key = "@mode", type = HashMap.class, componentType = LayoutDescriptors.class)
088    Map<String, LayoutDescriptors> propertyLayouts;
089
090    @XNodeMap(value = "properties/defaultValues", key = "@mode", type = HashMap.class, componentType = PropertiesDescriptor.class)
091    Map<String, PropertiesDescriptor> defaultPropertyValues;
092
093    Map<String, Serializable> properties;
094
095    public List<String> getCategories() {
096        return categories;
097    }
098
099    public String getDescription() {
100        return description;
101    }
102
103    @XContent("description")
104    public void setDescription(DocumentFragment descriptionDOM) {
105        try {
106            OutputFormat of = new OutputFormat();
107            of.setOmitXMLDeclaration(true);
108            this.description = DOMSerializer.toString(descriptionDOM, of).trim();
109        } catch (IOException e) {
110            log.error(e, e);
111        }
112    }
113
114    public String getTitle() {
115        return title;
116    }
117
118    public String getDemoId() {
119        return demoId;
120    }
121
122    public boolean isDemoPreviewEnabled() {
123        return demoPreviewEnabled;
124    }
125
126    /**
127     * @since 5.6
128     */
129    public boolean isHandlingLabels() {
130        return handlingLabels;
131    }
132
133    /**
134     * @since 5.9.1
135     */
136    public List<String> getSupportedControls() {
137        return supportedControls;
138    }
139
140    public boolean isContainingForm() {
141        return containingForm;
142    }
143
144    public Map<String, Serializable> getConfProperties() {
145        return properties;
146    }
147
148    public Serializable getConfProperty(String propName) {
149        if (properties == null) {
150            return null;
151        }
152        return properties.get(propName);
153    }
154
155    @XNode("confProperties")
156    public void setConfProperties(PropertiesDescriptor propsDesc) {
157        properties = propsDesc.getProperties();
158    }
159
160    protected List<LayoutDefinition> getLayouts(Map<String, LayoutDescriptors> descs, String mode, String additionalMode) {
161        if (descs != null) {
162            List<LayoutDefinition> res = new ArrayList<LayoutDefinition>();
163            if (additionalMode != null) {
164                LayoutDescriptors defaultLayouts = descs.get(additionalMode);
165                if (defaultLayouts != null) {
166                    List<LayoutDefinition> defaultLayoutsList = defaultLayouts.getLayouts();
167                    if (defaultLayoutsList != null) {
168                        res.addAll(defaultLayoutsList);
169                    }
170                }
171            }
172            LayoutDescriptors modeLayouts = descs.get(mode);
173            if (modeLayouts != null) {
174                List<LayoutDefinition> modeLayoutsList = modeLayouts.getLayouts();
175                if (modeLayoutsList != null) {
176                    res.addAll(modeLayoutsList);
177                }
178            }
179            return res;
180        }
181        return null;
182    }
183
184    protected Map<String, List<LayoutDefinition>> getLayouts(Map<String, LayoutDescriptors> descs) {
185        if (descs != null) {
186            Map<String, List<LayoutDefinition>> res = new HashMap<String, List<LayoutDefinition>>();
187            for (Map.Entry<String, LayoutDescriptors> entry : descs.entrySet()) {
188                res.put(entry.getKey(), entry.getValue().getLayouts());
189            }
190            return res;
191        }
192        return null;
193    }
194
195    public List<LayoutDefinition> getPropertyLayouts(String mode, String additionalMode) {
196        return getLayouts(propertyLayouts, mode, additionalMode);
197    }
198
199    public Map<String, List<LayoutDefinition>> getPropertyLayouts() {
200        return getLayouts(propertyLayouts);
201    }
202
203    public Map<String, Map<String, Serializable>> getDefaultPropertyValues() {
204        if (defaultPropertyValues != null) {
205            Map<String, Map<String, Serializable>> res = new HashMap<String, Map<String, Serializable>>();
206            for (Map.Entry<String, PropertiesDescriptor> entry : defaultPropertyValues.entrySet()) {
207                res.put(entry.getKey(), entry.getValue().getProperties());
208            }
209            return res;
210        }
211        return null;
212    }
213
214    public String getSinceVersion() {
215        return sinceVersion;
216    }
217
218    /**
219     * @since 5.6
220     */
221    public String getDeprecatedVersion() {
222        return deprecatedVersion;
223    }
224
225    public List<String> getSupportedModes() {
226        return supportedModes;
227    }
228
229    public LayoutTypeConfiguration getLayoutTypeConfiguration() {
230        LayoutTypeConfigurationImpl res = new LayoutTypeConfigurationImpl();
231        res.setSinceVersion(getSinceVersion());
232        res.setDeprecatedVersion(getDeprecatedVersion());
233        res.setTitle(getTitle());
234        res.setDescription(getDescription());
235        res.setDemoId(getDemoId());
236        res.setDemoPreviewEnabled(isDemoPreviewEnabled());
237        res.setSupportedModes(getSupportedModes());
238        res.setHandlingLabels(isHandlingLabels());
239        res.setContainingForm(isContainingForm());
240        res.setCategories(getCategories());
241        res.setPropertyLayouts(getPropertyLayouts());
242        res.setDefaultPropertyValues(getDefaultPropertyValues());
243        res.setSupportedControls(getSupportedControls());
244        return res;
245    }
246
247}