001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.schema;
016
017import org.nuxeo.common.xmap.annotation.XNode;
018import org.nuxeo.common.xmap.annotation.XNodeList;
019import org.nuxeo.common.xmap.annotation.XObject;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026
027/**
028 * Document Type Descriptor.
029 * <p>
030 * Can be used to delay document type registration when not all prerequisites are met (e.g. supertype was not yet
031 * registered).
032 * <p>
033 * In this case the descriptor containing all the information needed to register the document is put in a queue waiting
034 * for the prerequisites to be met.
035 *
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@XObject("doctype")
039public class DocumentTypeDescriptor {
040
041    @XNode("@name")
042    public String name;
043
044    @XNodeList(value = "schema", type = SchemaDescriptor[].class, componentType = SchemaDescriptor.class)
045    public SchemaDescriptor[] schemas;
046
047    @XNode("@extends")
048    public String superTypeName;
049
050    @XNodeList(value = "facet@name", type = String[].class, componentType = String.class)
051    public String[] facets;
052
053    @XNode("prefetch")
054    public String prefetch;
055
056    @XNode("@append")
057    public boolean append = false;
058
059    public DocumentTypeDescriptor() {
060    }
061
062    public DocumentTypeDescriptor(String superTypeName, String name, SchemaDescriptor[] schemas, String[] facets) {
063        this.name = name;
064        this.superTypeName = superTypeName;
065        this.schemas = schemas;
066        this.facets = facets;
067    }
068
069    @Override
070    public String toString() {
071        return "DocType: " + name;
072    }
073
074    public DocumentTypeDescriptor clone() {
075        DocumentTypeDescriptor clone = new DocumentTypeDescriptor();
076        clone.name = name;
077        clone.schemas = schemas;
078        clone.superTypeName = superTypeName;
079        clone.facets = facets;
080        clone.prefetch = prefetch;
081        clone.append = append;
082        return clone;
083    }
084
085    public DocumentTypeDescriptor merge(DocumentTypeDescriptor other) {
086        // only merge schemas, facets and prefetch
087        if (schemas == null) {
088            schemas = other.schemas;
089        } else {
090            if (other.schemas != null) {
091                List<SchemaDescriptor> mergedSchemas = new ArrayList<SchemaDescriptor>(Arrays.asList(schemas));
092                mergedSchemas.addAll(Arrays.asList(other.schemas));
093                schemas = mergedSchemas.toArray(new SchemaDescriptor[mergedSchemas.size()]);
094            }
095        }
096        if (facets == null) {
097            facets = other.facets;
098        } else {
099            if (other.facets != null) {
100                List<String> mergedFacets = new ArrayList<String>(Arrays.asList(facets));
101                mergedFacets.addAll(Arrays.asList(other.facets));
102                facets = mergedFacets.toArray(new String[mergedFacets.size()]);
103            }
104        }
105        if (prefetch == null) {
106            prefetch = other.prefetch;
107        } else {
108            if (other.prefetch != null) {
109                prefetch = prefetch + " " + other.prefetch;
110            }
111        }
112        return this;
113    }
114
115}