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