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