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  
25  import java.util.Objects;
26  
27  import javax.xml.bind.annotation.XmlAccessType;
28  import javax.xml.bind.annotation.XmlAccessorType;
29  import javax.xml.bind.annotation.XmlElement;
30  import javax.xml.bind.annotation.XmlRootElement;
31  import javax.xml.bind.annotation.XmlType;
32  
33  /**
34   * A simple user model.
35   */
36  @XmlAccessorType(XmlAccessType.FIELD)
37  @XmlType(name = "role", namespace = "http://org.opencastproject.security")
38  @XmlRootElement(name = "role", namespace = "http://org.opencastproject.security")
39  public final class JaxbRole implements Role {
40  
41    /** The role name */
42    @XmlElement(name = "name")
43    protected String name;
44  
45    /** The description */
46    @XmlElement(name = "description")
47    protected String description;
48  
49    @XmlElement(name = "organization")
50    protected JaxbOrganization organization;
51  
52    @XmlElement(name = "organization-id")
53    protected String organizationId;
54  
55    @XmlElement(name = "type")
56    protected Type type = Type.INTERNAL;
57  
58    /**
59     * No-arg constructor needed by JAXB
60     */
61    public JaxbRole() {
62    }
63  
64    /**
65     * Constructs a role with the specified name and organization.
66     *
67     * @param name
68     *          the name
69     * @param organization
70     *          the organization
71     */
72    public JaxbRole(String name, JaxbOrganization organization) throws IllegalArgumentException {
73      super();
74      this.name = name;
75      this.organizationId = organization.getId();
76    }
77  
78    /**
79     * Constructs a role with the specified name, organization and description.
80     *
81     * @param name
82     *          the name
83     * @param organization
84     *          the organization
85     * @param description
86     *          the description
87     */
88    public JaxbRole(String name, JaxbOrganization organization, String description) throws IllegalArgumentException {
89      this(name, organization);
90      this.description = description;
91    }
92  
93    /**
94     * Constructs a role with the specified name, organization, description, and persistence settings.
95     *
96     * @param name
97     *          the name
98     * @param organization
99     *          the organization
100    * @param description
101    *          the description
102    * @param type
103    *          the role {@link type}
104    */
105   public JaxbRole(String name, JaxbOrganization organization, String description, Type type) throws IllegalArgumentException {
106     this(name, organization, description);
107     this.type = type;
108   }
109 
110 
111   /**
112    * Constructs a role with the specified name, organization identifier, description, and persistence settings.
113    *
114    * @param name
115    *          the name
116    * @param organizationId
117    *          the organization identifier
118    * @param description
119    *          the description
120    * @param type
121    *          the role {@link type}
122    */
123   public JaxbRole(String name, String organizationId, String description, Type type) throws IllegalArgumentException {
124     super();
125     this.name = name;
126     this.organizationId = organizationId;
127     this.description = description;
128     this.type = type;
129   }
130 
131   public static JaxbRole fromRole(Role role) {
132     if (role instanceof JaxbRole)
133       return (JaxbRole) role;
134     return new JaxbRole(role.getName(), role.getOrganizationId(), role.getDescription(), role.getType());
135   }
136 
137   /**
138    * {@inheritDoc}
139    *
140    * @see org.opencastproject.security.api.Role#getName()
141    */
142   @Override
143   public String getName() {
144     return name;
145   }
146 
147   /**
148    * {@inheritDoc}
149    *
150    * @see org.opencastproject.security.api.Role#getDescription()
151    */
152   @Override
153   public String getDescription() {
154     return description;
155   }
156 
157   /**
158    * {@inheritDoc}
159    */
160   public String getOrganizationId() {
161     if (organizationId != null) {
162       return organizationId;
163     }
164     if (organization != null) {
165       return organization.getId();
166     }
167     return null;
168   }
169 
170   /**
171    * {@inheritDoc}
172    *
173    * @see org.opencastproject.security.api.Role#getType()
174    */
175   public Type getType() {
176     return type;
177   }
178 
179   /**
180    * {@inheritDoc}
181    *
182    * @see java.lang.Object#hashCode()
183    */
184   @Override
185   public int hashCode() {
186     return Objects.hash(name, getOrganizationId());
187   }
188 
189   /**
190    * {@inheritDoc}
191    *
192    * @see java.lang.Object#equals(java.lang.Object)
193    */
194   @Override
195   public boolean equals(Object obj) {
196     if (!(obj instanceof Role))
197       return false;
198     Role other = (Role) obj;
199     return name.equals(other.getName())
200             && Objects.equals(getOrganizationId(), other.getOrganizationId());
201   }
202 
203   /**
204    * {@inheritDoc}
205    *
206    * @see java.lang.Object#toString()
207    */
208   @Override
209   public String toString() {
210     return name + ":" + getOrganizationId();
211   }
212 }