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.ArrayList;
023import java.util.Collection;
024import java.util.Collections;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029import org.nuxeo.ecm.core.schema.SchemaManager;
030import org.nuxeo.ecm.core.schema.types.constraints.Constraint;
031import org.nuxeo.ecm.core.schema.types.resolver.ObjectResolver;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Abstract (common) implementation for a Type.
036 */
037public abstract class AbstractType implements Type {
038
039    private static final long serialVersionUID = 1L;
040
041    public static final Type[] EMPTY_SUPERTYPES = new Type[0];
042
043    protected final String name;
044
045    protected final String schema;
046
047    protected final Type superType;
048
049    protected Set<Constraint> constraints;
050
051    protected AbstractType(Type superType, String schema, String name) {
052        this.name = name;
053        this.schema = schema;
054        this.superType = superType;
055        constraints = new HashSet<Constraint>();
056    }
057
058    @Override
059    public Type getSuperType() {
060        return superType;
061    }
062
063    @Override
064    public String getName() {
065        return name;
066    }
067
068    @Override
069    public String getSchemaName() {
070        return schema;
071    }
072
073    @Override
074    public Schema getSchema() {
075        return Framework.getLocalService(SchemaManager.class).getSchema(schema);
076    }
077
078    @Override
079    public boolean isSuperTypeOf(Type type) {
080        Type t = type;
081        do {
082            if (this == t) {
083                return true;
084            }
085            t = t.getSuperType();
086        } while (t != null);
087        return false;
088    }
089
090    public boolean isAny() {
091        return false;
092    }
093
094    @Override
095    public Type[] getTypeHierarchy() {
096        Type type = getSuperType();
097        if (type == null) {
098            return EMPTY_SUPERTYPES;
099        }
100        List<Type> types = new ArrayList<Type>();
101        while (type != null) {
102            types.add(type);
103            type = type.getSuperType();
104        }
105        return types.toArray(new Type[types.size()]);
106    }
107
108    @Override
109    public boolean isSimpleType() {
110        return false;
111    }
112
113    @Override
114    public boolean isComplexType() {
115        return false;
116    }
117
118    @Override
119    public boolean isListType() {
120        return false;
121    }
122
123    @Override
124    public boolean isAnyType() {
125        return false;
126    }
127
128    @Override
129    public boolean isCompositeType() {
130        return false;
131    }
132
133    @Override
134    public boolean validate(Object object) throws TypeException {
135        return true;
136    }
137
138    @Override
139    public Object decode(String string) {
140        return null;
141    }
142
143    @Override
144    public String encode(Object object) {
145        return null;
146    }
147
148    @Override
149    public Object newInstance() {
150        return null;
151    }
152
153    @Override
154    public Set<Constraint> getConstraints() {
155        Set<Constraint> constraints = new HashSet<Constraint>();
156        if (getSuperType() instanceof SimpleType) {
157            SimpleType superType = (SimpleType) getSuperType();
158            constraints.addAll(superType.getConstraints());
159        }
160        constraints.addAll(this.constraints);
161        return Collections.unmodifiableSet(constraints);
162    }
163
164    public void addConstraints(Collection<Constraint> constraints) {
165        this.constraints.addAll(constraints);
166    }
167
168    protected boolean validateConstraints(Object object) {
169        if (constraints != null) {
170            for (Constraint constraint : constraints) {
171                if (!constraint.validate(object)) {
172                    return false;
173                }
174            }
175        }
176        return true;
177    }
178
179    @Override
180    public ObjectResolver getObjectResolver() {
181        return null;
182    }
183
184}