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 */
020
021package org.nuxeo.ecm.core.schema.types;
022
023import java.util.Collection;
024import java.util.Collections;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * A Composite Type resolves fields for several schemas.
031 */
032public class CompositeTypeImpl extends ComplexTypeImpl implements CompositeType {
033
034    private static final long serialVersionUID = 1L;
035
036    /** The schemas for this composite type. */
037    protected Map<String, Schema> schemas = new LinkedHashMap<String, Schema>();
038
039    /**
040     * Constructs a composite type. Schemas must include those from the super type.
041     */
042    public CompositeTypeImpl(CompositeType superType, String schema, String name, List<Schema> schemaList) {
043        super(superType, schema, name);
044        if (schemaList == null) {
045            schemaList = Collections.emptyList();
046        }
047        for (Schema s : schemaList) {
048            schemas.put(s.getName(), s);
049            for (Field field : s.getFields()) {
050                addField(field);
051            }
052        }
053    }
054
055    @Override
056    public boolean hasSchemas() {
057        return !schemas.isEmpty();
058    }
059
060    @Override
061    public Schema getSchema(String name) {
062        return schemas.get(name);
063    }
064
065    @Override
066    public boolean hasSchema(String name) {
067        return schemas.containsKey(name);
068    }
069
070    @Override
071    public String[] getSchemaNames() {
072        return schemas.keySet().toArray(new String[0]);
073    }
074
075    @Override
076    public Collection<Schema> getSchemas() {
077        return schemas.values();
078    }
079
080    @Override
081    public Field getField(QName name) {
082        // TODO can this be unified with super behavior?
083        return fieldsByName.get(name.getPrefixedName());
084    }
085
086    @Override
087    public boolean isComplexType() {
088        return false;
089    }
090
091    @Override
092    public boolean isCompositeType() {
093        return true;
094    }
095
096}