merge-update from trunk; added import preference to add media as media specimen
[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.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlElementWrapper;
24 import javax.xml.bind.annotation.XmlIDREF;
25 import javax.xml.bind.annotation.XmlRootElement;
26 import javax.xml.bind.annotation.XmlSchemaType;
27 import javax.xml.bind.annotation.XmlType;
28
29 import org.apache.log4j.Logger;
30 import org.hibernate.annotations.Cascade;
31 import org.hibernate.annotations.CascadeType;
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 //*********************** FACTORY *********************/
54
55 public static Group NewInstance(){
56 return new Group();
57 }
58
59 public static Group NewInstance(String name){
60 Group group = Group.NewInstance();
61 group.setName(name);
62 return group;
63 }
64
65 //**************** FIELDS ******************************/
66
67 @XmlElement(name = "Name")
68 @Column(unique = true)
69 @Field
70 protected String name;
71
72 @XmlElementWrapper(name = "Members")
73 @XmlElement(name = "Member")
74 @XmlIDREF
75 @XmlSchemaType(name = "IDREF")
76 @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
77 @Cascade(CascadeType.REFRESH) // see #2414 (Group updating doesn't work)
78 protected Set<User> members = new HashSet<User>();
79
80 @XmlElementWrapper(name = "GrantedAuthorities")
81 @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
82 @XmlIDREF
83 @XmlSchemaType(name = "IDREF")
84 @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
85 @Cascade({CascadeType.SAVE_UPDATE})
86 protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
87
88 // ********************* CONSTRUCTOR ************************/
89
90 protected Group(){
91 super();
92 }
93
94 // *************** METHODS ***********************************/
95
96 public Set<GrantedAuthority> getGrantedAuthorities() {
97 return grantedAuthorities;
98 }
99
100 public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
101 return grantedAuthorities.add(grantedAuthority);
102 }
103
104 public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
105 return grantedAuthorities.remove(grantedAuthority);
106 }
107
108 public void setName(String name) {
109 this.name = name;
110 }
111
112 public String getName() {
113 return name;
114 }
115
116 public Set<User> getMembers() {
117 return members;
118 }
119
120 public boolean addMember(User user) {
121 user.getGroups().add(this);
122 return this.members.add(user);
123 }
124
125 public boolean removeMember(User user) {
126 if(members.contains(user)) {
127 user.getGroups().remove(this);
128 return this.members.remove(user);
129 } else {
130 return false;
131 }
132 }
133 //*********************** CLONE ********************************************************/
134
135 /**
136 * Clones <i>this</i> Group. This is a shortcut that enables to create
137 * a new instance that differs only slightly from <i>this</i> group by
138 * modifying only some of the attributes.
139 *
140 * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
141 * @see java.lang.Object#clone()
142 */
143 @Override
144 public Object clone() {
145 Group result;
146 try{
147 result = (Group)super.clone();
148 result.grantedAuthorities = new HashSet<GrantedAuthority>();
149 for (GrantedAuthority grantedauthority: this.grantedAuthorities){
150 result.addGrantedAuthority(grantedauthority);
151 }
152
153 result.members = new HashSet<User>();
154 for (User member: this.members){
155 result.addMember(member);
156 }
157
158 //no changes to name
159 return result;
160 } catch (CloneNotSupportedException e) {
161 logger.warn("Object does not implement cloneable");
162 e.printStackTrace();
163 return null;
164
165 }
166
167
168 }
169
170 }