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.Organization;
24  import org.opencastproject.util.EqualsUtil;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.persistence.Access;
30  import javax.persistence.AccessType;
31  import javax.persistence.CollectionTable;
32  import javax.persistence.Column;
33  import javax.persistence.ElementCollection;
34  import javax.persistence.Entity;
35  import javax.persistence.Id;
36  import javax.persistence.Index;
37  import javax.persistence.JoinColumn;
38  import javax.persistence.Lob;
39  import javax.persistence.MapKeyColumn;
40  import javax.persistence.NamedQueries;
41  import javax.persistence.NamedQuery;
42  import javax.persistence.Table;
43  import javax.persistence.UniqueConstraint;
44  
45  /**
46   * JPA-annotated organization object.
47   */
48  @Entity
49  @Access(AccessType.FIELD)
50  @Table(name = "oc_organization")
51  @NamedQueries({
52      @NamedQuery(
53          name = "Organization.findAll",
54          query = "Select o FROM JpaOrganization o"
55      ),
56      @NamedQuery(
57          name = "Organization.findById",
58          query = "Select o FROM JpaOrganization o where o.id = :id"
59      ),
60      @NamedQuery(
61          name = "Organization.findByHost",
62          query = "Select o FROM JpaOrganization o JOIN o.servers s where key(s) = :serverName AND s = :port"
63      ),
64      @NamedQuery(
65          name = "Organization.getCount",
66          query = "Select COUNT(o) FROM JpaOrganization o"
67      ),
68  })
69  public class JpaOrganization implements Organization {
70  
71    @Id
72    @Column(name = "id", length = 128)
73    private String id;
74  
75    @Column(name = "name")
76    private String name;
77  
78    @ElementCollection
79    @MapKeyColumn(name = "name", nullable = false)
80    @Column(name = "port", nullable = false)
81    @CollectionTable(
82        name = "oc_organization_node",
83        joinColumns = @JoinColumn(name = "organization", nullable = false),
84        indexes = {
85            @Index(name = "IX_oc_organization_node_pk", columnList = ("organization")),
86            @Index(name = "IX_oc_organization_node_name", columnList = ("name")),
87            @Index(name = "IX_oc_organization_node_port", columnList = ("port"))
88        },
89        uniqueConstraints = @UniqueConstraint(columnNames = {"organization", "name", "port"})
90    )
91    private Map<String, Integer> servers;
92  
93    @Column(name = "admin_role")
94    private String adminRole;
95  
96    @Column(name = "anonymous_role")
97    private String anonymousRole;
98  
99    @Lob
100   @ElementCollection
101   @MapKeyColumn(name = "name", nullable = false)
102   @Column(name = "\"value\"", length = 65535)
103   @CollectionTable(
104       name = "oc_organization_property",
105       joinColumns = @JoinColumn(name = "organization", nullable = false)
106   )
107   private Map<String, String> properties;
108 
109   /**
110    * No-arg constructor needed by JPA
111    */
112   public JpaOrganization() {
113   }
114 
115   /**
116    * Constructs an organization with its attributes.
117    *
118    * @param orgId
119    *          the unique identifier
120    * @param name
121    *          the friendly name
122    * @param serverName
123    *          the host name
124    * @param serverPort
125    *          the host port
126    * @param adminRole
127    *          name of the local admin role
128    * @param anonymousRole
129    *          name of the local anonymous role
130    * @param properties
131    *          arbitrary properties defined for this organization, which might include branding, etc.
132    */
133   public JpaOrganization(
134       String orgId,
135       String name,
136       String serverName,
137       Integer serverPort,
138       String adminRole,
139       String anonymousRole,
140       Map<String, String> properties
141   ) {
142     super();
143     this.id = orgId;
144     this.name = name;
145     this.servers = new HashMap<String, Integer>();
146     this.servers.put(serverName, serverPort);
147     this.adminRole = adminRole;
148     this.anonymousRole = anonymousRole;
149     this.properties = properties;
150   }
151 
152   /**
153    * Constructs an organization with its attributes.
154    *
155    * @param orgId
156    *          the unique identifier
157    * @param name
158    *          the friendly name
159    * @param servers
160    *          the servers
161    * @param adminRole
162    *          name of the local admin role
163    * @param anonymousRole
164    *          name of the local anonymous role
165    * @param properties
166    *          arbitrary properties defined for this organization, which might include branding, etc.
167    */
168   public JpaOrganization(
169       String orgId,
170       String name,
171       Map<String, Integer> servers,
172       String adminRole,
173       String anonymousRole,
174       Map<String, String> properties
175   ) {
176     super();
177     this.id = orgId;
178     this.name = name;
179     this.servers = servers;
180     this.adminRole = adminRole;
181     this.anonymousRole = anonymousRole;
182     this.properties = properties;
183   }
184 
185   /**
186    * @see org.opencastproject.security.api.Organization#getId()
187    */
188   @Override
189   public String getId() {
190     return id;
191   }
192 
193   /**
194    * @see org.opencastproject.security.api.Organization#getAnonymousRole()
195    */
196   @Override
197   public String getAnonymousRole() {
198     return anonymousRole;
199   }
200 
201   public void setAnonymousRole(String anonymousRole) {
202     this.anonymousRole = anonymousRole;
203   }
204 
205   /**
206    * @see org.opencastproject.security.api.Organization#getAdminRole()
207    */
208   @Override
209   public String getAdminRole() {
210     return adminRole;
211   }
212 
213   public void setAdminRole(String adminRole) {
214     this.adminRole = adminRole;
215   }
216 
217   /**
218    * @see org.opencastproject.security.api.Organization#getName()
219    */
220   @Override
221   public String getName() {
222     return name;
223   }
224 
225   public void setName(String name) {
226     this.name = name;
227   }
228 
229   /**
230    * @see org.opencastproject.security.api.Organization#getProperties()
231    */
232   @Override
233   public Map<String, String> getProperties() {
234     return properties;
235   }
236 
237   public void setProperties(Map<String, String> properties) {
238     this.properties = properties;
239   }
240 
241   /**
242    * @see org.opencastproject.security.api.Organization#getServers()
243    */
244   @Override
245   public Map<String, Integer> getServers() {
246     return servers;
247   }
248 
249   /**
250    * Replaces the existing servers.
251    *
252    * @param servers
253    *          the servers
254    */
255   public void setServers(Map<String, Integer> servers) {
256     this.servers = servers;
257   }
258 
259   /**
260    * Adds the server - port mapping.
261    *
262    * @param serverName
263    *          the server name
264    * @param port
265    *          the port
266    */
267   public void addServer(String serverName, Integer port) {
268     if (servers == null) {
269       servers = new HashMap<String, Integer>();
270     }
271     servers.put(serverName, port);
272   }
273 
274   /**
275    * Removes the given server - port mapping.
276    *
277    * @param serverName
278    *          the server name
279    * @param port
280    *          the port
281    */
282   public void remove(String serverName, Integer port) {
283     if (port.equals(servers.get(serverName))) {
284       servers.remove(serverName);
285     }
286   }
287 
288   @Override
289   public boolean equals(Object obj) {
290     if (!(obj instanceof Organization)) {
291       return false;
292     }
293     return ((Organization) obj).getId().equals(id);
294   }
295 
296   @Override
297   public int hashCode() {
298     return EqualsUtil.hash(id);
299   }
300 
301   @Override
302   public String toString() {
303     return id;
304   }
305 
306 }