001/* 002 * (C) Copyright 2014 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 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.api.model.resolver; 021 022import java.io.Serializable; 023import java.util.List; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.schema.SchemaManager; 027import org.nuxeo.ecm.core.schema.types.Field; 028import org.nuxeo.ecm.core.schema.types.resolver.ObjectResolver; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * @since 7.1 033 */ 034public class DocumentPropertyObjectResolverImpl implements PropertyObjectResolver { 035 036 protected DocumentModel doc; 037 038 protected String xpath; 039 040 protected ObjectResolver resolver; 041 042 public static DocumentPropertyObjectResolverImpl create(DocumentModel doc, String xpath) { 043 Field field = Framework.getService(SchemaManager.class).getField(xpath); 044 if (field != null) { 045 ObjectResolver resolver = field.getType().getObjectResolver(); 046 if (resolver != null) { 047 return new DocumentPropertyObjectResolverImpl(doc, xpath, resolver); 048 } 049 } 050 return null; 051 } 052 053 public DocumentPropertyObjectResolverImpl(DocumentModel doc, String xpath, ObjectResolver resolver) { 054 this.doc = doc; 055 this.xpath = xpath; 056 this.resolver = resolver; 057 } 058 059 @Override 060 public List<Class<?>> getManagedClasses() { 061 return resolver.getManagedClasses(); 062 } 063 064 @Override 065 public boolean validate() { 066 return resolver.validate(doc.getPropertyValue(xpath)); 067 } 068 069 @Override 070 public Object fetch() { 071 return resolver.fetch(doc.getPropertyValue(xpath)); 072 } 073 074 @Override 075 public <T> T fetch(Class<T> type) { 076 return resolver.fetch(type, doc.getPropertyValue(xpath)); 077 } 078 079 @Override 080 public void setObject(Object object) { 081 Serializable reference = resolver.getReference(object); 082 doc.setPropertyValue(xpath, reference); 083 } 084 085 @Override 086 public ObjectResolver getObjectResolver() { 087 return resolver; 088 } 089 090}