001/* 002 * (C) Copyright 2020 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 * Charles Boidot <cboidot@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.platform.types; 021 022import static java.util.stream.Collectors.toSet; 023import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 024import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 025 026import java.io.IOException; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.Collections; 030import java.util.List; 031 032import org.apache.commons.lang3.BooleanUtils; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 035import org.nuxeo.ecm.core.io.registry.reflect.Setup; 036import org.nuxeo.ecm.core.schema.SchemaManager; 037import org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants; 038import org.nuxeo.runtime.api.Framework; 039 040import com.fasterxml.jackson.core.JsonGenerator; 041 042/** 043 * Enrich {@link DocumentModel} JSON object with an array of the document types that can be created under the current 044 * document taking account the local configuration. 045 * 046 * @since 11.5 047 */ 048@Setup(mode = SINGLETON, priority = REFERENCE) 049public class SubtypesJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 050 051 public static final String NAME = "subtypes"; 052 053 public SubtypesJsonEnricher() { 054 super(NAME); 055 } 056 057 @Override 058 public void write(JsonGenerator jg, DocumentModel enriched) throws IOException { 059 SchemaManager schemaManager = Framework.getService(SchemaManager.class); 060 Collection<String> subtypes = computeSubtypes(enriched); 061 jg.writeFieldName(NAME); 062 jg.writeStartArray(); 063 for (String subtype : subtypes) { 064 jg.writeStartObject(); 065 jg.writeStringField("type", subtype); 066 jg.writeArrayFieldStart("facets"); 067 for (String facet : schemaManager.getDocumentType(subtype).getFacets()) { 068 jg.writeString(facet); 069 } 070 jg.writeEndArray(); 071 jg.writeEndObject(); 072 } 073 jg.writeEndArray(); 074 } 075 076 protected Collection<String> computeSubtypes(DocumentModel enriched) { 077 Collection<String> defaultSubtypes = enriched.getDocumentType().getAllowedSubtypes(); 078 if (enriched.hasFacet(UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_FACET)) { 079 return computeLocalConfigurationSubtypes(enriched, defaultSubtypes); 080 } 081 return defaultSubtypes; 082 } 083 084 protected Collection<String> computeLocalConfigurationSubtypes(DocumentModel enriched, 085 Collection<String> defaultSubtypes) { 086 Boolean denyAllTypes = (Boolean) enriched.getPropertyValue( 087 UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENY_ALL_TYPES_PROPERTY); 088 if (BooleanUtils.isNotTrue(denyAllTypes)) { 089 String[] allowedTypesProperty = (String[]) enriched.getPropertyValue( 090 UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_ALLOWED_TYPES_PROPERTY); 091 String[] deniedTypesProperty = (String[]) enriched.getPropertyValue( 092 UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DENIED_TYPES_PROPERTY); 093 List<String> allowedTypes = allowedTypesProperty == null ? Collections.emptyList() 094 : Arrays.asList(allowedTypesProperty); 095 List<String> deniedTypes = deniedTypesProperty == null ? Collections.emptyList() 096 : Arrays.asList(deniedTypesProperty); 097 return defaultSubtypes.stream() 098 .filter(s -> !deniedTypes.contains(s)) 099 .filter(s -> allowedTypes.contains(s) || allowedTypes.isEmpty()) 100 .collect(toSet()); 101 } 102 return Collections.emptySet(); 103 } 104}