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.marshallers.json.AbstractJsonWriter; 035import org.nuxeo.ecm.core.io.registry.reflect.Setup; 036import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition; 037import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeConfiguration; 038 039import com.fasterxml.jackson.core.JsonGenerator; 040 041/** 042 * @since 6.0 043 * @since 10.1 converted to a marshaller 044 */ 045@Setup(mode = SINGLETON, priority = REFERENCE) 046public class LayoutTypeConfigurationJsonWriter extends AbstractJsonWriter<LayoutTypeConfiguration> { 047 048 @Override 049 public void write(LayoutTypeConfiguration entity, JsonGenerator jg) throws IOException { 050 jg.writeStartObject(); 051 jg.writeStringField("title", entity.getTitle()); 052 String description = entity.getDescription(); 053 if (StringUtils.isNotBlank(description)) { 054 jg.writeStringField("description", description); 055 } 056 String demoId = entity.getDemoId(); 057 if (demoId != null) { 058 jg.writeObjectFieldStart("demo"); 059 jg.writeStringField("id", demoId); 060 jg.writeBooleanField("previewEnabled", entity.isDemoPreviewEnabled()); 061 jg.writeEndObject(); 062 } 063 jg.writeStringField("sinceVersion", entity.getSinceVersion()); 064 String deprVersion = entity.getDeprecatedVersion(); 065 if (StringUtils.isNotBlank(deprVersion)) { 066 jg.writeStringField("deprecatedVersion", deprVersion); 067 } 068 069 List<String> supportedModes = entity.getSupportedModes(); 070 if (CollectionUtils.isNotEmpty(supportedModes)) { 071 writeSerializableListField("supportedModes", supportedModes, jg); 072 } 073 074 if (entity.isHandlingLabels()) { 075 jg.writeBooleanField("handlingLabels", entity.isHandlingLabels()); 076 } 077 078 List<String> supportedControls = entity.getSupportedControls(); 079 if (CollectionUtils.isNotEmpty(supportedControls)) { 080 writeSerializableListField("supportedControls", supportedControls, jg); 081 } 082 083 if (entity.isContainingForm()) { 084 jg.writeBooleanField("containingForm", true); 085 } 086 087 List<String> categories = entity.getCategories(); 088 if (CollectionUtils.isNotEmpty(categories)) { 089 writeSerializableListField("categories", categories, jg); 090 } 091 092 Map<String, List<LayoutDefinition>> propertyLayouts = entity.getPropertyLayouts(); 093 if (MapUtils.isNotEmpty(propertyLayouts) 094 && propertyLayouts.values().stream().anyMatch(CollectionUtils::isNotEmpty)) { 095 jg.writeObjectFieldStart("properties"); 096 jg.writeObjectFieldStart("layouts"); 097 // sort so that order is deterministic 098 for (Entry<String, List<LayoutDefinition>> entry : new TreeMap<>(propertyLayouts).entrySet()) { 099 writeSerializableListField(entry.getKey(), entry.getValue(), jg); 100 } 101 jg.writeEndObject(); 102 jg.writeEndObject(); 103 } 104 105 Map<String, Map<String, Serializable>> defaultPropValues = entity.getDefaultPropertyValues(); 106 if (MapUtils.isNotEmpty(defaultPropValues)) { 107 jg.writeObjectFieldStart("defaultPropertyValues"); 108 // sort so that order is deterministic 109 for (Entry<String, Map<String, Serializable>> entry : new TreeMap<>(defaultPropValues).entrySet()) { 110 writeSerializableMapField(entry.getKey(), entry.getValue(), jg); 111 } 112 jg.writeEndObject(); 113 } 114 jg.writeEndObject(); 115 } 116 117}