001/*
002 * (C) Copyright 2006-2019 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    /**
064     * Allows to exclude the doctype from copy operations for example.
065     *
066     * @since 11.1
067     */
068    @XNode("@special")
069    public Boolean special;
070
071    @XNodeList(value = "subtypes/type", type = String[].class, componentType = String.class)
072    public String[] subtypes = new String[0];
073
074    @XNodeList(value = "subtypes-forbidden/type", type = String[].class, componentType = String.class)
075    public String[] forbiddenSubtypes = new String[0];
076
077    public DocumentTypeDescriptor() {
078    }
079
080    public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets) {
081        this.name = name;
082        this.superTypeName = superTypeName;
083        this.schemas = schemas;
084        this.facets = facets;
085    }
086
087    public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets,
088            String[] subtypes, String[] forbiddenSubtypes) {
089        this(superTypeName, name, schemas, facets);
090        this.subtypes = subtypes;
091        this.forbiddenSubtypes = forbiddenSubtypes;
092    }
093
094    @Override
095    public String toString() {
096        return "DocType: " + name;
097    }
098
099    @Override
100    public DocumentTypeDescriptor clone() {
101        DocumentTypeDescriptor clone = new DocumentTypeDescriptor();
102        clone.name = name;
103        clone.schemas = schemas;
104        clone.superTypeName = superTypeName;
105        clone.facets = facets;
106        clone.prefetch = prefetch;
107        clone.append = append;
108        clone.special = special;
109        clone.subtypes = subtypes;
110        clone.forbiddenSubtypes = forbiddenSubtypes;
111        return clone;
112    }
113
114    public DocumentTypeDescriptor merge(DocumentTypeDescriptor other) {
115        // only merge schemas, facets and prefetch
116        if (schemas == null) {
117            schemas = other.schemas;
118        } else {
119            if (other.schemas != null) {
120                List<SchemaDescriptor> mergedSchemas = new ArrayList<>(Arrays.asList(schemas));
121                mergedSchemas.addAll(Arrays.asList(other.schemas));
122                schemas = mergedSchemas.toArray(new SchemaDescriptor[mergedSchemas.size()]);
123            }
124        }
125        if (facets == null) {
126            facets = other.facets;
127        } else {
128            if (other.facets != null) {
129                List<String> mergedFacets = new ArrayList<>(Arrays.asList(facets));
130                mergedFacets.addAll(Arrays.asList(other.facets));
131                facets = mergedFacets.toArray(new String[mergedFacets.size()]);
132            }
133        }
134        if (prefetch == null) {
135            prefetch = other.prefetch;
136        } else {
137            if (other.prefetch != null) {
138                prefetch = prefetch + " " + other.prefetch;
139            }
140        }
141
142        // update supertype
143        if (StringUtils.isEmpty(superTypeName) && StringUtils.isNotEmpty(other.superTypeName)) {
144            superTypeName = other.superTypeName;
145        }
146
147        // inherit the exclusion from copy
148        special = special == null ? other.special : special;
149
150        // merge subtypes
151        if (subtypes == null) {
152            subtypes = other.subtypes;
153        } else if (other.subtypes != null) {
154            List<String> mergedTypes = new ArrayList<>(Arrays.asList(subtypes));
155            mergedTypes.addAll(Arrays.asList(other.subtypes));
156            subtypes = mergedTypes.toArray(new String[mergedTypes.size()]);
157        }
158        if (forbiddenSubtypes == null) {
159            forbiddenSubtypes = other.forbiddenSubtypes;
160        } else if (other.forbiddenSubtypes != null) {
161            List<String> mergedTypes = new ArrayList<>(Arrays.asList(forbiddenSubtypes));
162            mergedTypes.addAll(Arrays.asList(other.forbiddenSubtypes));
163            forbiddenSubtypes = mergedTypes.toArray(new String[mergedTypes.size()]);
164        }
165
166        return this;
167    }
168}