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  package org.opencastproject.security.impl.jpa;
22  
23  import org.opencastproject.security.api.Role;
24  import org.opencastproject.util.EqualsUtil;
25  
26  import java.util.Objects;
27  
28  import javax.persistence.Access;
29  import javax.persistence.AccessType;
30  import javax.persistence.Column;
31  import javax.persistence.Entity;
32  import javax.persistence.GeneratedValue;
33  import javax.persistence.Id;
34  import javax.persistence.Index;
35  import javax.persistence.JoinColumn;
36  import javax.persistence.NamedQueries;
37  import javax.persistence.NamedQuery;
38  import javax.persistence.OneToOne;
39  import javax.persistence.Table;
40  import javax.persistence.Transient;
41  import javax.persistence.UniqueConstraint;
42  
43  /**
44   * JPA-annotated role object.
45   */
46  @Entity
47  @Access(AccessType.FIELD)
48  @Table(
49      name = "oc_role",
50      uniqueConstraints = @UniqueConstraint(columnNames = { "name", "organization" }),
51      indexes = {
52          @Index(name = "IX_oc_role_pk", columnList = "name, organization"),
53      }
54  )
55  @NamedQueries({
56      @NamedQuery(
57          name = "Role.findByQuery",
58          query = "select r from JpaRole r "
59              + "where r.organization.id=:org and UPPER(r.name) like :query or UPPER(r.description) like :query"
60      ),
61      @NamedQuery(
62          name = "Role.findByName",
63          query = "Select r FROM JpaRole r where r.name = :name and r.organization.id = :org"
64      ),
65      @NamedQuery(
66          name = "Role.findAll",
67          query = "Select r FROM JpaRole r where r.organization.id = :org"
68      ),
69  })
70  public final class JpaRole implements Role {
71    @Id
72    @Column(name = "id")
73    @GeneratedValue
74    private Long id;
75  
76    @Column(name = "name", length = 128)
77    private String name;
78  
79    @OneToOne()
80    @JoinColumn(name = "organization")
81    private JpaOrganization organization;
82  
83    @Column(name = "description", nullable = true)
84    private String description;
85  
86    @Transient
87    private Type type = Type.INTERNAL;
88  
89    /**
90     * No-arg constructor needed by JPA
91     */
92    public JpaRole() {
93    }
94  
95    /**
96     * Constructs a role with the specified name and organization.
97     *
98     * @param name
99     *          the name
100    * @param organization
101    *          the organization
102    */
103   public JpaRole(String name, JpaOrganization organization) {
104     super();
105     this.name = name;
106     this.organization = organization;
107   }
108 
109   /**
110    * Constructs a role with the specified name, organization and description.
111    *
112    * @param name
113    *          the name
114    * @param organization
115    *          the organization
116    * @param description
117    *          the description
118    */
119   public JpaRole(String name, JpaOrganization organization, String description) {
120     this(name, organization);
121     this.description = description;
122   }
123 
124   /**
125    * Constructs a role with the specified name, organization and description.
126    *
127    * @param name
128    *          the name
129    * @param organization
130    *          the organization
131    * @param description
132    *          the description
133    * @param type
134    *          the role {@link Type}
135    */
136   public JpaRole(String name, JpaOrganization organization, String description, Type type) {
137     this(name, organization, description);
138     this.type = type;
139   }
140 
141   /**
142    * Gets the identifier.
143    *
144    * @return the identifier
145    */
146   public Long getId() {
147     return id;
148   }
149 
150   @Override
151   public String getName() {
152     return name;
153   }
154 
155   @Override
156   public String getDescription() {
157     return description;
158   }
159 
160   /**
161    * Sets the description
162    *
163    * @param description
164    *          the description
165    */
166   public void setDescription(String description) {
167     this.description = description;
168   }
169 
170   @Override
171   public String getOrganizationId() {
172     return organization.getId();
173   }
174 
175   public JpaOrganization getJpaOrganization() {
176     return organization;
177   }
178 
179   @Override
180   public Type getType() {
181     return type;
182   }
183 
184   @Override
185   public int hashCode() {
186     return EqualsUtil.hash(name, getOrganizationId());
187   }
188 
189   @Override
190   public boolean equals(Object obj) {
191     if (!(obj instanceof Role)) {
192       return false;
193     }
194     Role other = (Role) obj;
195     return name.equals(other.getName())
196             && Objects.equals(getOrganizationId(), other.getOrganizationId());
197   }
198 
199   @Override
200   public String toString() {
201     return name + ":" + getOrganizationId();
202   }
203 
204 }