use strings for recordBasis (#2615 und #3544)
[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
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;
26
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;
34
35 @XmlAccessorType(XmlAccessType.FIELD)
36 @XmlType(name = "Group", propOrder = {
37 "name",
38 "members",
39 "grantedAuthorities"
40 })
41 @XmlRootElement(name = "Group")
42 @Entity
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);
48
49 @XmlElement(name = "Name")
50 @NaturalId
51 @Field
52 protected String name;
53
54 @XmlElementWrapper(name = "Members")
55 @XmlElement(name = "Member")
56 @XmlIDREF
57 @XmlSchemaType(name = "IDREF")
58 @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
59 protected Set<User> members = new HashSet<User>();
60
61 @XmlElementWrapper(name = "GrantedAuthorities")
62 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
63 @XmlIDREF
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>();
68
69 protected Group(){
70 super();
71 }
72
73 public static Group NewInstance(){
74 return new Group();
75 }
76
77 public static Group NewInstance(String name){
78 Group group = Group.NewInstance();
79 group.setName(name);
80 return group;
81 }
82
83 public Set<GrantedAuthority> getGrantedAuthorities() {
84 return grantedAuthorities;
85 }
86
87 public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
88 return grantedAuthorities.add(grantedAuthority);
89 }
90
91 public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
92 return grantedAuthorities.remove(grantedAuthority);
93 }
94
95 public void setName(String name) {
96 this.name = name;
97 }
98
99 public String getName() {
100 return name;
101 }
102
103 public Set<User> getMembers() {
104 return members;
105 }
106
107 public boolean addMember(User user) {
108 user.getGroups().add(this);
109 return this.members.add(user);
110 }
111
112 public boolean removeMember(User user) {
113 if(members.contains(user)) {
114 user.getGroups().remove(this);
115 return this.members.remove(user);
116 } else {
117 return false;
118 }
119 }
120 //*********************** CLONE ********************************************************/
121
122 /**
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.
126 *
127 * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
128 * @see java.lang.Object#clone()
129 */
130 @Override
131 public Object clone() {
132 Group result;
133 try{
134 result = (Group)super.clone();
135 result.grantedAuthorities = new HashSet<GrantedAuthority>();
136 for (GrantedAuthority grantedauthority: this.grantedAuthorities){
137 result.addGrantedAuthority(grantedauthority);
138 }
139
140 result.members = new HashSet<User>();
141 for (User member: this.members){
142 result.addMember(member);
143 }
144
145 //no changes to name
146 return result;
147 } catch (CloneNotSupportedException e) {
148 logger.warn("Object does not implement cloneable");
149 e.printStackTrace();
150 return null;
151
152 }
153
154
155 }
156
157 }