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 2008 Sun Microsystems, Inc.
015 */
016
017package org.forgerock.opendj.config;
018
019import org.forgerock.util.Reject;
020
021import java.util.EnumSet;
022
023import org.forgerock.opendj.ldap.schema.AttributeType;
024import org.forgerock.opendj.ldap.schema.Schema;
025
026/**
027 * Attribute type property definition.
028 */
029public final class AttributeTypePropertyDefinition extends PropertyDefinition<AttributeType> {
030
031    /**
032     * An interface for incrementally constructing attribute type property
033     * definitions.
034     */
035    public static final class Builder extends AbstractBuilder<AttributeType, AttributeTypePropertyDefinition> {
036
037        /** Private constructor. */
038        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
039            super(d, propertyName);
040        }
041
042        /** {@inheritDoc} */
043        @Override
044        protected AttributeTypePropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
045            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
046            DefaultBehaviorProvider<AttributeType> defaultBehavior) {
047            return new AttributeTypePropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
048        }
049    }
050
051    /**
052     * Create a attribute type property definition builder.
053     *
054     * @param d
055     *            The managed object definition associated with this property
056     *            definition.
057     * @param propertyName
058     *            The property name.
059     * @return Returns the new attribute type property definition builder.
060     */
061    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
062        return new Builder(d, propertyName);
063    }
064
065    /** Private constructor. */
066    private AttributeTypePropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
067        EnumSet<PropertyOption> options, AdministratorAction adminAction,
068        DefaultBehaviorProvider<AttributeType> defaultBehavior) {
069        super(d, AttributeType.class, propertyName, options, adminAction, defaultBehavior);
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
075        return v.visitAttributeType(this, p);
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public <R, P> R accept(PropertyValueVisitor<R, P> v, AttributeType value, P p) {
081        return v.visitAttributeType(this, value, p);
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public int compare(AttributeType o1, AttributeType o2) {
087        return o1.getNameOrOID().compareToIgnoreCase(o2.getNameOrOID());
088    }
089
090    /** {@inheritDoc} */
091    @Override
092    public AttributeType decodeValue(String value) {
093        Reject.ifNull(value);
094
095        final String name = value.trim();
096        if (!ConfigurationFramework.getInstance().isClient()
097                && !Schema.getDefaultSchema().hasAttributeType(name)) {
098            // If this is the server then the attribute type must be defined.
099            throw PropertyException.illegalPropertyValueException(this, value);
100        }
101        final AttributeType type =
102                Schema.getDefaultSchema().asNonStrictSchema().getAttributeType(name);
103        try {
104            validateValue(type);
105            return type;
106        } catch (PropertyException e) {
107            throw PropertyException.illegalPropertyValueException(this, value);
108        }
109    }
110
111    /** {@inheritDoc} */
112    @Override
113    public String encodeValue(AttributeType value) {
114        return value.getNameOrOID();
115    }
116
117    /** {@inheritDoc} */
118    @Override
119    public void validateValue(AttributeType value) {
120        Reject.ifNull(value);
121
122        // No implementation required.
123    }
124}