+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy\r
+* http://www.e-taxonomy.eu\r
+*\r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
package eu.etaxonomy.cdm.model.common;\r
\r
import java.util.HashSet;\r
import java.util.Set;\r
+import java.util.UUID;\r
\r
+import javax.persistence.Column;\r
import javax.persistence.Entity;\r
import javax.persistence.FetchType;\r
import javax.persistence.ManyToMany;\r
import javax.persistence.Table;\r
+import javax.xml.bind.annotation.XmlAccessType;\r
+import javax.xml.bind.annotation.XmlAccessorType;\r
+import javax.xml.bind.annotation.XmlElement;\r
+import javax.xml.bind.annotation.XmlElementWrapper;\r
+import javax.xml.bind.annotation.XmlIDREF;\r
+import javax.xml.bind.annotation.XmlRootElement;\r
+import javax.xml.bind.annotation.XmlSchemaType;\r
+import javax.xml.bind.annotation.XmlType;\r
\r
-import org.hibernate.annotations.NaturalId;\r
-import org.springframework.security.GrantedAuthority;\r
+import org.apache.log4j.Logger;\r
+import org.hibernate.annotations.Cascade;\r
+import org.hibernate.annotations.CascadeType;\r
+import org.hibernate.search.annotations.Field;\r
+import org.hibernate.search.annotations.Indexed;\r
+import org.springframework.security.core.GrantedAuthority;\r
\r
+@XmlAccessorType(XmlAccessType.FIELD)\r
+@XmlType(name = "Group", propOrder = {\r
+ "name",\r
+ "members",\r
+ "grantedAuthorities"\r
+})\r
+@XmlRootElement(name = "Group")\r
@Entity\r
+@Indexed(index = "eu.etaxonomy.cdm.model.common.Group")\r
@Table(name = "PermissionGroup")\r
public class Group extends CdmBase {\r
- \r
- /**\r
- * \r
- */\r
- private static final long serialVersionUID = 7216686200093054648L;\r
- \r
- @NaturalId\r
- protected String name;\r
- \r
- @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")\r
- protected Set<User> members = new HashSet<User>();\r
- \r
- @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)\r
- protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();\r
- \r
- public Set<GrantedAuthority> getGrantedAuthorities() {\r
- return grantedAuthorities;\r
- }\r
- \r
- public void setName(String name) {\r
- this.name = name;\r
- }\r
- \r
- public String getName() {\r
- return name;\r
- }\r
- \r
- public Set<User> getMembers() {\r
- return members;\r
- }\r
-\r
- public boolean addMember(User user) {\r
- user.getGroups().add(this);\r
- return this.members.add(user);\r
- }\r
-\r
- public boolean removeMember(User user) {\r
- if(members.contains(user)) {\r
- user.getGroups().remove(this);\r
- return this.members.remove(user);\r
- } else {\r
- return false;\r
- }\r
- }\r
+ private static final long serialVersionUID = 7216686200093054648L;\r
+ private static final Logger logger = Logger.getLogger(Group.class);\r
+\r
+ public final static UUID groupEditorUuid = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");\r
+ public final static UUID groupProjectManagerUuid = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");\r
+\r
+//*********************** FACTORY *********************/\r
+\r
+ public static Group NewInstance(){\r
+ return new Group();\r
+ }\r
+\r
+ public static Group NewInstance(String name){\r
+ Group group = Group.NewInstance();\r
+ group.setName(name);\r
+ return group;\r
+ }\r
+ \r
+//**************** FIELDS ******************************/ \r
+ \r
+ @XmlElement(name = "Name")\r
+ @Column(unique = true)\r
+ @Field\r
+ protected String name;\r
+\r
+ @XmlElementWrapper(name = "Members")\r
+ @XmlElement(name = "Member")\r
+ @XmlIDREF\r
+ @XmlSchemaType(name = "IDREF")\r
+ @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")\r
+ @Cascade(CascadeType.REFRESH) // see #2414 (Group updating doesn't work)\r
+ protected Set<User> members = new HashSet<User>();\r
+\r
+ @XmlElementWrapper(name = "GrantedAuthorities")\r
+ @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)\r
+ @XmlIDREF\r
+ @XmlSchemaType(name = "IDREF")\r
+ @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)\r
+ @Cascade({CascadeType.SAVE_UPDATE})\r
+ protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();\r
+\r
+// ********************* CONSTRUCTOR ************************/ \r
+ \r
+ protected Group(){\r
+ super();\r
+ }\r
+\r
+// *************** METHODS ***********************************/ \r
+\r
+ public Set<GrantedAuthority> getGrantedAuthorities() {\r
+ return grantedAuthorities;\r
+ }\r
+\r
+ public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){\r
+ return grantedAuthorities.add(grantedAuthority);\r
+ }\r
+\r
+ public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){\r
+ return grantedAuthorities.remove(grantedAuthority);\r
+ }\r
+\r
+ public void setName(String name) {\r
+ this.name = name;\r
+ }\r
+\r
+ public String getName() {\r
+ return name;\r
+ }\r
+\r
+ public Set<User> getMembers() {\r
+ return members;\r
+ }\r
+\r
+ public boolean addMember(User user) {\r
+ user.getGroups().add(this);\r
+ return this.members.add(user);\r
+ }\r
+\r
+ public boolean removeMember(User user) {\r
+ if(members.contains(user)) {\r
+ user.getGroups().remove(this);\r
+ return this.members.remove(user);\r
+ } else {\r
+ return false;\r
+ }\r
+ }\r
+//*********************** CLONE ********************************************************/\r
+\r
+ /**\r
+ * Clones <i>this</i> Group. This is a shortcut that enables to create\r
+ * a new instance that differs only slightly from <i>this</i> group by\r
+ * modifying only some of the attributes.\r
+ *\r
+ * @see eu.etaxonomy.cdm.model.common.TermBase#clone()\r
+ * @see java.lang.Object#clone()\r
+ */\r
+ @Override\r
+ public Object clone() {\r
+ Group result;\r
+ try{\r
+ result = (Group)super.clone();\r
+ result.grantedAuthorities = new HashSet<GrantedAuthority>();\r
+ for (GrantedAuthority grantedauthority: this.grantedAuthorities){\r
+ result.addGrantedAuthority(grantedauthority);\r
+ }\r
+\r
+ result.members = new HashSet<User>();\r
+ for (User member: this.members){\r
+ result.addMember(member);\r
+ }\r
+\r
+ //no changes to name\r
+ return result;\r
+ } catch (CloneNotSupportedException e) {\r
+ logger.warn("Object does not implement cloneable");\r
+ e.printStackTrace();\r
+ return null;\r
+\r
+ }\r
+\r
+\r
+ }\r
+\r
}\r