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-2016 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        /** The optional default managed objects associated with this set relation definition. */
060        private final Map<String, DefaultManagedObject<? extends C, ? extends S>> defaultManagedObjects =
061            new HashMap<>();
062
063        /**
064         * Creates a new builder which can be used to incrementally build a set
065         * relation definition.
066         *
067         * @param pd
068         *            The parent managed object definition.
069         * @param name
070         *            The name of the relation.
071         * @param pluralName
072         *            The plural name of the relation.
073         * @param cd
074         *            The child managed object definition.
075         */
076        public Builder(AbstractManagedObjectDefinition<?, ?> pd, String name, String pluralName,
077            AbstractManagedObjectDefinition<C, S> cd) {
078            super(pd, name, cd);
079            this.pluralName = pluralName;
080        }
081
082        /**
083         * Adds the default managed object to this set relation definition.
084         *
085         * @param defaultManagedObject
086         *            The default managed object.
087         */
088        public void setDefaultManagedObject(DefaultManagedObject<? extends C, ? extends S> defaultManagedObject) {
089            this.defaultManagedObjects.put(defaultManagedObject.getManagedObjectDefinition().getName(),
090                defaultManagedObject);
091        }
092
093        @Override
094        protected SetRelationDefinition<C, S> buildInstance(Common<C, S> common) {
095            return new SetRelationDefinition<>(common, pluralName, defaultManagedObjects);
096        }
097
098    }
099
100    /** The plural name of the relation. */
101    private final String pluralName;
102
103    /** The optional default managed objects associated with this set relation definition. */
104    private final Map<String, DefaultManagedObject<? extends C, ? extends S>> defaultManagedObjects;
105
106    /** Private constructor. */
107    private SetRelationDefinition(Common<C, S> common, String pluralName,
108        Map<String, DefaultManagedObject<? extends C, ? extends S>> defaultManagedObjects) {
109        super(common);
110        this.pluralName = pluralName;
111        this.defaultManagedObjects = defaultManagedObjects;
112    }
113
114    @Override
115    public <R, P> R accept(RelationDefinitionVisitor<R, P> v, P p) {
116        return v.visitSet(this, p);
117    }
118
119    /**
120     * Gets the named default managed object associated with this set relation
121     * definition.
122     *
123     * @param name
124     *            The name of the default managed object (for set relations this
125     *            is the type of the default managed object).
126     * @return The named default managed object.
127     * @throws IllegalArgumentException
128     *             If there is no default managed object associated with the
129     *             provided name.
130     */
131    public DefaultManagedObject<? extends C, ? extends S> getDefaultManagedObject(String name) {
132        if (!defaultManagedObjects.containsKey(name)) {
133            throw new IllegalArgumentException("unrecognized default managed object \"" + name + "\"");
134        }
135        return defaultManagedObjects.get(name);
136    }
137
138    /**
139     * Gets the names of the default managed objects associated with this set
140     * relation definition.
141     *
142     * @return An unmodifiable set containing the names of the default managed
143     *         object.
144     */
145    public Set<String> getDefaultManagedObjectNames() {
146        return Collections.unmodifiableSet(defaultManagedObjects.keySet());
147    }
148
149    /**
150     * Gets the plural name of the relation.
151     *
152     * @return The plural name of the relation.
153     */
154    public String getPluralName() {
155        return pluralName;
156    }
157
158    /**
159     * Gets the user friendly plural name of this relation definition in the
160     * default locale.
161     *
162     * @return Returns the user friendly plural name of this relation definition
163     *         in the default locale.
164     */
165    public LocalizableMessage getUserFriendlyPluralName() {
166        return getUserFriendlyPluralName(Locale.getDefault());
167    }
168
169    /**
170     * Gets the user friendly plural name of this relation definition in the
171     * specified locale.
172     *
173     * @param locale
174     *            The locale.
175     * @return Returns the user friendly plural name of this relation definition
176     *         in the specified locale.
177     */
178    public LocalizableMessage getUserFriendlyPluralName(Locale locale) {
179        String property = "relation." + getName() + ".user-friendly-plural-name";
180        return ManagedObjectDefinitionI18NResource.getInstance().getMessage(getParentDefinition(), property, locale);
181    }
182
183    @Override
184    public void toString(StringBuilder builder) {
185        builder.append("name=");
186        builder.append(getName());
187        builder.append(" type=set parent=");
188        builder.append(getParentDefinition().getName());
189        builder.append(" child=");
190        builder.append(getChildDefinition().getName());
191    }
192
193    @Override
194    protected void initialize() throws Exception {
195        for (DefaultManagedObject<?, ?> dmo : defaultManagedObjects.values()) {
196            dmo.initialize();
197        }
198    }
199}