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.HashSet;
031import java.util.List;
032import java.util.Set;
033
034/**
035 * Document Type Descriptor.
036 * <p>
037 * Can be used to delay document type registration when not all prerequisites are met (e.g. supertype was not yet
038 * registered).
039 * <p>
040 * In this case the descriptor containing all the information needed to register the document is put in a queue waiting
041 * for the prerequisites to be met.
042 *
043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
044 */
045@XObject("doctype")
046public class DocumentTypeDescriptor {
047
048    @XNode("@name")
049    public String name;
050
051    @XNodeList(value = "schema", type = SchemaDescriptor[].class, componentType = SchemaDescriptor.class)
052    public SchemaDescriptor[] schemas;
053
054    @XNode("@extends")
055    public String superTypeName;
056
057    @XNodeList(value = "facet@name", type = String[].class, componentType = String.class)
058    public String[] facets;
059
060    @XNode("prefetch")
061    public String prefetch;
062
063    @XNode("@append")
064    public boolean append = false;
065
066    public DocumentTypeDescriptor() {
067    }
068
069    public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets) {
070        this.name = name;
071        this.superTypeName = superTypeName;
072        this.schemas = schemas;
073        this.facets = facets;
074    }
075
076    @Override
077    public String toString() {
078        return "DocType: " + name;
079    }
080
081    public DocumentTypeDescriptor clone() {
082        DocumentTypeDescriptor clone = new DocumentTypeDescriptor();
083        clone.name = name;
084        clone.schemas = schemas;
085        clone.superTypeName = superTypeName;
086        clone.facets = facets;
087        clone.prefetch = prefetch;
088        clone.append = append;
089        return clone;
090    }
091
092    public DocumentTypeDescriptor merge(DocumentTypeDescriptor other) {
093        // only merge schemas, facets and prefetch
094        if (schemas == null) {
095            schemas = other.schemas;
096        } else {
097            if (other.schemas != null) {
098                List<SchemaDescriptor> mergedSchemas = new ArrayList<SchemaDescriptor>(Arrays.asList(schemas));
099                mergedSchemas.addAll(Arrays.asList(other.schemas));
100                schemas = mergedSchemas.toArray(new SchemaDescriptor[mergedSchemas.size()]);
101            }
102        }
103        if (facets == null) {
104            facets = other.facets;
105        } else {
106            if (other.facets != null) {
107                List<String> mergedFacets = new ArrayList<String>(Arrays.asList(facets));
108                mergedFacets.addAll(Arrays.asList(other.facets));
109                facets = mergedFacets.toArray(new String[mergedFacets.size()]);
110            }
111        }
112        if (prefetch == null) {
113            prefetch = other.prefetch;
114        } else {
115            if (other.prefetch != null) {
116                prefetch = prefetch + " " + other.prefetch;
117            }
118        }
119        return this;
120    }
121
122}