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.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.WidgetTypeConfiguration;
034import org.nuxeo.ecm.platform.forms.layout.api.WidgetTypeDefinition;
035
036import com.fasterxml.jackson.core.JsonGenerator;
037
038/**
039 * @author Anahide Tchertchian
040 * @since 5.4
041 * @since 10.1 converted to a marshaller
042 */
043@Setup(mode = SINGLETON, priority = REFERENCE)
044public class WidgetTypeDefinitionJsonWriter extends AbstractJsonWriter<WidgetTypeDefinition> {
045
046    @Override
047    public void write(WidgetTypeDefinition entity, JsonGenerator jg) throws IOException {
048        jg.writeStartObject();
049        jg.writeStringField("name", entity.getName());
050
051        List<String> aliases = entity.getAliases();
052        if (CollectionUtils.isNotEmpty(aliases)) {
053            writeSerializableListField("aliases", aliases, jg);
054        }
055        jg.writeStringField("handlerClassName", entity.getHandlerClassName());
056
057        Map<String, String> properties = entity.getProperties();
058        if (MapUtils.isNotEmpty(properties)) {
059            // sort so that order is deterministic
060            writeSerializableMapField("properties", new TreeMap<>(properties), jg);
061        }
062
063        WidgetTypeConfiguration configuration = entity.getConfiguration();
064        if (configuration != null) {
065            writeEntityField("configuration", configuration, jg);
066        }
067        jg.writeEndObject();
068    }
069
070}