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), doc.getCoreSession());
067    }
068
069    @Override
070    public boolean validate(Object context) {
071        return resolver.validate(doc.getPropertyValue(xpath), context);
072    }
073
074    @Override
075    public Object fetch() {
076        return resolver.fetch(doc.getPropertyValue(xpath), doc.getCoreSession());
077    }
078
079    @Override
080    public Object fetch(Object context) {
081        return resolver.fetch(doc.getPropertyValue(xpath), context);
082    }
083
084    @Override
085    public <T> T fetch(Class<T> type) {
086        return resolver.fetch(type, doc.getPropertyValue(xpath));
087    }
088
089    @Override
090    public void setObject(Object object) {
091        Serializable reference = resolver.getReference(object);
092        doc.setPropertyValue(xpath, reference);
093    }
094
095    @Override
096    public ObjectResolver getObjectResolver() {
097        return resolver;
098    }
099
100}