001/*
002 * (C) Copyright 2006-2014 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.api.model.impl;
021
022import org.nuxeo.ecm.core.api.model.Property;
023import org.nuxeo.ecm.core.api.model.impl.primitives.BinaryProperty;
024import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
025import org.nuxeo.ecm.core.api.model.impl.primitives.BooleanProperty;
026import org.nuxeo.ecm.core.api.model.impl.primitives.DateProperty;
027import org.nuxeo.ecm.core.api.model.impl.primitives.DoubleProperty;
028import org.nuxeo.ecm.core.api.model.impl.primitives.ExternalBlobProperty;
029import org.nuxeo.ecm.core.api.model.impl.primitives.LongProperty;
030import org.nuxeo.ecm.core.api.model.impl.primitives.StringProperty;
031import org.nuxeo.ecm.core.schema.TypeConstants;
032import org.nuxeo.ecm.core.schema.types.Field;
033import org.nuxeo.ecm.core.schema.types.ListType;
034import org.nuxeo.ecm.core.schema.types.SimpleTypeImpl;
035import org.nuxeo.ecm.core.schema.types.Type;
036import org.nuxeo.ecm.core.schema.types.primitives.BinaryType;
037import org.nuxeo.ecm.core.schema.types.primitives.BooleanType;
038import org.nuxeo.ecm.core.schema.types.primitives.DateType;
039import org.nuxeo.ecm.core.schema.types.primitives.DoubleType;
040import org.nuxeo.ecm.core.schema.types.primitives.IntegerType;
041import org.nuxeo.ecm.core.schema.types.primitives.LongType;
042import org.nuxeo.ecm.core.schema.types.primitives.StringType;
043
044/**
045 * The default property factory singleton.
046 */
047public class PropertyFactory {
048
049    private PropertyFactory() {
050        // utility class
051    }
052
053    public static Property createProperty(Property parent, Field field, int flags) {
054        Type type = field.getType();
055        if (type instanceof SimpleTypeImpl) {
056            // type with constraint
057            type = type.getSuperType();
058        }
059        switch (type.getName()) {
060        case StringType.ID:
061            return new StringProperty(parent, field, flags);
062        case IntegerType.ID:
063        case LongType.ID:
064            return new LongProperty(parent, field, flags);
065        case DoubleType.ID:
066            return new DoubleProperty(parent, field, flags);
067        case BooleanType.ID:
068            return new BooleanProperty(parent, field, flags);
069        case DateType.ID:
070            return new DateProperty(parent, field, flags);
071        case BinaryType.ID:
072            return new BinaryProperty(parent, field, flags);
073        case TypeConstants.CONTENT:
074            return new BlobProperty(parent, field, flags);
075        case TypeConstants.EXTERNAL_CONTENT:
076            return new ExternalBlobProperty(parent, field, flags);
077        }
078        if (type.isSimpleType()) {
079            return new ScalarProperty(parent, field, flags);
080        } else if (type.isComplexType()) {
081            return new MapProperty(parent, field, flags);
082        } else if (type.isListType()) {
083            if (((ListType) type).isArray()) {
084                return new ArrayProperty(parent, field, flags);
085            } else {
086                return new ListProperty(parent, field, flags);
087            }
088        } else {
089            throw new IllegalArgumentException("Unsupported field type: " + field.getType().getName());
090        }
091    }
092
093}