2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.model
.common
;
11 import java
.util
.HashSet
;
14 import javax
.persistence
.Entity
;
15 import javax
.persistence
.FetchType
;
16 import javax
.persistence
.ManyToMany
;
17 import javax
.persistence
.Table
;
18 import javax
.xml
.bind
.annotation
.XmlAccessType
;
19 import javax
.xml
.bind
.annotation
.XmlAccessorType
;
20 import javax
.xml
.bind
.annotation
.XmlElement
;
21 import javax
.xml
.bind
.annotation
.XmlElementWrapper
;
22 import javax
.xml
.bind
.annotation
.XmlIDREF
;
23 import javax
.xml
.bind
.annotation
.XmlRootElement
;
24 import javax
.xml
.bind
.annotation
.XmlSchemaType
;
25 import javax
.xml
.bind
.annotation
.XmlType
;
27 import org
.apache
.log4j
.Logger
;
28 import org
.hibernate
.annotations
.Cascade
;
29 import org
.hibernate
.annotations
.CascadeType
;
30 import org
.hibernate
.annotations
.NaturalId
;
31 import org
.hibernate
.search
.annotations
.Field
;
32 import org
.hibernate
.search
.annotations
.Indexed
;
33 import org
.springframework
.security
.core
.GrantedAuthority
;
35 @XmlAccessorType(XmlAccessType
.FIELD
)
36 @XmlType(name
= "Group", propOrder
= {
41 @XmlRootElement(name
= "Group")
43 @Indexed(index
= "eu.etaxonomy.cdm.model.common.Group")
44 @Table(name
= "PermissionGroup")
45 public class Group
extends CdmBase
{
46 private static final long serialVersionUID
= 7216686200093054648L;
47 private static final Logger logger
= Logger
.getLogger(Group
.class);
49 @XmlElement(name
= "Name")
52 protected String name
;
54 @XmlElementWrapper(name
= "Members")
55 @XmlElement(name
= "Member")
57 @XmlSchemaType(name
= "IDREF")
58 @ManyToMany(fetch
= FetchType
.LAZY
, mappedBy
= "groups")
59 protected Set
<User
> members
= new HashSet
<User
>();
61 @XmlElementWrapper(name
= "GrantedAuthorities")
62 @XmlElement(name
= "GrantedAuthority", type
= GrantedAuthorityImpl
.class)
64 @XmlSchemaType(name
= "IDREF")
65 @ManyToMany(fetch
= FetchType
.LAZY
, targetEntity
= GrantedAuthorityImpl
.class)
66 @Cascade({CascadeType
.SAVE_UPDATE
})
67 protected Set
<GrantedAuthority
> grantedAuthorities
= new HashSet
<GrantedAuthority
>();
73 public static Group
NewInstance(){
77 public static Group
NewInstance(String name
){
78 Group group
= Group
.NewInstance();
83 public Set
<GrantedAuthority
> getGrantedAuthorities() {
84 return grantedAuthorities
;
87 public boolean addGrantedAuthority(GrantedAuthority grantedAuthority
){
88 return grantedAuthorities
.add(grantedAuthority
);
91 public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority
){
92 return grantedAuthorities
.remove(grantedAuthority
);
95 public void setName(String name
) {
99 public String
getName() {
103 public Set
<User
> getMembers() {
107 public boolean addMember(User user
) {
108 user
.getGroups().add(this);
109 return this.members
.add(user
);
112 public boolean removeMember(User user
) {
113 if(members
.contains(user
)) {
114 user
.getGroups().remove(this);
115 return this.members
.remove(user
);
120 //*********************** CLONE ********************************************************/
123 * Clones <i>this</i> Group. This is a shortcut that enables to create
124 * a new instance that differs only slightly from <i>this</i> group by
125 * modifying only some of the attributes.
127 * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
128 * @see java.lang.Object#clone()
131 public Object
clone() {
134 result
= (Group
)super.clone();
135 result
.grantedAuthorities
= new HashSet
<GrantedAuthority
>();
136 for (GrantedAuthority grantedauthority
: this.grantedAuthorities
){
137 result
.addGrantedAuthority(grantedauthority
);
140 result
.members
= new HashSet
<User
>();
141 for (User member
: this.members
){
142 result
.addMember(member
);
147 } catch (CloneNotSupportedException e
) {
148 logger
.warn("Object does not implement cloneable");