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.application;
6   
7   import java.util.ArrayList;
8   import java.util.Date;
9   import java.util.HashSet;
10  import java.util.List;
11  import java.util.Set;
12  
13  import javax.persistence.CascadeType;
14  import javax.persistence.Column;
15  import javax.persistence.Entity;
16  import javax.persistence.FetchType;
17  import javax.persistence.JoinColumn;
18  import javax.persistence.ManyToMany;
19  import javax.persistence.OneToMany;
20  import javax.persistence.OneToOne;
21  import javax.persistence.Temporal;
22  import javax.persistence.TemporalType;
23  import javax.persistence.Transient;
24  
25  import org.directdemocracyportal.democracy.model.core.Agent;
26  import org.directdemocracyportal.democracy.model.core.Role;
27  import org.directdemocracyportal.democracy.model.world.OrganisationType;
28  import org.directdemocracyportal.democracy.model.world.Person;
29  import org.hibernate.annotations.Cache;
30  import org.hibernate.annotations.CacheConcurrencyStrategy;
31  
32  /***
33   * The Class User.
34   */
35  @Entity
36  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
37  public class User extends Agent
38  {
39  
40      /*** The Constant serialVersionUID. */
41      private static final long serialVersionUID = -1381199844545595026L;
42  
43      /*** The username. */
44      private String username;
45  
46      /*** The password. */
47      private String password;
48  
49      /*** The email. */
50      private String email;
51  
52      /*** The created date. */
53      private Date createdDate;
54  
55      /*** The person. */
56      private Person person;
57  
58      /*** The account non expired. */
59      private boolean accountNonExpired;
60  
61      /*** The account non locked. */
62      private boolean accountNonLocked;
63  
64      /*** The credentials non expired. */
65      private boolean credentialsNonExpired;
66  
67      /*** The enabled. */
68      private boolean enabled;
69  
70      /*** The granted authorities. */
71      private Set<Authority> grantedAuthorities = new HashSet<Authority>();
72  
73      /*** The events. */
74      private List<Event> events = new ArrayList<Event>();
75  
76      /***
77       * Instantiates a new user.
78       */
79      public User() {
80      }
81  
82      /***
83       * Instantiates a new user.
84       *
85       * @param createdDate the created date
86       * @param username the username
87       * @param password the password
88       * @param email the email
89       * @param person the person
90       */
91      public User(Date createdDate, String username, String password,String email,
92              Person person) {
93          this.username = username;
94          this.password = password;
95          this.createdDate = createdDate;
96          this.email = email;
97          this.person = person;
98      }
99  
100     /***
101      * Gets the password.
102      *
103      * @return the password
104      */
105     public String getPassword() {
106         return this.password;
107     }
108 
109     /***
110      * Sets the password.
111      *
112      * @param password the new password
113      */
114     public void setPassword(String password) {
115         this.password = password;
116     }
117 
118     /***
119      * Gets the username.
120      *
121      * @return the username
122      */
123     @Column(unique = true)
124     public String getUsername() {
125         return this.username;
126     }
127 
128     /***
129      * Sets the username.
130      *
131      * @param username the new username
132      */
133     public void setUsername(String username) {
134         this.username = username;
135     }
136 
137     /***
138      * Sets the created date.
139      *
140      * @param createdDate the new created date
141      */
142     @Temporal(TemporalType.DATE)
143     public void setCreatedDate(Date createdDate) {
144         this.createdDate = createdDate;
145     }
146 
147     /***
148      * Gets the created date.
149      *
150      * @return the created date
151      */
152     public Date getCreatedDate() {
153         return this.createdDate;
154     }
155 
156     /***
157      * Gets the events.
158      *
159      * @return the events
160      */
161     @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
162     public List<Event> getEvents() {
163         return events;
164     }
165 
166     /***
167      * Sets the events.
168      *
169      * @param events the new events
170      */
171     public void setEvents(List<Event> events) {
172         this.events = events;
173     }
174 
175     /***
176      * Gets the person.
177      *
178      * @return the person
179      */
180     @OneToOne(cascade = CascadeType.ALL)
181     @JoinColumn(name = "person_id")
182     public Person getPerson() {
183         return person;
184     }
185 
186     /***
187      * Sets the person.
188      *
189      * @param person the new person
190      */
191     public void setPerson(Person person) {
192         this.person = person;
193     }
194 
195     /***
196      * Gets the granted authorities.
197      *
198      * @return the granted authorities
199      */
200     @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
201     public Set<Authority> getGrantedAuthorities() {
202         return this.grantedAuthorities;
203     }
204 
205     /***
206      * Sets the granted authorities.
207      *
208      * @param grantedAuthorities the new granted authorities
209      */
210     public void setGrantedAuthorities(Set<Authority> grantedAuthorities) {
211         this.grantedAuthorities = grantedAuthorities;
212     }
213 
214     /***
215      * Sets the account non expired.
216      *
217      * @param accountNonExpired the new account non expired
218      */
219     public void setAccountNonExpired(boolean accountNonExpired) {
220         this.accountNonExpired = accountNonExpired;
221     }
222 
223     /***
224      * Sets the account non locked.
225      *
226      * @param accountNonLocked the new account non locked
227      */
228     public void setAccountNonLocked(boolean accountNonLocked) {
229         this.accountNonLocked = accountNonLocked;
230     }
231 
232     /***
233      * Sets the credentials non expired.
234      *
235      * @param credentialsNonExpired the new credentials non expired
236      */
237     public void setCredentialsNonExpired(boolean credentialsNonExpired) {
238         this.credentialsNonExpired = credentialsNonExpired;
239     }
240 
241     /***
242      * Sets the enabled.
243      *
244      * @param enabled the new enabled
245      */
246     public void setEnabled(boolean enabled) {
247         this.enabled = enabled;
248     }
249 
250     /***
251      * Checks if is account non expired.
252      *
253      * @return true, if is account non expired
254      */
255     public boolean isAccountNonExpired() {
256         return accountNonExpired;
257     }
258 
259     /***
260      * Checks if is account non locked.
261      *
262      * @return true, if is account non locked
263      */
264     public boolean isAccountNonLocked() {
265         return accountNonLocked;
266     }
267 
268     /***
269      * Checks if is credentials non expired.
270      *
271      * @return true, if is credentials non expired
272      */
273     public boolean isCredentialsNonExpired() {
274         return credentialsNonExpired;
275     }
276 
277     /***
278      * Checks if is enabled.
279      *
280      * @return true, if is enabled
281      */
282     public boolean isEnabled() {
283         return enabled;
284     }
285 
286     /***
287      * Gets the email.
288      *
289      * @return the email
290      */
291     public String getEmail() {
292         return email;
293     }
294 
295     /***
296      * Sets the email.
297      *
298      * @param email the new email
299      */
300     public void setEmail(String email) {
301         this.email = email;
302     }
303 
304     /***
305      * Gets the local government portal.
306      *
307      * @return the local government portal
308      */
309     @Transient
310     public GovernmentPortal getLocalGovernmentPortal() {
311         return getGovernmentPortal(OrganisationType.Local);
312     }
313 
314     /***
315      * Gets the regional government portal.
316      *
317      * @return the regional government portal
318      */
319     @Transient
320     public GovernmentPortal getRegionalGovernmentPortal() {
321         return getGovernmentPortal(OrganisationType.Regional);
322     }
323 
324     /***
325      * Gets the national government portal.
326      *
327      * @return the national government portal
328      */
329     @Transient
330     public GovernmentPortal getNationalGovernmentPortal() {
331         return getGovernmentPortal(OrganisationType.National);
332     }
333 
334     /***
335      * Gets the government portal.
336      *
337      * @param organisationType the organisation type
338      * @return the government portal
339      */
340     @Transient
341     private GovernmentPortal getGovernmentPortal(
342             OrganisationType organisationType) {
343         for (Role role : getPerson().getRoles()) {
344             if (role.getGroupAgent() instanceof GovernmentPortal) {
345                 GovernmentPortal governmentPortal = (GovernmentPortal) role
346                         .getGroupAgent();
347 
348                 if (governmentPortal.getOrganisationType().equals(
349                         organisationType)) {
350                     return governmentPortal;
351                 }
352             }
353         }
354         return null;
355     }
356 }