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 2013-2016 ForgeRock AS.
015 */
016package org.forgerock.opendj.rest2ldap;
017
018import org.forgerock.opendj.ldap.AttributeDescription;
019
020/** The writability policy determines whether an attribute supports updates. */
021public enum WritabilityPolicy {
022    // @formatter:off
023    /**
024     * The attribute cannot be provided when creating a new resource, nor
025     * modified afterwards. Attempts to update the attribute will result in an
026     * error.
027     */
028    READ_ONLY("readOnly", false),
029
030    /**
031     * The attribute cannot be provided when creating a new resource, nor
032     * modified afterwards. Attempts to update the attribute will not result in
033     * an error (the new values will be ignored).
034     */
035    READ_ONLY_DISCARD_WRITES("readOnlyDiscardWrites", true),
036
037    /**
038     * The attribute may be provided when creating a new resource, but cannot be
039     * modified afterwards. Attempts to update the attribute will result in an
040     * error.
041     */
042    CREATE_ONLY("createOnly", false),
043
044    /**
045     * The attribute may be provided when creating a new resource, but cannot be
046     * modified afterwards. Attempts to update the attribute will not result in
047     * an error (the new values will be ignored).
048     */
049    CREATE_ONLY_DISCARD_WRITES("createOnlyDiscardWrites", true),
050
051    /**
052     * The attribute may be provided when creating a new resource, and modified
053     * afterwards.
054     */
055    READ_WRITE("readWrite", false);
056    // @formatter:on
057
058    private final String name;
059    private final boolean discardWrites;
060
061    WritabilityPolicy(final String name, final boolean discardWrites) {
062        this.name = name;
063        this.discardWrites = discardWrites;
064    }
065
066    boolean canCreate(final AttributeDescription attribute) {
067        return this != READ_ONLY && !attribute.getAttributeType().isNoUserModification();
068    }
069
070    boolean canWrite(final AttributeDescription attribute) {
071        return this == READ_WRITE && !attribute.getAttributeType().isNoUserModification();
072    }
073
074    boolean discardWrites() {
075        return discardWrites;
076    }
077
078    @Override
079    public String toString() {
080        return name;
081    }
082}