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.schema.types.constraints;
021
022import java.io.Serializable;
023import java.util.Collections;
024import java.util.Locale;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.schema.types.resolver.ObjectResolver;
028
029/**
030 * External references are document field with a simple type whose value refers to an external business entity. This
031 * constraints ensure some value is a reference of an existing external entity resolved by the underlying resolver :
032 * {@link #getResolver()} .
033 *
034 * @since 7.1
035 */
036public final class ObjectResolverConstraint extends AbstractConstraint {
037
038    private static final long serialVersionUID = 1L;
039
040    private ObjectResolver resolver;
041
042    public ObjectResolverConstraint(ObjectResolver resolver) {
043        super();
044        this.resolver = resolver;
045    }
046
047    public ObjectResolver getResolver() {
048        return resolver;
049    }
050
051    @Override
052    public boolean validate(Object object) {
053        if (object == null) {
054            return true;
055        }
056        return resolver.validate(object);
057    }
058
059    @Override
060    public Description getDescription() {
061        Map<String, Serializable> parameters = Collections.unmodifiableMap(resolver.getParameters());
062        return new Description(resolver.getName(), parameters);
063    }
064
065    @Override
066    public String getErrorMessage(Object invalidValue, Locale locale) {
067        return resolver.getConstraintErrorMessage(invalidValue, locale);
068    }
069}