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 2014-2016 ForgeRock AS.
016 */
017package org.forgerock.opendj.config.conditions;
018
019import java.util.SortedSet;
020
021import org.forgerock.opendj.config.AbstractManagedObjectDefinition;
022import org.forgerock.opendj.config.PropertyDefinition;
023import org.forgerock.opendj.config.client.ManagedObject;
024import org.forgerock.opendj.config.client.ManagementContext;
025import org.forgerock.opendj.config.server.ConfigException;
026import org.forgerock.opendj.config.server.ServerManagedObject;
027import org.forgerock.opendj.ldap.LdapException;
028import org.forgerock.util.Reject;
029
030/**
031 * A condition which evaluates to <code>true</code> if and only if a property
032 * contains a particular value.
033 */
034public final class ContainsCondition implements Condition {
035
036    /**
037     * The strongly typed underlying implementation.
038     *
039     * @param <T>
040     *            The type of the property value being tested.
041     */
042    private static final class Impl<T> implements Condition {
043
044        /** The property. */
045        final PropertyDefinition<T> pd;
046
047        /** The required property value. */
048        final T value;
049
050        /** Private constructor. */
051        private Impl(PropertyDefinition<T> pd, T value) {
052            this.pd = pd;
053            this.value = value;
054        }
055
056        @Override
057        public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
058            SortedSet<T> values = managedObject.getPropertyValues(pd);
059            return values.contains(value);
060        }
061
062        @Override
063        public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
064            SortedSet<T> values = managedObject.getPropertyValues(pd);
065            return values.contains(value);
066        }
067
068        @Override
069        public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
070            // Not used.
071        }
072
073        /** Private implementation of fix() method. */
074        private void setPropertyValue(ManagedObject<?> managedObject) {
075            managedObject.setPropertyValue(pd, value);
076        }
077
078    }
079
080    /** The strongly typed private implementation. */
081    private Impl<?> impl;
082
083    /** The property name. */
084    private final String propertyName;
085
086    /** The string representation of the required property value. */
087    private final String propertyStringValue;
088
089    /**
090     * Creates a new contains value condition.
091     *
092     * @param propertyName
093     *            The property name.
094     * @param stringValue
095     *            The string representation of the required property value.
096     */
097    public ContainsCondition(String propertyName, String stringValue) {
098        Reject.ifNull(propertyName, stringValue);
099        this.propertyName = propertyName;
100        this.propertyStringValue = stringValue;
101    }
102
103    @Override
104    public boolean evaluate(ManagementContext context, ManagedObject<?> managedObject) throws LdapException {
105        return impl.evaluate(context, managedObject);
106    }
107
108    @Override
109    public boolean evaluate(ServerManagedObject<?> managedObject) throws ConfigException {
110        return impl.evaluate(managedObject);
111    }
112
113    /**
114     * Modifies the provided managed object so that it has the property value
115     * associated with this condition.
116     *
117     * @param managedObject
118     *            The managed object.
119     */
120    public void setPropertyValue(ManagedObject<?> managedObject) {
121        impl.setPropertyValue(managedObject);
122    }
123
124    @Override
125    public void initialize(AbstractManagedObjectDefinition<?, ?> d) throws Exception {
126        // Decode the property.
127        buildImpl(d.getPropertyDefinition(propertyName));
128    }
129
130    /** Creates the new private implementation. */
131    private <T> void buildImpl(PropertyDefinition<T> pd) {
132        T value = pd.decodeValue(propertyStringValue);
133        this.impl = new Impl<>(pd, value);
134    }
135
136    /**
137     * Returns the property definition associated with this condition.
138     *
139     * @return the property definition associated with this condition.
140     */
141    public PropertyDefinition<?> getPropertyDefinition() {
142        return impl.pd;
143    }
144
145    /**
146     * Returns the value that must be set for this condition to be fulfilled.
147     *
148     * @return the value that must be set for this condition to be fulfilled.
149     */
150    public Object getValue() {
151        return impl.value;
152    }
153}