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