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.validation;
021
022import org.nuxeo.ecm.core.api.NuxeoException;
023
024/**
025 * Exception thrown when some process failed due to {@link ConstraintViolation}.
026 *
027 * @since 7.1
028 */
029public class DocumentValidationException extends NuxeoException {
030
031    private static final String MESSAGE_SINGLE = "Constraint violation thrown: '%s'";
032
033    private static final String MESSAGE = "%s constraint violation(s) thrown. First one is: '%s', call "
034            + DocumentValidationException.class.getSimpleName() + ".getViolations() to get the others";
035
036    private static final long serialVersionUID = 1L;
037
038    private DocumentValidationReport report;
039
040    public DocumentValidationException(DocumentValidationReport report) {
041        super();
042        this.report = report;
043    }
044
045    public DocumentValidationReport getReport() {
046        return report;
047    }
048
049    @Override
050    public String getMessage() {
051        if (report.hasError()) {
052            int num = report.numberOfErrors();
053            String violationMessage = report.asList().get(0).getMessage(null);
054            if (num > 1) {
055                return String.format(MESSAGE, report.numberOfErrors(), violationMessage);
056            } else {
057                return String.format(MESSAGE_SINGLE, violationMessage);
058            }
059        } else {
060            return super.getMessage();
061        }
062    }
063
064}