001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.forms.layout.export;
020
021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
023
024import java.io.IOException;
025import java.io.Serializable;
026import java.util.List;
027import java.util.Map;
028import java.util.Map.Entry;
029import java.util.TreeMap;
030
031import org.apache.commons.collections.CollectionUtils;
032import org.apache.commons.collections.MapUtils;
033import org.apache.commons.lang3.StringUtils;
034import org.nuxeo.ecm.core.io.registry.reflect.Setup;
035import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
036import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
037import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeConfiguration;
038
039import com.fasterxml.jackson.core.JsonGenerator;
040
041/**
042 * @since 10.1
043 */
044@Setup(mode = SINGLETON, priority = REFERENCE)
045public class WidgetTypeConfigurationJsonWriter extends AbstractLayoutJsonWriter<WidgetTypeConfiguration> {
046
047    @Override
048    public void write(WidgetTypeConfiguration entity, JsonGenerator jg) throws IOException {
049        jg.writeStartObject();
050        jg.writeStringField("title", entity.getTitle());
051        jg.writeStringField("description", entity.getDescription());
052        String demoId = entity.getDemoId();
053        if (demoId != null) {
054            jg.writeObjectFieldStart("demo");
055            jg.writeStringField("id", demoId);
056            jg.writeBooleanField("previewEnabled", entity.isDemoPreviewEnabled());
057            jg.writeEndObject();
058        }
059        jg.writeStringField("sinceVersion", entity.getSinceVersion());
060        String deprVersion = entity.getDeprecatedVersion();
061        if (StringUtils.isNotBlank(deprVersion)) {
062            jg.writeStringField("deprecatedVersion", deprVersion);
063        }
064        Map<String, Serializable> confProperties = entity.getConfProperties();
065        if (MapUtils.isNotEmpty(confProperties)) {
066            writeSerializableMapField("confProperties", confProperties, jg);
067        }
068
069        List<String> supportedModes = entity.getSupportedModes();
070        if (CollectionUtils.isNotEmpty(supportedModes)) {
071            writeSerializableListField("supportedModes", supportedModes, jg);
072        }
073
074        if (entity.isAcceptingSubWidgets()) {
075            jg.writeBooleanField("acceptingSubWidgets", entity.isAcceptingSubWidgets());
076        }
077        if (entity.isHandlingLabels()) {
078            jg.writeBooleanField("handlingLabels", entity.isHandlingLabels());
079        }
080        List<String> supportedControls = entity.getSupportedControls();
081        if (CollectionUtils.isNotEmpty(supportedControls)) {
082            writeSerializableListField("supportedControls", supportedControls, jg);
083        }
084        if (entity.isContainingForm()) {
085            jg.writeBooleanField("containingForm", true);
086        }
087
088        jg.writeObjectFieldStart("fields");
089        jg.writeBooleanField("list", entity.isList());
090        jg.writeBooleanField("complex", entity.isComplex());
091
092        List<String> supportedTypes = entity.getSupportedFieldTypes();
093        if (CollectionUtils.isNotEmpty(supportedTypes)) {
094            writeSerializableListField("supportedTypes", supportedTypes, jg);
095        }
096
097        List<String> defaultTypes = entity.getDefaultFieldTypes();
098        if (CollectionUtils.isNotEmpty(defaultTypes)) {
099            writeSerializableListField("defaultTypes", defaultTypes, jg);
100        }
101
102        List<FieldDefinition> fieldDefinitions = entity.getDefaultFieldDefinitions();
103        if (CollectionUtils.isNotEmpty(fieldDefinitions)) {
104            writeSerializableListField("defaultConfiguration", fieldDefinitions, jg);
105        }
106
107        Map<String, List<LayoutDefinition>> fieldLayouts = entity.getFieldLayouts();
108        if (MapUtils.isNotEmpty(fieldLayouts) && fieldLayouts.values().stream().anyMatch(CollectionUtils::isNotEmpty)) {
109            // sort so that order is deterministic
110            jg.writeObjectFieldStart("layouts");
111            for (Entry<String, List<LayoutDefinition>> entry : new TreeMap<>(fieldLayouts).entrySet()) {
112                writeSerializableListField(entry.getKey(), entry.getValue(), jg);
113            }
114            jg.writeEndObject();
115        }
116        // end of fields
117        jg.writeEndObject();
118
119        List<String> categories = entity.getCategories();
120        if (CollectionUtils.isNotEmpty(categories)) {
121            writeSerializableListField("categories", categories, jg);
122        }
123
124        Map<String, List<LayoutDefinition>> propertyLayouts = entity.getPropertyLayouts();
125        if (MapUtils.isNotEmpty(propertyLayouts)
126                && propertyLayouts.values().stream().anyMatch(CollectionUtils::isNotEmpty)) {
127            // sort so that order is deterministic
128            jg.writeObjectFieldStart("properties");
129            jg.writeObjectFieldStart("layouts");
130            for (Entry<String, List<LayoutDefinition>> entry : new TreeMap<>(propertyLayouts).entrySet()) {
131                writeSerializableListField(entry.getKey(), entry.getValue(), jg);
132            }
133            jg.writeEndObject();
134            jg.writeEndObject();
135        }
136
137        Map<String, Map<String, Serializable>> defaultPropertyValues = entity.getDefaultPropertyValues();
138        if (MapUtils.isNotEmpty(defaultPropertyValues)
139                && defaultPropertyValues.values().stream().anyMatch(MapUtils::isNotEmpty)) {
140            writeSerializableMapMapField("defaultPropertyValues", cleanAndSort(defaultPropertyValues), jg);
141        }
142
143        Map<String, Map<String, Serializable>> defaultControlValues = entity.getDefaultControlValues();
144        if (MapUtils.isNotEmpty(defaultControlValues)
145                && defaultControlValues.values().stream().anyMatch(MapUtils::isNotEmpty)) {
146            writeSerializableMapMapField("defaultControlValues", cleanAndSort(defaultControlValues), jg);
147        }
148        jg.writeEndObject();
149    }
150
151}