View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.security.api;
23  
24  import org.opencastproject.util.EqualsUtil;
25  
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import javax.xml.bind.annotation.XmlAccessType;
30  import javax.xml.bind.annotation.XmlAccessorType;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlElementWrapper;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.XmlType;
35  
36  /**
37   * A simple user model.
38   */
39  @XmlAccessorType(XmlAccessType.FIELD)
40  @XmlType(name = "group", namespace = "http://org.opencastproject.security")
41  @XmlRootElement(name = "group", namespace = "http://org.opencastproject.security")
42  public final class JaxbGroup implements Group {
43  
44    @XmlElement(name = "id")
45    protected String groupId;
46  
47    @XmlElement(name = "organization")
48    protected JaxbOrganization organization;
49  
50    @XmlElement(name = "name")
51    protected String name;
52  
53    @XmlElement(name = "description")
54    protected String description;
55  
56    @XmlElement(name = "role")
57    protected String role;
58  
59    @XmlElement(name = "member")
60    @XmlElementWrapper(name = "members")
61    protected Set<String> members;
62  
63    @XmlElement(name = "role")
64    @XmlElementWrapper(name = "roles")
65    protected Set<JaxbRole> roles;
66  
67    /**
68     * No-arg constructor needed by JAXB
69     */
70    public JaxbGroup() {
71    }
72  
73    /**
74     * Constructs a group with the specified groupId, name, description and group role.
75     *
76     * @param groupId
77     *          the group id
78     * @param organization
79     *          the organization
80     * @param name
81     *          the name
82     * @param description
83     *          the description
84     */
85    public JaxbGroup(String groupId, JaxbOrganization organization, String name, String description) {
86      super();
87      this.groupId = groupId;
88      this.organization = organization;
89      this.name = name;
90      this.description = description;
91      this.role = ROLE_PREFIX + groupId.toUpperCase();
92      this.roles = new HashSet<JaxbRole>();
93    }
94  
95    /**
96     * Constructs a group with the specified groupId, name, description, group role and roles.
97     *
98     * @param groupId
99     *          the group id
100    * @param organization
101    *          the organization
102    * @param name
103    *          the name
104    * @param description
105    *          the description
106    * @param roles
107    *          the additional group roles
108    */
109   public JaxbGroup(String groupId, JaxbOrganization organization, String name, String description, Set<JaxbRole> roles) {
110     this(groupId, organization, name, description);
111     this.roles = roles;
112   }
113 
114   /**
115    * Constructs a group with the specified groupId, name, description, group role and roles.
116    *
117    * @param groupId
118    *          the group id
119    * @param organization
120    *          the organization
121    * @param name
122    *          the name
123    * @param description
124    *          the description
125    * @param roles
126    *          the additional group roles
127    * @param members
128    *          the group members
129    */
130   public JaxbGroup(String groupId, JaxbOrganization organization, String name, String description, Set<JaxbRole> roles,
131           Set<String> members) {
132     this(groupId, organization, name, description, roles);
133     this.members = members;
134   }
135 
136   public static JaxbGroup fromGroup(Group group) {
137     JaxbOrganization organization = JaxbOrganization.fromOrganization(group.getOrganization());
138     Set<JaxbRole> roles = new HashSet<JaxbRole>();
139     for (Role role : group.getRoles()) {
140       if (role instanceof JaxbRole) {
141         roles.add((JaxbRole) role);
142       } else {
143         roles.add(JaxbRole.fromRole(role));
144       }
145     }
146     return new JaxbGroup(group.getGroupId(), organization, group.getName(), group.getDescription(), roles,
147             group.getMembers());
148   }
149 
150   /**
151    * @see org.opencastproject.security.api.Group#getGroupId()
152    */
153   @Override
154   public String getGroupId() {
155     return groupId;
156   }
157 
158   /**
159    * @see org.opencastproject.security.api.Group#getName()
160    */
161   @Override
162   public String getName() {
163     return name;
164   }
165 
166   /**
167    * @see org.opencastproject.security.api.Group#getOrganization()
168    */
169   @Override
170   public Organization getOrganization() {
171     return organization;
172   }
173 
174   /**
175    * @see org.opencastproject.security.api.Group#getDescription()
176    */
177   @Override
178   public String getDescription() {
179     return description;
180   }
181 
182   /**
183    * @see org.opencastproject.security.api.Group#getRole()
184    */
185   @Override
186   public String getRole() {
187     return role;
188   }
189 
190   /**
191    * @see org.opencastproject.security.api.Group#getMembers()
192    */
193   @Override
194   public Set<String> getMembers() {
195     return members;
196   }
197 
198   /**
199    * @see org.opencastproject.security.api.Group#getRoles()
200    */
201   @Override
202   public Set<Role> getRoles() {
203     return new HashSet<Role>(roles);
204   }
205 
206   /**
207    * {@inheritDoc}
208    *
209    * @see java.lang.Object#hashCode()
210    */
211   @Override
212   public int hashCode() {
213     return EqualsUtil.hash(groupId, organization);
214   }
215 
216   /**
217    * {@inheritDoc}
218    *
219    * @see java.lang.Object#equals(java.lang.Object)
220    */
221   @Override
222   public boolean equals(Object obj) {
223     if (!(obj instanceof Group))
224       return false;
225     Group other = (Group) obj;
226     return groupId.equals(other.getGroupId()) && organization.equals(other.getOrganization());
227   }
228 
229   /**
230    * {@inheritDoc}
231    *
232    * @see java.lang.Object#toString()
233    */
234   @Override
235   public String toString() {
236     return new StringBuilder(groupId).append(":").append(organization).toString();
237   }
238 
239 }