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.types;
021
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Set;
025
026import org.nuxeo.ecm.core.schema.Namespace;
027import org.nuxeo.ecm.core.schema.SchemaNames;
028import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
029
030/**
031 * The implementation of a Schema
032 */
033public class SchemaImpl extends ComplexTypeImpl implements Schema {
034
035    private static final long serialVersionUID = 1L;
036
037    private final Map<String, Type> types = new HashMap<>();
038
039    public boolean isVersionWritabe;
040
041    /**
042     * Constructor for a schema. Its types (fields) are then added through {@link #registerType}.
043     */
044    public SchemaImpl(String name, Namespace ns) {
045        this(name, ns, false);
046    }
047
048    public SchemaImpl(String name, Namespace ns, boolean isVersionWritabe) {
049        super(null, SchemaNames.SCHEMAS, name, ns == null ? Namespace.DEFAULT_NS : ns);
050        this.isVersionWritabe = isVersionWritabe;
051    }
052
053    /**
054     * Create a schema from a ComplexType
055     *
056     * @since 5.7
057     */
058    public SchemaImpl(ComplexType complexType, String name, Namespace ns, boolean isVersionWritabe) {
059        super(null, SchemaNames.SCHEMAS, name, ns);
060        this.isVersionWritabe = isVersionWritabe;
061        if (complexType != null) {
062            for (Field field : complexType.getFields()) {
063                QName fieldname = QName.valueOf(field.getName().getLocalName(), ns.prefix);
064                Type type = field.getType();
065                String defaultValue = type.encode(field.getDefaultValue());
066                Set<Constraint> constraint = field.getConstraints();
067                FieldImpl newField = new FieldImpl(fieldname, this, type, defaultValue, 0, constraint);
068                newField.setConstant(field.isConstant());
069                addField(newField);
070            }
071        }
072    }
073
074    @Override
075    public Type getType(String typeName) {
076        return types.get(typeName);
077    }
078
079    @Override
080    public Type[] getTypes() {
081        return types.values().toArray(new Type[0]);
082    }
083
084    @Override
085    public void registerType(Type type) {
086        types.put(type.getName(), type);
087    }
088
089    @Override
090    public String toString() {
091        return getClass().getSimpleName() + '(' + name + ')';
092    }
093
094    @Override
095    public Schema getSchema() {
096        return this;
097    }
098
099    @Override
100    public boolean isVersionWritabe() {
101        return isVersionWritabe;
102    }
103
104}