001/* 002 * (C) Copyright 2006-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 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.core.schema; 021 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.List; 025 026import org.apache.commons.lang3.StringUtils; 027import org.nuxeo.common.xmap.annotation.XNode; 028import org.nuxeo.common.xmap.annotation.XNodeList; 029import org.nuxeo.common.xmap.annotation.XObject; 030 031/** 032 * Document Type Descriptor. 033 * <p> 034 * Can be used to delay document type registration when not all prerequisites are met (e.g. supertype was not yet 035 * registered). 036 * <p> 037 * In this case the descriptor containing all the information needed to register the document is put in a queue waiting 038 * for the prerequisites to be met. 039 * 040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 041 */ 042@XObject("doctype") 043public class DocumentTypeDescriptor { 044 045 @XNode("@name") 046 public String name; 047 048 @XNodeList(value = "schema", type = SchemaDescriptor[].class, componentType = SchemaDescriptor.class) 049 public SchemaDescriptor[] schemas; 050 051 @XNode("@extends") 052 public String superTypeName; 053 054 @XNodeList(value = "facet@name", type = String[].class, componentType = String.class) 055 public String[] facets; 056 057 @XNode("prefetch") 058 public String prefetch; 059 060 @XNode("@append") 061 public boolean append = false; 062 063 @XNodeList(value = "subtypes/type", type = String[].class, componentType = String.class) 064 public String[] subtypes = new String[0]; 065 066 @XNodeList(value = "subtypes-forbidden/type", type = String[].class, componentType = String.class) 067 public String[] forbiddenSubtypes = new String[0]; 068 069 public DocumentTypeDescriptor() { 070 } 071 072 public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets) { 073 this.name = name; 074 this.superTypeName = superTypeName; 075 this.schemas = schemas; 076 this.facets = facets; 077 } 078 079 public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets, 080 String[] subtypes, String[] forbiddenSubtypes) { 081 this(superTypeName, name, schemas, facets); 082 this.subtypes = subtypes; 083 this.forbiddenSubtypes = forbiddenSubtypes; 084 } 085 086 @Override 087 public String toString() { 088 return "DocType: " + name; 089 } 090 091 public DocumentTypeDescriptor clone() { 092 DocumentTypeDescriptor clone = new DocumentTypeDescriptor(); 093 clone.name = name; 094 clone.schemas = schemas; 095 clone.superTypeName = superTypeName; 096 clone.facets = facets; 097 clone.prefetch = prefetch; 098 clone.append = append; 099 clone.subtypes = subtypes; 100 clone.forbiddenSubtypes = forbiddenSubtypes; 101 return clone; 102 } 103 104 public DocumentTypeDescriptor merge(DocumentTypeDescriptor other) { 105 // only merge schemas, facets and prefetch 106 if (schemas == null) { 107 schemas = other.schemas; 108 } else { 109 if (other.schemas != null) { 110 List<SchemaDescriptor> mergedSchemas = new ArrayList<>(Arrays.asList(schemas)); 111 mergedSchemas.addAll(Arrays.asList(other.schemas)); 112 schemas = mergedSchemas.toArray(new SchemaDescriptor[mergedSchemas.size()]); 113 } 114 } 115 if (facets == null) { 116 facets = other.facets; 117 } else { 118 if (other.facets != null) { 119 List<String> mergedFacets = new ArrayList<>(Arrays.asList(facets)); 120 mergedFacets.addAll(Arrays.asList(other.facets)); 121 facets = mergedFacets.toArray(new String[mergedFacets.size()]); 122 } 123 } 124 if (prefetch == null) { 125 prefetch = other.prefetch; 126 } else { 127 if (other.prefetch != null) { 128 prefetch = prefetch + " " + other.prefetch; 129 } 130 } 131 132 // update supertype 133 if (StringUtils.isEmpty(superTypeName) && StringUtils.isNotEmpty(other.superTypeName)) { 134 superTypeName = other.superTypeName; 135 } 136 137 // merge subtypes 138 if (subtypes == null) { 139 subtypes = other.subtypes; 140 } else if (other.subtypes != null) { 141 List<String> mergedTypes = new ArrayList<>(Arrays.asList(subtypes)); 142 mergedTypes.addAll(Arrays.asList(other.subtypes)); 143 subtypes = mergedTypes.toArray(new String[mergedTypes.size()]); 144 } 145 if (forbiddenSubtypes == null) { 146 forbiddenSubtypes = other.forbiddenSubtypes; 147 } else if (other.forbiddenSubtypes != null) { 148 List<String> mergedTypes = new ArrayList<>(Arrays.asList(forbiddenSubtypes)); 149 mergedTypes.addAll(Arrays.asList(other.forbiddenSubtypes)); 150 forbiddenSubtypes = mergedTypes.toArray(new String[mergedTypes.size()]); 151 } 152 153 return this; 154 } 155 156}