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;
029    import org.opends.messages.Message;
030    
031    
032    
033    import static org.opends.server.util.Validator.*;
034    
035    import java.util.Collections;
036    import java.util.HashMap;
037    import java.util.Locale;
038    import java.util.Map;
039    import java.util.Set;
040    
041    
042    
043    /**
044     * A managed object composite relationship definition which represents
045     * a composition of zero or more managed objects.
046     *
047     * @param <C>
048     *          The type of client managed object configuration that this
049     *          relation definition refers to.
050     * @param <S>
051     *          The type of server managed object configuration that this
052     *          relation definition refers to.
053     */
054    public final class InstantiableRelationDefinition
055        <C extends ConfigurationClient, S extends Configuration>
056        extends RelationDefinition<C, S> {
057    
058      /**
059       * An interface for incrementally constructing instantiable relation
060       * definitions.
061       *
062       * @param <C>
063       *          The type of client managed object configuration that
064       *          this relation definition refers to.
065       * @param <S>
066       *          The type of server managed object configuration that
067       *          this relation definition refers to.
068       */
069      public static final class Builder
070          <C extends ConfigurationClient, S extends Configuration>
071          extends AbstractBuilder<C, S, InstantiableRelationDefinition<C, S>> {
072    
073        // The optional naming property definition.
074        private PropertyDefinition<?> namingPropertyDefinition = null;
075    
076        // The plural name of the relation.
077        private final String pluralName;
078    
079        // The optional default managed objects associated with this
080        // instantiable relation definition.
081        private final Map<String, DefaultManagedObject<? extends C, ? extends S>>
082          defaultManagedObjects = new HashMap<String,
083            DefaultManagedObject<? extends C, ? extends S>>();
084    
085    
086        /**
087         * Creates a new builder which can be used to incrementally build
088         * an instantiable relation definition.
089         *
090         * @param pd
091         *          The parent managed object definition.
092         * @param name
093         *          The name of the relation.
094         * @param pluralName
095         *          The plural name of the relation.
096         * @param cd
097         *          The child managed object definition.
098         */
099        public Builder(AbstractManagedObjectDefinition<?, ?> pd, String name,
100            String pluralName, AbstractManagedObjectDefinition<C, S> cd) {
101          super(pd, name, cd);
102          this.pluralName = pluralName;
103        }
104    
105    
106    
107        /**
108         * Adds the named default managed object to this instantiable
109         * relation definition.
110         *
111         * @param name
112         *          The name of the default managed object.
113         * @param defaultManagedObject
114         *          The default managed object.
115         */
116        public void setDefaultManagedObject(String name,
117            DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
118          this.defaultManagedObjects.put(name, defaultManagedObject);
119        }
120    
121    
122    
123        /**
124         * Sets the naming property for the instantiable relation
125         * definition.
126         *
127         * @param namingPropertyDefinition
128         *          The property of the child managed object definition
129         *          which should be used for naming, or <code>null</code>
130         *          if this relation does not use a property for naming.
131         */
132        public void setNamingProperty(
133            PropertyDefinition<?> namingPropertyDefinition) {
134          ensureNotNull(namingPropertyDefinition);
135          this.namingPropertyDefinition = namingPropertyDefinition;
136        }
137    
138    
139    
140        /**
141         * {@inheritDoc}
142         */
143        @Override
144        protected InstantiableRelationDefinition<C, S> buildInstance(
145            Common<C, S> common) {
146          return new InstantiableRelationDefinition<C, S>(common, pluralName,
147              namingPropertyDefinition, defaultManagedObjects);
148        }
149    
150      }
151    
152      // The optional naming property definition.
153      private final PropertyDefinition<?> namingPropertyDefinition;
154    
155      // The plural name of the relation.
156      private final String pluralName;
157    
158      // The optional default managed objects associated with this
159      // instantiable relation definition.
160      private final Map<String, DefaultManagedObject<? extends C, ? extends S>>
161        defaultManagedObjects;
162    
163    
164    
165      // Private constructor.
166      private InstantiableRelationDefinition(Common<C, S> common,
167          String pluralName,
168          PropertyDefinition<?> namingPropertyDefinition,
169          Map<String, DefaultManagedObject<? extends C, ? extends S>>
170            defaultManagedObjects) {
171        super(common);
172        this.pluralName = pluralName;
173        this.namingPropertyDefinition = namingPropertyDefinition;
174        this.defaultManagedObjects = defaultManagedObjects;
175      }
176    
177    
178    
179      /**
180       * {@inheritDoc}
181       */
182      @Override
183      public <R, P> R accept(RelationDefinitionVisitor<R, P> v, P p) {
184        return v.visitInstantiable(this, p);
185      }
186    
187    
188    
189      /**
190       * Gets the named default managed object associated with this
191       * instantiable relation definition.
192       *
193       * @param name
194       *          The name of the default managed object.
195       * @return Returns the named default managed object.
196       * @throws IllegalArgumentException
197       *           If there is no default managed object associated with
198       *           the provided name.
199       */
200      public DefaultManagedObject<? extends C, ? extends S> getDefaultManagedObject(
201          String name) throws IllegalArgumentException {
202        if (!defaultManagedObjects.containsKey(name)) {
203          throw new IllegalArgumentException(
204              "unrecognized default managed object \"" + name + "\"");
205        }
206        return defaultManagedObjects.get(name);
207      }
208    
209    
210    
211      /**
212       * Gets the names of the default managed objects associated with
213       * this instantiable relation definition.
214       *
215       * @return Returns an unmodifiable set containing the names of the
216       *         default managed object.
217       */
218      public Set<String> getDefaultManagedObjectNames() {
219        return Collections.unmodifiableSet(defaultManagedObjects.keySet());
220      }
221    
222    
223    
224      /**
225       * Get the property of the child managed object definition which
226       * should be used for naming children.
227       *
228       * @return Returns the property of the child managed object
229       *         definition which should be used for naming, or
230       *         <code>null</code> if this relation does not use a
231       *         property for naming.
232       */
233      public PropertyDefinition<?> getNamingPropertyDefinition() {
234        return namingPropertyDefinition;
235      }
236    
237    
238    
239      /**
240       * Get the plural name of the relation.
241       *
242       * @return Returns the plural name of the relation.
243       */
244      public String getPluralName() {
245        return pluralName;
246      }
247    
248    
249    
250      /**
251       * Gets the user friendly plural name of this relation definition in
252       * the default locale.
253       *
254       * @return Returns the user friendly plural name of this relation
255       *         definition in the default locale.
256       */
257      public Message getUserFriendlyPluralName() {
258        return getUserFriendlyPluralName(Locale.getDefault());
259      }
260    
261    
262    
263      /**
264       * Gets the user friendly plural name of this relation definition in
265       * the specified locale.
266       *
267       * @param locale
268       *          The locale.
269       * @return Returns the user friendly plural name of this relation
270       *         definition in the specified locale.
271       */
272      public Message getUserFriendlyPluralName(Locale locale) {
273        String property = "relation." + getName() + ".user-friendly-plural-name";
274        return ManagedObjectDefinitionI18NResource.getInstance().getMessage(
275            getParentDefinition(), property, locale);
276      }
277    
278    
279    
280      /**
281       * {@inheritDoc}
282       */
283      @Override
284      public void toString(StringBuilder builder) {
285        builder.append("name=");
286        builder.append(getName());
287        builder.append(" type=composition parent=");
288        builder.append(getParentDefinition().getName());
289        builder.append(" child=");
290        builder.append(getChildDefinition().getName());
291        builder.append(" child=");
292        builder.append(getChildDefinition().getName());
293        builder.append(" minOccurs=0");
294      }
295    
296    
297    
298      /**
299       * {@inheritDoc}
300       */
301      @Override
302      protected void initialize() throws Exception {
303        for (DefaultManagedObject<?, ?> dmo : defaultManagedObjects.values()) {
304          dmo.initialize();
305        }
306      }
307    }