001    /**
002     * Copyright (c) 2000-2011 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(
037                    int modificationOp, String id, String value) {
038    
039                    BasicAttribute basicAttribute = new BasicAttribute(id);
040    
041                    if (Validator.isNotNull(value)) {
042                            basicAttribute.add(value);
043                    }
044    
045                    ModificationItem item = new ModificationItem(
046                            modificationOp, basicAttribute);
047    
048                    _items.add(item);
049    
050                    return item;
051            }
052    
053            public ModificationItem addItem(String id, String value) {
054                    return addItem(DirContext.REPLACE_ATTRIBUTE, id, value);
055            }
056    
057            public ModificationItem[] getItems() {
058                    return _items.toArray(new ModificationItem[_items.size()]);
059            }
060    
061            private Modifications() {
062            }
063    
064            private List<ModificationItem> _items = new ArrayList<ModificationItem>();
065    
066    }