| 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.Index; |
|---|
| 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 | @XmlElement(name = "Name") |
|---|
| 51 | @NaturalId |
|---|
| 52 | @Field(index = Index.UN_TOKENIZED) |
|---|
| 53 | protected String name; |
|---|
| 54 | |
|---|
| 55 | @XmlElementWrapper(name = "Members") |
|---|
| 56 | @XmlElement(name = "Member") |
|---|
| 57 | @XmlIDREF |
|---|
| 58 | @XmlSchemaType(name = "IDREF") |
|---|
| 59 | @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups") |
|---|
| 60 | protected Set<User> members = new HashSet<User>(); |
|---|
| 61 | |
|---|
| 62 | @XmlElementWrapper(name = "GrantedAuthorities") |
|---|
| 63 | @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class) |
|---|
| 64 | @XmlIDREF |
|---|
| 65 | @XmlSchemaType(name = "IDREF") |
|---|
| 66 | @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class) |
|---|
| 67 | @Cascade({CascadeType.SAVE_UPDATE}) |
|---|
| 68 | protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>(); |
|---|
| 69 | |
|---|
| 70 | protected Group(){ |
|---|
| 71 | super(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | public static Group NewInstance(){ |
|---|
| 75 | return new Group(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public static Group NewInstance(String name){ |
|---|
| 79 | Group group = Group.NewInstance(); |
|---|
| 80 | group.setName(name); |
|---|
| 81 | return group; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public Set<GrantedAuthority> getGrantedAuthorities() { |
|---|
| 85 | return grantedAuthorities; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){ |
|---|
| 89 | return grantedAuthorities.add(grantedAuthority); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){ |
|---|
| 93 | return grantedAuthorities.remove(grantedAuthority); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public void setName(String name) { |
|---|
| 97 | this.name = name; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public String getName() { |
|---|
| 101 | return name; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | public Set<User> getMembers() { |
|---|
| 105 | return members; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | public boolean addMember(User user) { |
|---|
| 109 | user.getGroups().add(this); |
|---|
| 110 | return this.members.add(user); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | public boolean removeMember(User user) { |
|---|
| 114 | if(members.contains(user)) { |
|---|
| 115 | user.getGroups().remove(this); |
|---|
| 116 | return this.members.remove(user); |
|---|
| 117 | } else { |
|---|
| 118 | return false; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | //*********************** CLONE ********************************************************/ |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Clones <i>this</i> Group. This is a shortcut that enables to create |
|---|
| 125 | * a new instance that differs only slightly from <i>this</i> group by |
|---|
| 126 | * modifying only some of the attributes. |
|---|
| 127 | * |
|---|
| 128 | * @see eu.etaxonomy.cdm.model.common.TermBase#clone() |
|---|
| 129 | * @see java.lang.Object#clone() |
|---|
| 130 | */ |
|---|
| 131 | @Override |
|---|
| 132 | public Object clone() { |
|---|
| 133 | Group result; |
|---|
| 134 | try{ |
|---|
| 135 | result = (Group)super.clone(); |
|---|
| 136 | result.grantedAuthorities = new HashSet<GrantedAuthority>(); |
|---|
| 137 | for (GrantedAuthority grantedauthority: this.grantedAuthorities){ |
|---|
| 138 | result.addGrantedAuthority(grantedauthority); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | result.members = new HashSet<User>(); |
|---|
| 142 | for (User member: this.members){ |
|---|
| 143 | result.addMember(member); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | //no changes to name |
|---|
| 147 | return result; |
|---|
| 148 | } catch (CloneNotSupportedException e) { |
|---|
| 149 | logger.warn("Object does not implement cloneable"); |
|---|
| 150 | e.printStackTrace(); |
|---|
| 151 | return null; |
|---|
| 152 | |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | } |
|---|