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