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.Map;
027
028import org.apache.commons.collections.MapUtils;
029import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter;
030import org.nuxeo.ecm.core.io.registry.reflect.Setup;
031import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
032import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOptions;
033
034import com.fasterxml.jackson.core.JsonGenerator;
035
036/**
037 * @since 10.1
038 */
039@Setup(mode = SINGLETON, priority = REFERENCE)
040public class WidgetSelectOptionJsonWriter extends AbstractJsonWriter<WidgetSelectOption> {
041
042    @Override
043    public void write(WidgetSelectOption entity, JsonGenerator jg) throws IOException {
044        jg.writeStartObject();
045        boolean isMulti = entity instanceof WidgetSelectOptions;
046        jg.writeBooleanField("multiple", isMulti);
047        Serializable value = entity.getValue();
048        if (value != null) {
049            writeSerializableField("value", value, jg);
050        }
051        String var = entity.getVar();
052        if (var != null) {
053            jg.writeStringField("var", var);
054        }
055        String itemLabel = entity.getItemLabel();
056        if (itemLabel != null) {
057            jg.writeStringField("itemLabel", itemLabel);
058        }
059        Map<String, String> labels = entity.getItemLabels();
060        if (MapUtils.isNotEmpty(labels)) {
061            writeSerializableMapField("itemLabels", labels, jg);
062        }
063        String itemValue = entity.getItemValue();
064        if (itemValue != null) {
065            jg.writeStringField("itemValue", itemValue);
066        }
067        Serializable itemDisabled = entity.getItemDisabled();
068        if (itemDisabled != null) {
069            writeSerializableField("itemDisabled", itemDisabled, jg);
070        }
071        Serializable itemRendered = entity.getItemRendered();
072        if (itemRendered != null) {
073            writeSerializableField("itemRendered", itemRendered, jg);
074        }
075        if (isMulti) {
076            WidgetSelectOptions entities = (WidgetSelectOptions) entity;
077            String ordering = entities.getOrdering();
078            if (ordering != null) {
079                jg.writeStringField("ordering", ordering);
080            }
081            Boolean caseSensitive = entities.getCaseSensitive();
082            if (caseSensitive != null) {
083                jg.writeBooleanField("caseSensitive", caseSensitive.booleanValue());
084            }
085        }
086        jg.writeEndObject();
087    }
088
089}