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