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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.admin.client;
029    
030    
031    
032    import static org.opends.messages.AdminMessages.*;
033    
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.Collections;
037    
038    import org.opends.messages.Message;
039    import org.opends.messages.MessageBuilder;
040    import org.opends.server.admin.OperationsException;
041    import org.opends.server.admin.PropertyIsMandatoryException;
042    import org.opends.server.util.Validator;
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     */
051    public 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 Message createMessage(
062          Collection<PropertyIsMandatoryException> causes) {
063        Validator.ensureNotNull(causes);
064        Validator.ensureTrue(!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          MessageBuilder builder = new MessageBuilder();
071    
072          boolean isFirst = true;
073          for (PropertyIsMandatoryException 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<PropertyIsMandatoryException> causes;
088    
089      // Indicates whether the exception occurred during managed object
090      // creation.
091      private final boolean isCreate;
092    
093      // The user friendly name of the component that caused this
094      // exception.
095      private final Message ufn;
096    
097    
098    
099      /**
100       * Creates a new missing mandatory properties exception with the
101       * provided causes.
102       *
103       * @param ufn
104       *          The user friendly name of the component that caused this
105       *          exception.
106       * @param causes
107       *          The causes of this exception (must be non-<code>null</code>
108       *          and non-empty).
109       * @param isCreate
110       *          Indicates whether the exception occurred during managed
111       *          object creation.
112       */
113      public MissingMandatoryPropertiesException(Message ufn,
114          Collection<PropertyIsMandatoryException> causes, boolean isCreate) {
115        super(createMessage(causes));
116    
117        this.causes = new ArrayList<PropertyIsMandatoryException>(causes);
118        this.ufn = ufn;
119        this.isCreate = isCreate;
120      }
121    
122    
123    
124      /**
125       * Gets the first exception that caused this exception.
126       *
127       * @return Returns the first exception that caused this exception.
128       */
129      @Override
130      public PropertyIsMandatoryException getCause() {
131        return causes.iterator().next();
132      }
133    
134    
135    
136      /**
137       * Gets an unmodifiable collection view of the causes of this
138       * exception.
139       *
140       * @return Returns an unmodifiable collection view of the causes of
141       *         this exception.
142       */
143      public Collection<PropertyIsMandatoryException> getCauses() {
144        return Collections.unmodifiableCollection(causes);
145      }
146    
147    
148    
149      /**
150       * Gets the user friendly name of the component that caused this
151       * exception.
152       *
153       * @return Returns the user friendly name of the component that
154       *         caused this exception.
155       */
156      public Message getUserFriendlyName() {
157        return ufn;
158      }
159    
160    
161    
162      /**
163       * Indicates whether or not this exception was thrown during managed
164       * object creation or during modification.
165       *
166       * @return Returns <code>true</code> if this exception was thrown
167       *         during managed object creation.
168       */
169      public boolean isCreate() {
170        return isCreate;
171      }
172    
173    }