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