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