View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package org.directdemocracyportal.democracy.model.core;
6   
7   import java.util.HashSet;
8   import java.util.Set;
9   
10  import javax.persistence.CascadeType;
11  import javax.persistence.Entity;
12  import javax.persistence.FetchType;
13  import javax.persistence.JoinColumn;
14  import javax.persistence.ManyToOne;
15  import javax.persistence.OneToMany;
16  import javax.persistence.Transient;
17  
18  import org.hibernate.annotations.Cache;
19  import org.hibernate.annotations.CacheConcurrencyStrategy;
20  
21  /***
22   * The Class GroupAgent.
23   */
24  @Entity
25  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
26  public abstract class GroupAgent extends Agent
27  {
28  
29      /*** The parent. */
30      private GroupAgent parent;
31  
32      /*** The children. */
33      private Set<GroupAgent> children = new HashSet<GroupAgent>();
34  
35      /*** The defined roles. */
36      private Set<Role> definedRoles = new HashSet<Role>();
37  
38      /***
39       * Instantiates a new group agent.
40       */
41      public GroupAgent() {
42      }
43  
44      /***
45       * Instantiates a new group agent.
46       *
47       * @param name the name
48       */
49      public GroupAgent(String name) {
50          super(name);
51      }
52  
53      /***
54       * Gets the parent.
55       *
56       * @return the parent
57       */
58      @ManyToOne
59      @JoinColumn(name = "PARENT_ID")
60      public GroupAgent getParent() {
61          return parent;
62      }
63  
64      /***
65       * Sets the parent.
66       *
67       * @param parent the new parent
68       */
69      public void setParent(GroupAgent parent) {
70          this.parent = parent;
71      }
72  
73      /***
74       * Gets the children.
75       *
76       * @return the children
77       */
78      @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
79      public Set<GroupAgent> getChildren() {
80          return children;
81      }
82  
83      /***
84       * Sets the children.
85       *
86       * @param children the new children
87       */
88      public void setChildren(Set<GroupAgent> children) {
89          this.children = children;
90      }
91  
92      /***
93       * Gets the defined roles.
94       *
95       * @return the defined roles
96       */
97      @OneToMany(mappedBy = "groupAgent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
98      public Set<Role> getDefinedRoles() {
99          return this.definedRoles;
100     }
101 
102     /***
103      * Sets the defined roles.
104      *
105      * @param definedRoles the new defined roles
106      */
107     public void setDefinedRoles(Set<Role> definedRoles) {
108         this.definedRoles = definedRoles;
109     }
110 
111     /***
112      * Find org by name.
113      *
114      * @param name the name
115      * @return the group agent
116      */
117     @Transient
118     public GroupAgent findOrgByName(String name) {
119         for (GroupAgent organisation : getChildren()) {
120             if (name.equals(organisation.getName())) {
121                 return organisation;
122             }
123         }
124         return null;
125     }
126 
127     /***
128      * Find defined role by name.
129      *
130      * @param name the name
131      * @return the role
132      */
133     @Transient
134     public Role findDefinedRoleByName(String name) {
135         for (Role role : getDefinedRoles()) {
136             if (name.equals(role.getName())) {
137                 return role;
138             }
139         }
140         return null;
141     }
142 }