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 * mcedica 016 */ 017package org.nuxeo.ecm.core.api.propertiesmapping.impl; 018 019import java.util.Map; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.api.CoreSession; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.NuxeoException; 026import org.nuxeo.ecm.core.api.model.Property; 027import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 028import org.nuxeo.ecm.core.api.propertiesmapping.PropertiesMappingContributionRegistry; 029import org.nuxeo.ecm.core.api.propertiesmapping.PropertiesMappingDescriptor; 030import org.nuxeo.ecm.core.api.propertiesmapping.PropertiesMappingService; 031import org.nuxeo.ecm.core.schema.types.ComplexType; 032import org.nuxeo.ecm.core.schema.types.Field; 033import org.nuxeo.ecm.core.schema.types.ListType; 034import org.nuxeo.ecm.core.schema.types.Type; 035import org.nuxeo.runtime.model.ComponentInstance; 036import org.nuxeo.runtime.model.DefaultComponent; 037 038/** 039 * Service that allows to copy a set of metadata from a source to a target document 040 * 041 * @since 5.6 042 */ 043public class PropertiesMappingComponent extends DefaultComponent implements PropertiesMappingService { 044 045 public static final Log log = LogFactory.getLog(PropertiesMappingComponent.class); 046 047 public static final String MAPPING_EP = "mapping"; 048 049 protected PropertiesMappingContributionRegistry mappingsRegistry = new PropertiesMappingContributionRegistry(); 050 051 @Override 052 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 053 if (MAPPING_EP.equals(extensionPoint)) { 054 PropertiesMappingDescriptor desc = (PropertiesMappingDescriptor) contribution; 055 mappingsRegistry.addContribution(desc); 056 } 057 } 058 059 @Override 060 public Map<String, String> getMapping(String mappingName) { 061 return mappingsRegistry.getMappingProperties(mappingName); 062 } 063 064 @Override 065 public void mapProperties(CoreSession session, DocumentModel sourceDoc, DocumentModel targetDoc, String mapping) 066 { 067 Map<String, String> properties = getMapping(mapping); 068 for (String keyProp : properties.keySet()) { 069 // verify that mapping can be done 070 Property sourceProperty = sourceDoc.getProperty(keyProp); 071 Property targetProperty = targetDoc.getProperty(properties.get(keyProp)); 072 073 Type sourceType = sourceProperty.getType(); 074 Type targetType = targetProperty.getType(); 075 076 if (!compatibleTypes(targetType, sourceType)) { 077 throw new NuxeoException( 078 String.format("Invalid mapping. Cannot map %s on type %s ", sourceType, targetType)); 079 } 080 081 targetDoc.setPropertyValue(targetProperty.getPath(), sourceProperty.getValue()); 082 } 083 session.saveDocument(targetDoc); 084 } 085 086 protected boolean compatibleTypes(Type targetType, Type sourceType) { 087 if (!sourceType.getName().equals(targetType.getName())) { 088 return false; 089 } 090 if (sourceType.isComplexType()) { 091 for (Field field : ((ComplexType) sourceType).getFields()) { 092 Field targetField = ((ComplexType) targetType).getField(field.getName()); 093 if (targetField == null || !field.getType().equals(targetField.getType())) { 094 return false; 095 } 096 } 097 } 098 if (sourceType.isListType()) { 099 if (!((ListType) sourceType).getFieldType().equals(((ListType) targetType).getFieldType())) { 100 return false; 101 } 102 if (((ListType) sourceType).getFieldType().isComplexType()) { 103 return compatibleTypes(((ListType) targetType).getFieldType(), ((ListType) sourceType).getFieldType()); 104 } 105 } 106 return true; 107 } 108}