001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2015-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config.client;
018
019import static com.forgerock.opendj.ldap.config.ConfigMessages.*;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024
025import org.forgerock.i18n.LocalizableMessage;
026import org.forgerock.i18n.LocalizableMessageBuilder;
027import org.forgerock.opendj.config.OperationsException;
028import org.forgerock.opendj.config.PropertyException;
029import org.forgerock.util.Reject;
030
031/**
032 * This exception is thrown when an attempt is made to add or modify a managed
033 * object when one or more of its mandatory properties are undefined.
034 */
035public class MissingMandatoryPropertiesException extends OperationsException {
036
037    /** Serialization ID. */
038    private static final long serialVersionUID = 6342522125252055588L;
039
040    /** Create the message. */
041    private static LocalizableMessage createMessage(Collection<PropertyException> causes) {
042        Reject.ifNull(causes);
043        Reject.ifFalse(!causes.isEmpty(), "causes should not be empty");
044
045        if (causes.size() == 1) {
046            return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_SINGLE.get(causes.iterator().next()
047                .getPropertyDefinition().getName());
048        } else {
049            LocalizableMessageBuilder builder = new LocalizableMessageBuilder();
050
051            boolean isFirst = true;
052            for (PropertyException cause : causes) {
053                if (!isFirst) {
054                    builder.append(", ");
055                }
056                builder.append(cause.getPropertyDefinition().getName());
057                isFirst = false;
058            }
059
060            return ERR_MISSING_MANDATORY_PROPERTIES_EXCEPTION_PLURAL.get(builder.toMessage());
061        }
062    }
063
064    /** The causes of this exception. */
065    private final Collection<PropertyException> causes;
066
067    /** Indicates whether the exception occurred during managed object creation. */
068    private final boolean isCreate;
069
070    /** The user friendly name of the component that caused this exception. */
071    private final LocalizableMessage ufn;
072
073    /**
074     * Creates a new missing mandatory properties exception with the provided
075     * causes.
076     *
077     * @param ufn
078     *            The user friendly name of the component that caused this
079     *            exception.
080     * @param causes
081     *            The causes of this exception (must be non-<code>null</code>
082     *            and non-empty).
083     * @param isCreate
084     *            Indicates whether the exception occurred during managed object
085     *            creation.
086     */
087    public MissingMandatoryPropertiesException(LocalizableMessage ufn,
088        Collection<PropertyException> causes, boolean isCreate) {
089        super(createMessage(causes));
090
091        this.causes = new ArrayList<>(causes);
092        this.ufn = ufn;
093        this.isCreate = isCreate;
094    }
095
096    /**
097     * Gets the first exception that caused this exception.
098     *
099     * @return Returns the first exception that caused this exception.
100     */
101    @Override
102    public PropertyException getCause() {
103        return causes.iterator().next();
104    }
105
106    /**
107     * Gets an unmodifiable collection view of the causes of this exception.
108     *
109     * @return Returns an unmodifiable collection view of the causes of this
110     *         exception.
111     */
112    public Collection<PropertyException> getCauses() {
113        return Collections.unmodifiableCollection(causes);
114    }
115
116    /**
117     * Gets the user friendly name of the component that caused this exception.
118     *
119     * @return Returns the user friendly name of the component that caused this
120     *         exception.
121     */
122    public LocalizableMessage getUserFriendlyName() {
123        return ufn;
124    }
125
126    /**
127     * Indicates whether this exception was thrown during managed object
128     * creation or during modification.
129     *
130     * @return Returns <code>true</code> if this exception was thrown during
131     *         managed object creation.
132     */
133    public boolean isCreate() {
134        return isCreate;
135    }
136
137}