001/*
002 * (C) Copyright 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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.ecm.diff.service;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.schema.SchemaManager;
026import org.nuxeo.ecm.core.schema.TypeConstants;
027import org.nuxeo.ecm.core.schema.types.ComplexType;
028import org.nuxeo.ecm.core.schema.types.Field;
029import org.nuxeo.ecm.core.schema.types.ListType;
030import org.nuxeo.ecm.core.schema.types.Schema;
031import org.nuxeo.ecm.core.schema.types.SimpleType;
032import org.nuxeo.ecm.core.schema.types.Type;
033import org.nuxeo.ecm.diff.model.PropertyType;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Helper to get complex property names and values, and list property values.
038 * <p>
039 * TODO: refactor as a service + use xpath for fetching doc property values.
040 */
041public final class ComplexPropertyHelper {
042
043    private ComplexPropertyHelper() {
044        // helper class
045    }
046
047    public static Field getField(String schemaName, String fieldName) {
048
049        Schema schema = getSchemaManager().getSchema(schemaName);
050        if (schema == null) {
051            throw new NuxeoException(String.format("Schema [%s] does not exist.", schemaName));
052        }
053
054        Field field = schema.getField(fieldName);
055        if (field == null) {
056            throw new NuxeoException(String.format("Field [%s] does not exist in schema [%s].", fieldName, schemaName));
057        }
058        return field;
059    }
060
061    public static Field getComplexFieldItem(Field field, String complexItemName) {
062
063        Type fieldType = field.getType();
064        if (!fieldType.isComplexType()) {
065            throw new NuxeoException(String.format("Field '%s' is not a complex type.", field));
066        }
067
068        return ((ComplexType) fieldType).getField(complexItemName);
069
070    }
071
072    public static List<Field> getComplexFieldItems(Field field) {
073
074        Type fieldType = field.getType();
075        if (!fieldType.isComplexType()) {
076            throw new NuxeoException(
077                    String.format("Field [%s] is not a complex type.", field.getName().getLocalName()));
078        }
079
080        return new ArrayList<>(((ComplexType) fieldType).getFields());
081    }
082
083    public static Field getListFieldItem(Field field) {
084
085        Type fieldType = field.getType();
086        if (!fieldType.isListType()) {
087            throw new NuxeoException(String.format("Field [%s] is not a list type.", field.getName().getLocalName()));
088        }
089
090        Field listFieldItem = ((ListType) fieldType).getField();
091        if (listFieldItem == null) {
092            throw new NuxeoException(String.format(
093                    "Field [%s] is a list type but has no field defining the elements stored by this list.",
094                    field.getName().getLocalName()));
095        }
096
097        return listFieldItem;
098    }
099
100    public static String getFieldType(Field field) {
101
102        String fieldTypeName;
103        Type fieldType = field.getType();
104
105        // Complex type
106        if (fieldType.isComplexType()) {
107            // Content
108            if (TypeConstants.isContentType(fieldType)) {
109                fieldTypeName = PropertyType.CONTENT;
110            }
111            // Complex
112            else {
113                fieldTypeName = PropertyType.COMPLEX;
114            }
115        }
116        // List type
117        else if (fieldType.isListType()) {
118            Field listField = ((ListType) fieldType).getField();
119            // Complex list
120            if (listField.getType().isComplexType()) {
121                fieldTypeName = PropertyType.COMPLEX_LIST;
122            }
123            // Scalar list
124            else {
125                fieldTypeName = PropertyType.SCALAR_LIST;
126            }
127        }
128        // Scalar type (string, boolean, date, integer, long, double)
129        else if (fieldType.isSimpleType()) {
130            fieldTypeName = ((SimpleType) fieldType).getPrimitiveType().getName();
131        }
132        // Fallback, should never happen
133        else {
134            fieldTypeName = fieldType.getName();
135        }
136
137        return fieldTypeName;
138    }
139
140    private static final SchemaManager getSchemaManager() {
141        SchemaManager schemaManager = Framework.getService(SchemaManager.class);
142        if (schemaManager == null) {
143            throw new NuxeoException("SchemaManager service is null.");
144        }
145        return schemaManager;
146    }
147}