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