001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.naming.directory.BasicAttribute;
023    import javax.naming.directory.DirContext;
024    import javax.naming.directory.ModificationItem;
025    
026    /**
027     * @author Amos Fong
028     * @author Brian Wing Shun Chan
029     */
030    public class Modifications {
031    
032            public static Modifications getInstance() {
033                    return new Modifications();
034            }
035    
036            public ModificationItem addItem(BasicAttribute basicAttribute) {
037                    return addItem(DirContext.REPLACE_ATTRIBUTE, basicAttribute);
038            }
039    
040            public ModificationItem addItem(
041                    int modificationOp, BasicAttribute basicAttribute) {
042    
043                    ModificationItem modificationItem = new ModificationItem(
044                            modificationOp, basicAttribute);
045    
046                    _modificationItems.add(modificationItem);
047    
048                    return modificationItem;
049            }
050    
051            public ModificationItem addItem(
052                    int modificationOp, String id, Object value) {
053    
054                    BasicAttribute basicAttribute = new BasicAttribute(id);
055    
056                    if (value != null) {
057                            basicAttribute.add(value);
058                    }
059    
060                    return addItem(modificationOp, basicAttribute);
061            }
062    
063            public ModificationItem addItem(
064                    int modificationOp, String id, String value) {
065    
066                    BasicAttribute basicAttribute = new BasicAttribute(id);
067    
068                    if (Validator.isNotNull(value)) {
069                            basicAttribute.add(value);
070                    }
071    
072                    return addItem(modificationOp, basicAttribute);
073            }
074    
075            public ModificationItem addItem(String id, Object value) {
076                    return addItem(DirContext.REPLACE_ATTRIBUTE, id, value);
077            }
078    
079            public ModificationItem addItem(String id, String value) {
080                    return addItem(DirContext.REPLACE_ATTRIBUTE, id, value);
081            }
082    
083            public ModificationItem[] getItems() {
084                    return _modificationItems.toArray(
085                            new ModificationItem[_modificationItems.size()]);
086            }
087    
088            private Modifications() {
089            }
090    
091            private List<ModificationItem> _modificationItems =
092                    new ArrayList<ModificationItem>();
093    
094    }