001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * ataillefer 016 */ 017package org.nuxeo.ecm.diff.service; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.nuxeo.ecm.core.api.NuxeoException; 023import org.nuxeo.ecm.core.schema.SchemaManager; 024import org.nuxeo.ecm.core.schema.TypeConstants; 025import org.nuxeo.ecm.core.schema.types.ComplexType; 026import org.nuxeo.ecm.core.schema.types.Field; 027import org.nuxeo.ecm.core.schema.types.ListType; 028import org.nuxeo.ecm.core.schema.types.Schema; 029import org.nuxeo.ecm.core.schema.types.Type; 030import org.nuxeo.ecm.diff.model.PropertyType; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * TODO: refactor as a service + use xpath for fetching doc property values. Helper to get complex property names and 035 * values, and list property values. 036 * 037 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 038 */ 039public final class ComplexPropertyHelper { 040 041 public static Field getField(String schemaName, String fieldName) { 042 043 Schema schema = getSchemaManager().getSchema(schemaName); 044 if (schema == null) { 045 throw new NuxeoException(String.format("Schema [%s] does not exist.", schemaName)); 046 } 047 048 Field field = schema.getField(fieldName); 049 if (field == null) { 050 throw new NuxeoException(String.format("Field [%s] does not exist in schema [%s].", fieldName, schemaName)); 051 } 052 return field; 053 } 054 055 public static Field getComplexFieldItem(Field field, String complexItemName) { 056 057 Type fieldType = field.getType(); 058 if (!fieldType.isComplexType()) { 059 throw new NuxeoException(String.format("Field '%s' is not a complex type.", field)); 060 } 061 062 return ((ComplexType) fieldType).getField(complexItemName); 063 064 } 065 066 public static List<Field> getComplexFieldItems(Field field) { 067 068 Type fieldType = field.getType(); 069 if (!fieldType.isComplexType()) { 070 throw new NuxeoException( 071 String.format("Field [%s] is not a complex type.", field.getName().getLocalName())); 072 } 073 074 return new ArrayList<Field>(((ComplexType) fieldType).getFields()); 075 } 076 077 public static Field getListFieldItem(Field field) { 078 079 Type fieldType = field.getType(); 080 if (!fieldType.isListType()) { 081 throw new NuxeoException(String.format("Field [%s] is not a list type.", field.getName().getLocalName())); 082 } 083 084 Field listFieldItem = ((ListType) fieldType).getField(); 085 if (listFieldItem == null) { 086 throw new NuxeoException(String.format( 087 "Field [%s] is a list type but has no field defining the elements stored by this list.", 088 field.getName().getLocalName())); 089 } 090 091 return listFieldItem; 092 } 093 094 public static String getFieldType(Field field) { 095 096 String fieldTypeName; 097 Type fieldType = field.getType(); 098 099 // Complex type 100 if (fieldType.isComplexType()) { 101 // Content 102 if (TypeConstants.isContentType(fieldType)) { 103 fieldTypeName = PropertyType.CONTENT; 104 } 105 // Complex 106 else { 107 fieldTypeName = PropertyType.COMPLEX; 108 } 109 } 110 // List type 111 else if (fieldType.isListType()) { 112 Field listField = ((ListType) fieldType).getField(); 113 // Complex list 114 if (listField.getType().isComplexType()) { 115 fieldTypeName = PropertyType.COMPLEX_LIST; 116 } 117 // Scalar list 118 else { 119 fieldTypeName = PropertyType.SCALAR_LIST; 120 } 121 } 122 // Scalar type (string, boolean, date, integer, long, double) or content 123 // type. 124 else { 125 fieldTypeName = fieldType.getName(); 126 } 127 128 return fieldTypeName; 129 } 130 131 private static final SchemaManager getSchemaManager() { 132 SchemaManager schemaManager = Framework.getService(SchemaManager.class); 133 if (schemaManager == null) { 134 throw new NuxeoException("SchemaManager service is null."); 135 } 136 return schemaManager; 137 } 138}