001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: PeopleContainer.java,v 1.4 2009/01/28 05:34:50 ww203982 Exp $
026 *
027 */
028
029package com.iplanet.ums;
030
031import java.security.Principal;
032
033import com.sun.identity.shared.ldap.util.DN;
034
035import com.iplanet.services.ldap.Attr;
036import com.iplanet.services.ldap.AttrSet;
037
038/**
039 * Represents People Container in UMS. People Container is simply a container
040 * for storing user entries.
041 * 
042 * @supported.api
043 */
044public class PeopleContainer extends PersistentObject {
045
046    /**
047     * No args constructor, used to contruct the right object as entries are
048     * read from persistent storage.
049     */
050    protected PeopleContainer() throws UMSException {
051    }
052
053    /**
054     * Constructor of People Container from supplied session and guid
055     * identifying the People Container to be constructed.
056     * 
057     * @param session
058     *            authenticated session object maintained by the Session Manager
059     * @param guid
060     *            globally unique identifier for the People Container
061     */
062    PeopleContainer(Principal principal, Guid guid) throws UMSException {
063        super(principal, guid);
064        verifyClass();
065    }
066
067    /**
068     * Constructs an People Container object without a session. Unlike the
069     * constructor with a session parameter , this one simply creates an object
070     * in memory, using the default template. Call save() to save the object to
071     * the persistent store.
072     * 
073     * @param attrSet
074     *            attribute/value set
075     * @supported.api
076     */
077    PeopleContainer(AttrSet attrSet) throws UMSException {
078        this(TemplateManager.getTemplateManager().getCreationTemplate(_class,
079                null), attrSet);
080    }
081
082    /**
083     * Constructs a People Container object without a session. Unlike
084     * constructor with session, this one simply creates People Container object
085     * in memory. Call save() to save the object to persistent storage.
086     * 
087     * @param template
088     *            template for the People Container, containing required and
089     *            optional attributes, and possibly default values
090     * @param attrSet
091     *            attribute/value set 
092     * @supported.api
093     */
094    public PeopleContainer(CreationTemplate template, AttrSet attrSet)
095            throws UMSException {
096        super(template, attrSet);
097    }
098
099    /**
100     * Adds a new User object to the People Container.
101     * 
102     * @param user
103     *            User object to be added to the container
104     * @exception AccessRightsEsception
105     *                if an access rights exception occurs
106     * @exception EntryAlreadyExistsException
107     *                if the entry already exists
108     * @exception UMSException
109     *                Fail to add the object 
110     * @supported.api
111     */
112    public void addUser(User user) throws AccessRightsException,
113            EntryAlreadyExistsException, UMSException {
114        super.addChild(user);
115    }
116
117    /**
118     * Adds a new People Container object to the People Container.
119     * 
120     * @param pc
121     *            People Container object to be added to the container
122     * @exception AccessRightsEsception
123     *                if an access rights exception occurs
124     * @exception EntryAlreadyExistsException
125     *                if the entry already exists
126     * @exception UMSException
127     *                fails to add the object 
128     * @supported.api
129     */
130    public void addChildPeopleContainer(PeopleContainer pc)
131            throws AccessRightsException, EntryAlreadyExistsException,
132            UMSException {
133        super.addChild(pc);
134    }
135
136    /**
137     * Removes an User object from the People Container.
138     * 
139     * @param user
140     *            User object to be removed to the container
141     * @exception AccessRightsEsception
142     *                if an access rights exception occurs
143     * @exception UMSException
144     *                fails to remove the object 
145     * @supported.api
146     */
147    public void removeUser(User user) throws AccessRightsException,
148            UMSException {
149        super.removeChild(user);
150    }
151
152    /**
153     * Removes a People Container object from the People Container.
154     * 
155     * @param pc
156     *            People Container object to be removed to the container
157     * @exception AccessRightsEsception
158     *                if an access rights exception occurs
159     * @exception EntryNotFoundException
160     *                if the entry is not found
161     * @exception UMSException
162     *                fails to remove the object 
163     * @supported.api
164     */
165    public void removeChildPeopleContainer(PeopleContainer pc)
166            throws AccessRightsException, EntryNotFoundException, UMSException {
167        super.removeChild(pc);
168    }
169
170    /**
171     * Gets the current number of users.
172     * 
173     * @return the current number of users 
174     * @supported.api
175     */
176    public long getUserCount() throws UMSException {
177        /*
178         * String value = getAttribute( NUM_USER_ATTR_NAME ).getValue(); return
179         * (new Long( value )).longValue();
180         */
181        SearchTemplate template = TemplateManager.getTemplateManager()
182                .getSearchTemplate(BASIC_USER_SEARCH, getParentGuid());
183        SearchResults results = getChildren(template, null);
184        long count = 0;
185        while (results.hasMoreElements()) {
186            results.next();
187            count++;
188        }
189        return count;
190    }
191
192    /**
193     * Gets the current number of People Containers.
194     * 
195     * @return the current number of People Containers
196     * @supported.api
197     */
198    public long getChildPeopleContainerCount() throws UMSException {
199        /*
200         * String value = getAttribute( NUM_PEOPLECONTAINER_ATTR_NAME
201         * ).getValue(); return (new Long( value )).longValue();
202         */
203        SearchTemplate template = TemplateManager.getTemplateManager()
204                .getSearchTemplate(BASIC_PEOPLECONTAINER_SEARCH,
205                        getParentGuid());
206        SearchResults results = getChildren(template, null);
207        long count = 0;
208        while (results.hasMoreElements()) {
209            results.next();
210            count++;
211        }
212        return count;
213    }
214
215    /**
216     * Sets max user limit for a People Container.
217     * 
218     * @param limit
219     *            number of users allowed in a People Container
220     * @supported.api
221     */
222    public void setMaxUserLimit(long limit) throws UMSException {
223        String value = (new Long(limit)).toString();
224        setAttribute(new Attr(MAX_USER_ATTR_NAME, value));
225    }
226
227    /**
228     * Sets max children People Container limit for a People Container.
229     * 
230     * @param limit
231     *            number of children People Containers allowed
232     * @supported.api
233     */
234    public void setMaxChildPeopleContainerLimit(long limit) throws UMSException
235    {
236        String value = (new Long(limit)).toString();
237        setAttribute(new Attr(MAX_PEOPLECONTAINER_ATTR_NAME, value));
238    }
239
240    /**
241     * Gets the user limit constraint.
242     * 
243     * @return user limit constraint 
244     * @supported.api
245     */
246    public long getMaxUserLimit() throws UMSException {
247        String value = getAttribute(MAX_USER_ATTR_NAME).getValue();
248        return (new Long(value)).longValue();
249    }
250
251    /**
252     * Gets the container limit constraint.
253     * 
254     * @return container limit constraint 
255     * @supported.api
256     */
257    public long getMaxChildPeopleContainerLimit() throws UMSException {
258        String value = getAttribute(MAX_PEOPLECONTAINER_ATTR_NAME).getValue();
259        return (new Long(value)).longValue();
260    }
261
262    /**
263     * Checks if a given user is a member of the container.
264     * 
265     * @param user
266     *            User object to be checked
267     * @return true if it is a member 
268     * @supported.api
269     */
270    public boolean isMember(User user) throws UMSException {
271        DN userdn = new DN(user.getDN());
272        DN pcdn = new DN(this.getDN());
273
274        if (userdn.getParent().equals(pcdn)) {
275            return true;
276        } else {
277            return false;
278        }
279    }
280
281    /**
282     * Return name of the People Container.
283     * 
284     * @return name of the People Container 
285     * @supported.api
286     */
287    public String getName() throws UMSException {
288        return (getAttribute(getNamingAttribute()).getValue());
289    }
290
291    /**
292     * Resource keys
293     */
294    private static final Class _class = com.iplanet.ums.PeopleContainer.class;
295
296    private static final String MAX_USER_ATTR_NAME = "nsMaxUsers";
297
298    private static final String MAX_PEOPLECONTAINER_ATTR_NAME = 
299        "nsMaxPeopleContainers";
300
301    private static final String BASIC_USER_SEARCH = "BasicUserSearch";
302
303    private static final String BASIC_PEOPLECONTAINER_SEARCH = 
304        "BasicPeopleContainerSearch";
305
306    static final String MULTIPLE_PEOPLE_CONTAINERS_SUPPORT = 
307        "MultiplePeopleContainersSupport";
308
309}