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 java.io.IOException; 022import java.io.Serializable; 023import java.util.Map; 024import java.util.Map.Entry; 025import java.util.SortedMap; 026import java.util.TreeMap; 027 028import org.apache.commons.collections.MapUtils; 029import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter; 030 031import com.fasterxml.jackson.core.JsonGenerator; 032 033/** 034 * @since 10.1 035 */ 036public abstract class AbstractLayoutJsonWriter<EntityType> extends AbstractJsonWriter<EntityType> { 037 038 protected SortedMap<String, Map<String, Serializable>> cleanAndSort(Map<String, Map<String, Serializable>> map) { 039 SortedMap<String, Map<String, Serializable>> sortedMap = new TreeMap<>(); 040 for (Entry<String, Map<String, Serializable>> entry : map.entrySet()) { 041 if (MapUtils.isNotEmpty(entry.getValue())) { 042 sortedMap.put(entry.getKey(), new TreeMap<>(entry.getValue())); 043 } 044 } 045 return sortedMap; 046 } 047 048 /** 049 * Writes a map whose values are a map. 050 * 051 * @param fieldName The name of the Json field in which the maps will be written. 052 * @param map The map to write. 053 * @param jg The {@link JsonGenerator} used to write the given map. 054 */ 055 protected void writeSerializableMapMapField(String fieldName, Map<String, Map<String, Serializable>> map, 056 JsonGenerator jg) throws IOException { 057 jg.writeObjectFieldStart(fieldName); 058 for (Entry<String, Map<String, Serializable>> entry : map.entrySet()) { 059 writeSerializableMapField(entry.getKey(), entry.getValue(), jg); 060 } 061 jg.writeEndObject(); 062 } 063 064}