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