001/*
002 * (C) Copyright 2006-2012 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.schema;
021
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Set;
027
028import org.nuxeo.ecm.core.schema.types.CompositeTypeImpl;
029import org.nuxeo.ecm.core.schema.types.Schema;
030
031/**
032 * Implementation of a document type.
033 */
034public class DocumentTypeImpl extends CompositeTypeImpl implements DocumentType {
035
036    private static final long serialVersionUID = 1L;
037
038    protected Set<String> facets;
039
040    protected PrefetchInfo prefetchInfo;
041
042    protected Set<String> subtypes;
043
044    protected Set<String> forbiddenSubtypes;
045
046    protected Set<String> allowedSubtypes;
047
048    /**
049     * Constructs a document type. Schemas and facets must include those from the super type.
050     */
051    public DocumentTypeImpl(String name, DocumentType superType, List<Schema> schemas, Collection<String> facets,
052            PrefetchInfo prefetchInfo) {
053        super(superType, SchemaNames.DOCTYPES, name, schemas);
054        if (facets == null) {
055            this.facets = Collections.emptySet();
056        } else {
057            this.facets = new HashSet<String>(facets);
058        }
059        this.prefetchInfo = prefetchInfo;
060    }
061
062    public DocumentTypeImpl(String name) {
063        this(name, null, Collections.<Schema> emptyList(), Collections.<String> emptySet(), null);
064    }
065
066    public void setPrefetchInfo(PrefetchInfo prefetchInfo) {
067        this.prefetchInfo = prefetchInfo;
068    }
069
070    @Override
071    public PrefetchInfo getPrefetchInfo() {
072        return prefetchInfo;
073    }
074
075    @Override
076    public boolean isFile() {
077        return !facets.contains(FacetNames.FOLDERISH);
078    }
079
080    @Override
081    public boolean isFolder() {
082        return facets.contains(FacetNames.FOLDERISH);
083    }
084
085    @Override
086    public boolean isOrdered() {
087        return facets.contains(FacetNames.ORDERABLE);
088    }
089
090    @Override
091    public Set<String> getFacets() {
092        return facets;
093    }
094
095    @Override
096    public boolean hasFacet(String facetName) {
097        return facets.contains(facetName);
098    }
099
100    @Override
101    public Set<String> getSubtypes() {
102        return subtypes;
103    }
104
105    @Override
106    public void setSubtypes(Collection<String> subtypes) {
107        if (subtypes == null) {
108            this.subtypes = Collections.emptySet();
109        } else {
110            this.subtypes = new HashSet<>(subtypes);
111        }
112        allowedSubtypes = new HashSet<>(this.subtypes);
113        if (this.forbiddenSubtypes != null) {
114            allowedSubtypes.removeAll(this.forbiddenSubtypes);
115        }
116    }
117
118    @Override
119    public boolean hasSubtype(String subtype) {
120        return subtype.contains(subtype);
121    }
122
123    @Override
124    public Set<String> getForbiddenSubtypes() {
125        return forbiddenSubtypes;
126    }
127
128    @Override
129    public void setForbiddenSubtypes(Collection<String> forbiddenSubtypes) {
130        if (forbiddenSubtypes == null) {
131            this.forbiddenSubtypes = Collections.emptySet();
132        } else {
133            this.forbiddenSubtypes = new HashSet<>(forbiddenSubtypes);
134        }
135        if (this.subtypes != null) {
136            allowedSubtypes = new HashSet<>(this.subtypes);
137            allowedSubtypes.removeAll(this.forbiddenSubtypes);
138        }
139    }
140
141    @Override
142    public boolean hasForbiddenSubtype(String subtype) {
143        return forbiddenSubtypes.contains(subtype);
144    }
145
146    @Override
147    public Set<String> getAllowedSubtypes() {
148        return allowedSubtypes;
149    }
150
151    @Override
152    public boolean hasAllowedSubtype(String subtype) {
153        return allowedSubtypes.contains(subtype);
154    }
155
156}