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