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)
106           throws IllegalArgumentException {
107     this(name, organization, description);
108     this.type = type;
109   }
110 
111 
112   /**
113    * Constructs a role with the specified name, organization identifier, description, and persistence settings.
114    *
115    * @param name
116    *          the name
117    * @param organizationId
118    *          the organization identifier
119    * @param description
120    *          the description
121    * @param type
122    *          the role {@link type}
123    */
124   public JaxbRole(String name, String organizationId, String description, Type type) throws IllegalArgumentException {
125     super();
126     this.name = name;
127     this.organizationId = organizationId;
128     this.description = description;
129     this.type = type;
130   }
131 
132   public static JaxbRole fromRole(Role role) {
133     if (role instanceof JaxbRole) {
134       return (JaxbRole) role;
135     }
136     return new JaxbRole(role.getName(), role.getOrganizationId(), role.getDescription(), role.getType());
137   }
138 
139   /**
140    * {@inheritDoc}
141    *
142    * @see org.opencastproject.security.api.Role#getName()
143    */
144   @Override
145   public String getName() {
146     return name;
147   }
148 
149   /**
150    * {@inheritDoc}
151    *
152    * @see org.opencastproject.security.api.Role#getDescription()
153    */
154   @Override
155   public String getDescription() {
156     return description;
157   }
158 
159   /**
160    * {@inheritDoc}
161    */
162   public String getOrganizationId() {
163     if (organizationId != null) {
164       return organizationId;
165     }
166     if (organization != null) {
167       return organization.getId();
168     }
169     return null;
170   }
171 
172   /**
173    * {@inheritDoc}
174    *
175    * @see org.opencastproject.security.api.Role#getType()
176    */
177   public Type getType() {
178     return type;
179   }
180 
181   /**
182    * {@inheritDoc}
183    *
184    * @see java.lang.Object#hashCode()
185    */
186   @Override
187   public int hashCode() {
188     return Objects.hash(name, getOrganizationId());
189   }
190 
191   /**
192    * {@inheritDoc}
193    *
194    * @see java.lang.Object#equals(java.lang.Object)
195    */
196   @Override
197   public boolean equals(Object obj) {
198     if (!(obj instanceof Role)) {
199       return false;
200     }
201     Role other = (Role) obj;
202     return name.equals(other.getName())
203             && Objects.equals(getOrganizationId(), other.getOrganizationId());
204   }
205 
206   /**
207    * {@inheritDoc}
208    *
209    * @see java.lang.Object#toString()
210    */
211   @Override
212   public String toString() {
213     return name + ":" + getOrganizationId();
214   }
215 }