001    /*
002     * Copyright 2011-2012 UnboundID Corp.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     * it under the terms of the GNU General Public License (GPLv2 only)
006     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007     * as published by the Free Software Foundation.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, see <http://www.gnu.org/licenses>.
016     */
017    
018    package com.unboundid.scim.data;
019    
020    import com.unboundid.scim.schema.ResourceDescriptor;
021    import com.unboundid.scim.sdk.InvalidResourceException;
022    import com.unboundid.scim.sdk.SCIMConstants;
023    import com.unboundid.scim.sdk.SCIMObject;
024    
025    import java.util.Collection;
026    
027    /**
028     * This class represents a Group resource.
029     */
030    public class GroupResource extends BaseResource
031    {
032      /**
033       * A <code>ResourceFactory</code> for creating <code>GroupResource</code>
034       * instances.
035       */
036      public static final ResourceFactory<GroupResource> GROUP_RESOURCE_FACTORY =
037          new ResourceFactory<GroupResource>() {
038        public GroupResource createResource(
039            final ResourceDescriptor resourceDescriptor,
040            final SCIMObject scimObject) {
041          return new GroupResource(resourceDescriptor, scimObject);
042        }
043      };
044    
045      /**
046       * Construct an empty <code>GroupResource</code> with the specified
047       * <code>ResourceDescriptor</code>.
048       *
049       * @param resourceDescriptor The resource descriptor for this SCIM resource.
050       */
051      public GroupResource(final ResourceDescriptor resourceDescriptor) {
052        super(resourceDescriptor);
053      }
054    
055      /**
056       * Construct a <code>GroupResource</code> with the specified
057       * <code>ResourceDescriptor</code> and backed by the given
058       * <code>SCIMObject</code>.
059       *
060       * @param resourceDescriptor The resource descriptor for this SCIM resource.
061       * @param scimObject         The <code>SCIMObject</code> containing all the
062       *                           SCIM attributes and their values.
063       */
064      public GroupResource(final ResourceDescriptor resourceDescriptor,
065                           final SCIMObject scimObject) {
066        super(resourceDescriptor, scimObject);
067      }
068    
069      /**
070       * Retrieves the human readable name for the Group.
071       *
072       * @return the human readable name for the Group.
073       */
074      public String getDisplayName()
075      {
076        return getSingularAttributeValue(SCIMConstants.SCHEMA_URI_CORE,
077            "displayName", AttributeValueResolver.STRING_RESOLVER);
078      }
079    
080      /**
081       * Sets the human readable name for the Group.
082       *
083       * @param displayName the human readable name for the Group.
084       * @return this resource instance.
085       */
086      public GroupResource setDisplayName(final String displayName)
087      {
088        try {
089          setSingularAttributeValue(SCIMConstants.SCHEMA_URI_CORE, "displayName",
090              AttributeValueResolver.STRING_RESOLVER, displayName);
091        } catch (InvalidResourceException e) {
092          // This should never happen as these are core attributes...
093          throw new RuntimeException(e);
094        }
095        return this;
096      }
097    
098      /**
099       * Retrieves the list of member IDs of the Group.
100       *
101       * @return the list of member IDs of the Group.
102       */
103      public Collection<Entry<String>> getMembers()
104      {
105        return getAttributeValues(SCIMConstants.SCHEMA_URI_CORE,
106            "members", Entry.STRINGS_RESOLVER);
107      }
108    
109      /**
110       * Sets the list of member IDs of the Group.
111       *
112       * @param members the list of member IDs of the Group.
113       * @return this resource instance.
114       */
115      public GroupResource setMembers(final Collection<Entry<String>> members)
116      {
117        try {
118          setAttributeValues(SCIMConstants.SCHEMA_URI_CORE, "members",
119              Entry.STRINGS_RESOLVER, members);
120        } catch (InvalidResourceException e) {
121          // This should never happen as these are core attributes...
122          throw new RuntimeException(e);
123        }
124        return this;
125      }
126    }