ref #7099 move CRUD and Operation to model
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / rights / 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.rights;
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.validation.constraints.Pattern;
22 import javax.xml.bind.annotation.XmlAccessType;
23 import javax.xml.bind.annotation.XmlAccessorType;
24 import javax.xml.bind.annotation.XmlElement;
25 import javax.xml.bind.annotation.XmlElementWrapper;
26 import javax.xml.bind.annotation.XmlIDREF;
27 import javax.xml.bind.annotation.XmlRootElement;
28 import javax.xml.bind.annotation.XmlSchemaType;
29 import javax.xml.bind.annotation.XmlType;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.log4j.Logger;
33 import org.hibernate.annotations.Cascade;
34 import org.hibernate.annotations.CascadeType;
35 import org.hibernate.search.annotations.Field;
36 import org.springframework.security.core.GrantedAuthority;
37
38 import eu.etaxonomy.cdm.model.common.CdmBase;
39
40 @XmlAccessorType(XmlAccessType.FIELD)
41 @XmlType(name = "Group", propOrder = {
42 "name",
43 "members",
44 "grantedAuthorities"
45 })
46 @XmlRootElement(name = "Group")
47 @Entity
48 //@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
49 //@Indexed(index = "eu.etaxonomy.cdm.model.common.Group")
50 @Table(name = "PermissionGroup")
51 public class Group extends CdmBase {
52 private static final long serialVersionUID = 7216686200093054648L;
53 private static final Logger logger = Logger.getLogger(Group.class);
54
55 public final static UUID GROUP_EDITOR_UUID = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");
56 public final static UUID GROUP_EDITOR_EXTENDED_CREATE_UUID = UUID.fromString("89a7f47f-6f2b-45ac-88d4-a99a4cf29f07");
57 public final static UUID GROUP_PROJECT_MANAGER_UUID = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
58 public final static UUID GROUP_PUBLISHER_UUID = UUID.fromString("c1f20ad8-1782-40a7-b06b-ce4773acb5ea");
59 public final static UUID GROUP_ADMIN_UUID = UUID.fromString("1739df71-bf73-4dc6-8320-aaaf72cb555f");
60
61 public final static String GROUP_EDITOR_NAME = "Editor";
62 /**
63 * This group will in future replace the group Editor, see issue #7150
64 */
65 public final static String GROUP_EDITOR_EXTENDED_CREATE_NAME = "EditorExtendedCreate";
66 public final static String GROUP_PROJECT_MANAGER_NAME = "ProjectManager";
67 public final static String GROUP_ADMIN_NAME = "Admin";
68
69
70 //*********************** FACTORY *********************/
71
72 public static Group NewInstance(){
73 return new Group();
74 }
75
76 public static Group NewInstance(String name){
77 Group group = Group.NewInstance();
78 group.setName(name);
79 return group;
80 }
81
82 //**************** FIELDS ******************************/
83
84 @XmlElement(name = "Name")
85 @Column(unique = true)
86 @Field
87 @NotNull
88 @Pattern(regexp=User.USERNAME_REGEX)
89 protected String name;
90
91 @XmlElementWrapper(name = "Members")
92 @XmlElement(name = "Member")
93 @XmlIDREF
94 @XmlSchemaType(name = "IDREF")
95 @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
96 protected Set<User> members = new HashSet<>();
97
98 @XmlElementWrapper(name = "GrantedAuthorities")
99 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
100 @XmlIDREF
101 @XmlSchemaType(name = "IDREF")
102 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
103 @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
104 protected Set <GrantedAuthority> grantedAuthorities = new HashSet<>();
105
106 // ********************* CONSTRUCTOR ************************/
107
108 protected Group(){
109 super();
110 }
111
112 // *************** METHODS ***********************************/
113
114 public Set<GrantedAuthority> getGrantedAuthorities() {
115 return grantedAuthorities;
116 }
117
118 public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
119 return grantedAuthorities.add(grantedAuthority);
120 }
121
122 public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
123 return grantedAuthorities.remove(grantedAuthority);
124 }
125
126 public void setName(String name) {
127 this.name = name;
128 }
129
130 public String getName() {
131 return name;
132 }
133
134 public Set<User> getMembers() {
135 return members;
136 }
137
138 public boolean addMember(User user) {
139 user.getGroups().add(this);
140 return this.members.add(user);
141 }
142
143 public boolean removeMember(User user) {
144 if(members.contains(user)) {
145 user.getGroups().remove(this);
146 return this.members.remove(user);
147 } else {
148 return false;
149 }
150 }
151
152 //*********************** CLONE ********************************************************/
153
154 /**
155 * Clones <i>this</i> Group. This is a shortcut that enables to create
156 * a new instance that differs only slightly from <i>this</i> group by
157 * modifying only some of the attributes.
158 *
159 * @see eu.etaxonomy.cdm.model.term.TermBase#clone()
160 * @see java.lang.Object#clone()
161 */
162 @Override
163 public Object clone() {
164 Group result;
165 try{
166 result = (Group)super.clone();
167 result.grantedAuthorities = new HashSet<>();
168 for (GrantedAuthority grantedauthority: this.grantedAuthorities){
169 result.addGrantedAuthority(grantedauthority);
170 }
171
172 result.members = new HashSet<>();
173 for (User member: this.members){
174 result.addMember(member);
175 }
176
177 //no changes to name
178 return result;
179 } catch (CloneNotSupportedException e) {
180 logger.warn("Object does not implement cloneable");
181 e.printStackTrace();
182 return null;
183 }
184 }
185
186 //************************************** toString ***************************************
187
188 @Override
189 public String toString() {
190 if (StringUtils.isNotBlank(name)){
191 return name;
192 }else{
193 return super.toString();
194 }
195 }
196 }