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