001/*
002 * (C) Copyright 2014 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-2.1.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 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.core.api.validation;
019
020import java.io.Serializable;
021import java.util.Map;
022
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.model.Property;
025import org.nuxeo.ecm.core.schema.types.Field;
026import org.nuxeo.ecm.core.schema.types.Schema;
027
028/**
029 * Provides a way to validate {@link DocumentModel} according to their {@link Schema}'s constraints.
030 *
031 * @since 7.1
032 */
033public interface DocumentValidationService {
034
035    public static final String CTX_MAP_KEY = "DocumentValidationService.Forcing";
036
037    public static final String CTX_CREATEDOC = "createDocument";
038
039    public static final String CTX_SAVEDOC = "saveDocument";
040
041    public static final String CTX_IMPORTDOC = "importDocument";
042
043    public static enum Forcing {
044        TURN_ON, TURN_OFF, USUAL;
045    }
046
047    /**
048     * To activate validation in some context, for example "CoreSession.saveDocument", you have to contribute to
049     * component "org.nuxeo.ecm.core.api.DocumentValidationService" with extension point "activations".
050     * <p>
051     * Example :
052     * </p>
053     *
054     * <pre>
055     * {@code
056     * <extension target="org.nuxeo.ecm.core.api.DocumentValidationService" point="activations">
057     *   <validation context="CoreSession.saveDocument" activated="false" />
058     * </extension>
059     * }
060     * </pre>
061     * <p>
062     * Here are some available context :
063     * </p>
064     * <ul>
065     * <li>CoreSession.createDocument</li>
066     * <li>CoreSession.saveDocument</li>
067     * <li>CoreSession.importDocument</li>
068     * </ul>
069     *
070     * @param context A string representation of the context, where validation service should be activated.
071     * @param contextMap if not null, search forcing flag in the context map. see {@link Forcing} for values and
072     *            {@link #CTX_MAP_KEY} for the key.
073     * @return true if validation is activated in the specified context, false otherwise.
074     * @since 7.1
075     */
076    boolean isActivated(String context, Map<String, Serializable> contextMap);
077
078    /**
079     * Validates all {@link Field} of all {@link Schema} of a {@link DocumentModel}. Including no dirty properties.
080     *
081     * @since 7.1
082     */
083    DocumentValidationReport validate(DocumentModel document);
084
085    /**
086     * Validates all {@link Field} of all {@link Schema} of a {@link DocumentModel}.
087     *
088     * @param dirtyOnly If true, limit validation to dirty properties of the {@link DocumentModel}.
089     * @since 7.1
090     */
091    DocumentValidationReport validate(DocumentModel document, boolean dirtyOnly);
092
093    /**
094     * Validates a value according to some {@link Field} definition.
095     *
096     * @since 7.1
097     */
098    DocumentValidationReport validate(Field field, Object value);
099
100    /**
101     * Validates a value according to some {@link Field} definition.
102     *
103     * @param validateSubProperties Tell whether the sub properties must be validated.
104     * @since 7.2
105     */
106    DocumentValidationReport validate(Field field, Object value, boolean validateSubProperties);
107
108    /**
109     * Validates a property according to its {@link Field} definition.
110     *
111     * @since 7.1
112     */
113    DocumentValidationReport validate(Property property);
114
115    /**
116     * Validates a property according to its {@link Field} definition.
117     *
118     * @param validateSubProperties Tell whether the sub properties must be validated.
119     * @since 7.10
120     */
121    DocumentValidationReport validate(Property property, boolean validateSubProperties);
122
123    /**
124     * Validates a value according to some {@link Field} definition.
125     *
126     * @param xpath schema:fieldName, for example dc:title - the xpath could also be a value that match a complex
127     *            property field (for example, an field of a complex type in a list: schema:list:complex:field).
128     * @throws IllegalArgumentException If the xpath does not match any field.
129     * @since 7.1
130     */
131    DocumentValidationReport validate(String xpath, Object value);
132
133    /**
134     * Validates a value according to some {@link Field} definition.
135     *
136     * @param xpath schema:fieldName, for example dc:title - the xpath could also be a value that match a complex
137     *            property field (for example, an field of a complex type in a list: schema:list:complex:field).
138     * @param validateSubProperties Tell whether the sub properties must be validated.
139     * @throws IllegalArgumentException If the xpath does not match any field.
140     * @since 7.10
141     */
142    DocumentValidationReport validate(String xpath, Object value, boolean validateSubProperties);
143
144}