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