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,
110       Set<JaxbRole> roles) {
111     this(groupId, organization, name, description);
112     this.roles = roles;
113   }
114 
115   /**
116    * Constructs a group with the specified groupId, name, description, group role and roles.
117    *
118    * @param groupId
119    *          the group id
120    * @param organization
121    *          the organization
122    * @param name
123    *          the name
124    * @param description
125    *          the description
126    * @param roles
127    *          the additional group roles
128    * @param members
129    *          the group members
130    */
131   public JaxbGroup(String groupId, JaxbOrganization organization, String name, String description, Set<JaxbRole> roles,
132           Set<String> members) {
133     this(groupId, organization, name, description, roles);
134     this.members = members;
135   }
136 
137   public static JaxbGroup fromGroup(Group group) {
138     JaxbOrganization organization = JaxbOrganization.fromOrganization(group.getOrganization());
139     Set<JaxbRole> roles = new HashSet<JaxbRole>();
140     for (Role role : group.getRoles()) {
141       if (role instanceof JaxbRole) {
142         roles.add((JaxbRole) role);
143       } else {
144         roles.add(JaxbRole.fromRole(role));
145       }
146     }
147     return new JaxbGroup(group.getGroupId(), organization, group.getName(), group.getDescription(), roles,
148             group.getMembers());
149   }
150 
151   /**
152    * @see org.opencastproject.security.api.Group#getGroupId()
153    */
154   @Override
155   public String getGroupId() {
156     return groupId;
157   }
158 
159   /**
160    * @see org.opencastproject.security.api.Group#getName()
161    */
162   @Override
163   public String getName() {
164     return name;
165   }
166 
167   /**
168    * @see org.opencastproject.security.api.Group#getOrganization()
169    */
170   @Override
171   public Organization getOrganization() {
172     return organization;
173   }
174 
175   /**
176    * @see org.opencastproject.security.api.Group#getDescription()
177    */
178   @Override
179   public String getDescription() {
180     return description;
181   }
182 
183   /**
184    * @see org.opencastproject.security.api.Group#getRole()
185    */
186   @Override
187   public String getRole() {
188     return role;
189   }
190 
191   /**
192    * @see org.opencastproject.security.api.Group#getMembers()
193    */
194   @Override
195   public Set<String> getMembers() {
196     return members;
197   }
198 
199   /**
200    * @see org.opencastproject.security.api.Group#getRoles()
201    */
202   @Override
203   public Set<Role> getRoles() {
204     return new HashSet<Role>(roles);
205   }
206 
207   /**
208    * {@inheritDoc}
209    *
210    * @see java.lang.Object#hashCode()
211    */
212   @Override
213   public int hashCode() {
214     return EqualsUtil.hash(groupId, organization);
215   }
216 
217   /**
218    * {@inheritDoc}
219    *
220    * @see java.lang.Object#equals(java.lang.Object)
221    */
222   @Override
223   public boolean equals(Object obj) {
224     if (!(obj instanceof Group)) {
225       return false;
226     }
227     Group other = (Group) obj;
228     return groupId.equals(other.getGroupId()) && organization.equals(other.getOrganization());
229   }
230 
231   /**
232    * {@inheritDoc}
233    *
234    * @see java.lang.Object#toString()
235    */
236   @Override
237   public String toString() {
238     return new StringBuilder(groupId).append(":").append(organization).toString();
239   }
240 
241 }