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.Arrays;
027import java.util.Map;
028
029import org.apache.commons.collections.MapUtils;
030import org.apache.commons.lang3.ArrayUtils;
031import org.apache.commons.lang3.StringUtils;
032import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter;
033import org.nuxeo.ecm.core.io.registry.reflect.Setup;
034import org.nuxeo.ecm.platform.forms.layout.api.FieldDefinition;
035import org.nuxeo.ecm.platform.forms.layout.api.Layout;
036import org.nuxeo.ecm.platform.forms.layout.api.LayoutRow;
037import org.nuxeo.ecm.platform.forms.layout.api.Widget;
038import org.nuxeo.ecm.platform.forms.layout.api.WidgetSelectOption;
039
040import com.fasterxml.jackson.core.JsonGenerator;
041
042/**
043 * @since 10.1
044 */
045@Setup(mode = SINGLETON, priority = REFERENCE)
046public class LayoutJsonWriter extends AbstractJsonWriter<Layout> {
047
048    @Override
049    public void write(Layout entity, JsonGenerator jg) throws IOException {
050        jg.writeStartObject();
051        jg.writeStringField("name", entity.getName());
052
053        String type = entity.getType();
054        if (type != null) {
055            jg.writeStringField("type", type);
056        }
057
058        String typeCat = entity.getTypeCategory();
059        if (typeCat != null) {
060            jg.writeStringField("typeCategory", typeCat);
061        }
062
063        jg.writeStringField("mode", entity.getMode());
064
065        String template = entity.getTemplate();
066        if (template != null) {
067            jg.writeStringField("template", template);
068        }
069
070        Map<String, Serializable> properties = entity.getProperties();
071        if (MapUtils.isNotEmpty(properties)) {
072            writeSerializableMapField("properties", properties, jg);
073        }
074
075        LayoutRow[] lRows = entity.getRows();
076        if (lRows != null) {
077            jg.writeArrayFieldStart("rows");
078            for (LayoutRow lRow : lRows) {
079                writeLayoutRow(lRow, jg);
080            }
081            jg.writeEndArray();
082        }
083
084        jg.writeEndObject();
085    }
086
087    protected void writeLayoutRow(LayoutRow layoutRow, JsonGenerator jg) throws IOException {
088        jg.writeStartObject();
089        String name = layoutRow.getName();
090        if (name != null) {
091            jg.writeStringField("name", name);
092        }
093        // fill selection info only if that's not the default value from the
094        // definition
095        if (layoutRow.isAlwaysSelected()) {
096            jg.writeBooleanField("alwaysSelected", true);
097        }
098        if (!layoutRow.isSelectedByDefault()) {
099            jg.writeBooleanField("selectedByDefault", false);
100        }
101
102        Map<String, Serializable> properties = layoutRow.getProperties();
103        if (MapUtils.isNotEmpty(properties)) {
104            writeSerializableMapField("properties", properties, jg);
105        }
106        Widget[] widgets = layoutRow.getWidgets();
107        if (ArrayUtils.isNotEmpty(widgets)) {
108            jg.writeArrayFieldStart("widgets");
109            for (Widget widget : widgets) {
110                writeWidget(widget, jg);
111            }
112            jg.writeEndArray();
113        }
114        jg.writeEndObject();
115    }
116
117    protected void writeWidget(Widget widget, JsonGenerator jg) throws IOException {
118        jg.writeStartObject();
119        jg.writeStringField("name", widget.getName());
120        jg.writeStringField("type", widget.getType());
121        jg.writeStringField("typeCategory", widget.getTypeCategory());
122        jg.writeStringField("mode", widget.getMode());
123        jg.writeStringField("label", widget.getLabel());
124        String helpLabel = widget.getHelpLabel();
125        if (StringUtils.isNotBlank(helpLabel)) {
126            jg.writeStringField("helpLabel", helpLabel);
127        }
128        jg.writeBooleanField("translated", widget.isTranslated());
129        jg.writeBooleanField("handlingLabels", widget.isHandlingLabels());
130        FieldDefinition[] fieldDefs = widget.getFieldDefinitions();
131        if (ArrayUtils.isNotEmpty(fieldDefs)) {
132            jg.writeArrayFieldStart("fields");
133            for (FieldDefinition fieldDef : fieldDefs) {
134                writeEntity(fieldDef, jg);
135            }
136            jg.writeEndArray();
137        }
138
139        Widget[] subWidgets = widget.getSubWidgets();
140        if (ArrayUtils.isNotEmpty(subWidgets)) {
141            jg.writeArrayFieldStart("subWidgets");
142            for (Widget wDef : subWidgets) {
143                writeWidget(wDef, jg);
144            }
145            jg.writeEndArray();
146        }
147
148        Map<String, Serializable> properties = widget.getProperties();
149        if (MapUtils.isNotEmpty(properties)) {
150            writeSerializableMapField("properties", properties, jg);
151        }
152
153        Map<String, Serializable> controls = widget.getControls();
154        if (MapUtils.isNotEmpty(controls)) {
155            writeSerializableMapField("controls", controls, jg);
156        }
157
158        WidgetSelectOption[] selectOptions = widget.getSelectOptions();
159        if (ArrayUtils.isNotEmpty(selectOptions)) {
160            writeSerializableListField("selectOptions", Arrays.asList(selectOptions), jg);
161        }
162        jg.writeEndObject();
163    }
164
165}