root/trunk/cdmlib/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/Group.java

Revision 12103, 4.4 kB (checked in by n.hoffmann, 12 months ago)

Fixes #2372

  • Property svn:keywords set to Id
Line 
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*/
9package eu.etaxonomy.cdm.model.common;
10
11import java.util.HashSet;
12import java.util.Set;
13
14import javax.persistence.Entity;
15import javax.persistence.FetchType;
16import javax.persistence.ManyToMany;
17import javax.persistence.Table;
18import javax.xml.bind.annotation.XmlAccessType;
19import javax.xml.bind.annotation.XmlAccessorType;
20import javax.xml.bind.annotation.XmlElement;
21import javax.xml.bind.annotation.XmlElementWrapper;
22import javax.xml.bind.annotation.XmlIDREF;
23import javax.xml.bind.annotation.XmlRootElement;
24import javax.xml.bind.annotation.XmlSchemaType;
25import javax.xml.bind.annotation.XmlType;
26
27import org.apache.log4j.Logger;
28import org.hibernate.annotations.Cascade;
29import org.hibernate.annotations.CascadeType;
30import org.hibernate.annotations.NaturalId;
31import org.hibernate.search.annotations.Field;
32import org.hibernate.search.annotations.Index;
33import org.hibernate.search.annotations.Indexed;
34import 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")
46public 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}
Note: See TracBrowser for help on using the browser.