minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / Group.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.model.common;
10
11 import java.util.HashSet;
12 import java.util.Set;
13 import java.util.UUID;
14
15 import javax.persistence.Entity;
16 import javax.persistence.FetchType;
17 import javax.persistence.ManyToMany;
18 import javax.persistence.Table;
19 import javax.xml.bind.annotation.XmlAccessType;
20 import javax.xml.bind.annotation.XmlAccessorType;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlElementWrapper;
23 import javax.xml.bind.annotation.XmlIDREF;
24 import javax.xml.bind.annotation.XmlRootElement;
25 import javax.xml.bind.annotation.XmlSchemaType;
26 import javax.xml.bind.annotation.XmlType;
27
28 import org.apache.log4j.Logger;
29 import org.hibernate.annotations.Cascade;
30 import org.hibernate.annotations.CascadeType;
31 import org.hibernate.annotations.NaturalId;
32 import org.hibernate.search.annotations.Field;
33 import org.hibernate.search.annotations.Indexed;
34 import org.springframework.security.core.GrantedAuthority;
35
36 @XmlAccessorType(XmlAccessType.FIELD)
37 @XmlType(name = "Group", propOrder = {
38 "name",
39 "members",
40 "grantedAuthorities"
41 })
42 @XmlRootElement(name = "Group")
43 @Entity
44 @Indexed(index = "eu.etaxonomy.cdm.model.common.Group")
45 @Table(name = "PermissionGroup")
46 public class Group extends CdmBase {
47 private static final long serialVersionUID = 7216686200093054648L;
48 private static final Logger logger = Logger.getLogger(Group.class);
49
50 public final static UUID groupEditorUuid = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");
51 public final static UUID groupProjectManagerUuid = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
52
53 @XmlElement(name = "Name")
54 @NaturalId
55 @Field
56 protected String name;
57
58 @XmlElementWrapper(name = "Members")
59 @XmlElement(name = "Member")
60 @XmlIDREF
61 @XmlSchemaType(name = "IDREF")
62 @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
63 @Cascade(CascadeType.REFRESH) // see #2414 (Group updating doesn't work)
64 protected Set<User> members = new HashSet<User>();
65
66 @XmlElementWrapper(name = "GrantedAuthorities")
67 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
68 @XmlIDREF
69 @XmlSchemaType(name = "IDREF")
70 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
71 @Cascade({CascadeType.SAVE_UPDATE})
72 protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
73
74 protected Group(){
75 super();
76 }
77
78 public static Group NewInstance(){
79 return new Group();
80 }
81
82 public static Group NewInstance(String name){
83 Group group = Group.NewInstance();
84 group.setName(name);
85 return group;
86 }
87
88 public Set<GrantedAuthority> getGrantedAuthorities() {
89 return grantedAuthorities;
90 }
91
92 public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
93 return grantedAuthorities.add(grantedAuthority);
94 }
95
96 public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
97 return grantedAuthorities.remove(grantedAuthority);
98 }
99
100 public void setName(String name) {
101 this.name = name;
102 }
103
104 public String getName() {
105 return name;
106 }
107
108 public Set<User> getMembers() {
109 return members;
110 }
111
112 public boolean addMember(User user) {
113 user.getGroups().add(this);
114 return this.members.add(user);
115 }
116
117 public boolean removeMember(User user) {
118 if(members.contains(user)) {
119 user.getGroups().remove(this);
120 return this.members.remove(user);
121 } else {
122 return false;
123 }
124 }
125 //*********************** CLONE ********************************************************/
126
127 /**
128 * Clones <i>this</i> Group. This is a shortcut that enables to create
129 * a new instance that differs only slightly from <i>this</i> group by
130 * modifying only some of the attributes.
131 *
132 * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
133 * @see java.lang.Object#clone()
134 */
135 @Override
136 public Object clone() {
137 Group result;
138 try{
139 result = (Group)super.clone();
140 result.grantedAuthorities = new HashSet<GrantedAuthority>();
141 for (GrantedAuthority grantedauthority: this.grantedAuthorities){
142 result.addGrantedAuthority(grantedauthority);
143 }
144
145 result.members = new HashSet<User>();
146 for (User member: this.members){
147 result.addMember(member);
148 }
149
150 //no changes to name
151 return result;
152 } catch (CloneNotSupportedException e) {
153 logger.warn("Object does not implement cloneable");
154 e.printStackTrace();
155 return null;
156
157 }
158
159
160 }
161
162 }