Validates a modification of a LDAP attribute. - Java javax.naming.directory

Java examples for javax.naming.directory:Attributes

Description

Validates a modification of a LDAP attribute.

Demo Code

/*/*  ww w  .  j av  a 2s  . c  o m*/
 * Copyright (C) 2009 - 2014 Interactive Media Management
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

public class Main {
    /**
     * Validates a modification of an attribute. The method will determine if
     * the attribute needs to be <code>ADDED</code>, <code>REMOVED</code> or
     * <code>MODIFIED</code>.
     *
     * @param key Key of the attribute
     * @param newValue New value of the attribute
     * @param oldValue Old value of the attribute
     * @return {@link ModificationItem} for either ADD, REMOVE or MODIFY
     */
    public static ModificationItem validateModification(final String key,
            final String newValue, final String oldValue) {

        ModificationItem mod;

        if ("".equalsIgnoreCase(newValue)) {
            if (!"".equalsIgnoreCase(oldValue)) {
                mod = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
                        new BasicAttribute(key));
            } else {
                mod = null;
            }
        } else {
            mod = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                    new BasicAttribute(key, newValue));
        }

        return mod;
    }
}

Related Tutorials