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.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.util.List;
026import java.util.Map;
027import java.util.TreeMap;
028
029import org.apache.commons.collections.CollectionUtils;
030import org.apache.commons.collections.MapUtils;
031import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter;
032import org.nuxeo.ecm.core.io.registry.reflect.Setup;
033import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration;
034import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeDefinition;
035
036import com.fasterxml.jackson.core.JsonGenerator;
037
038/**
039 * @since 6.0
040 * @since 10.1 converted to a marshaller
041 */
042@Setup(mode = SINGLETON, priority = REFERENCE)
043public class LayoutTypeDefinitionJsonWriter extends AbstractJsonWriter<LayoutTypeDefinition> {
044
045    @Override
046    public void write(LayoutTypeDefinition entity, JsonGenerator jg) throws IOException {
047        jg.writeStartObject();
048        jg.writeStringField("name", entity.getName());
049
050        List<String> aliases = entity.getAliases();
051        if (CollectionUtils.isNotEmpty(aliases)) {
052            writeSerializableListField("aliases", aliases, jg);
053        }
054
055        Map<String, String> templates = entity.getTemplates();
056        if (MapUtils.isNotEmpty(templates)) {
057            writeSerializableMapField("templates", new TreeMap<>(templates), jg);
058        }
059
060        LayoutTypeConfiguration configuration = entity.getConfiguration();
061        if (configuration != null) {
062            writeEntityField("configuration", configuration, jg);
063        }
064        jg.writeEndObject();
065    }
066
067}