Project

General

Profile

Download (5.5 KB) Statistics
| Branch: | Tag: | Revision:
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.validation.constraints.NotNull;
21
import javax.xml.bind.annotation.XmlAccessType;
22
import javax.xml.bind.annotation.XmlAccessorType;
23
import javax.xml.bind.annotation.XmlElement;
24
import javax.xml.bind.annotation.XmlElementWrapper;
25
import javax.xml.bind.annotation.XmlIDREF;
26
import javax.xml.bind.annotation.XmlRootElement;
27
import javax.xml.bind.annotation.XmlSchemaType;
28
import javax.xml.bind.annotation.XmlType;
29

    
30
import org.apache.log4j.Logger;
31
import org.hibernate.annotations.Cascade;
32
import org.hibernate.annotations.CascadeType;
33
import org.hibernate.search.annotations.Field;
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 disabled to reduce clutter in indexes, since this type is not used by any search
45
//@Indexed(index = "eu.etaxonomy.cdm.model.common.Group")
46
@Table(name = "PermissionGroup")
47
public class Group extends CdmBase {
48
    private static final long serialVersionUID = 7216686200093054648L;
49
    private static final Logger logger = Logger.getLogger(Group.class);
50

    
51
    public final static UUID GROUP_EDITOR_UUID = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");
52
    public final static UUID GROUP_PROJECT_MANAGER_UUID = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
53
    public final static UUID GROUP_PUBLISHER_UUID = UUID.fromString("c1f20ad8-1782-40a7-b06b-ce4773acb5ea");
54
    public final static UUID GROUP_ADMIN_UUID = UUID.fromString("1739df71-bf73-4dc6-8320-aaaf72cb555f");
55

    
56
//*********************** FACTORY *********************/
57

    
58
    public static Group NewInstance(){
59
        return new Group();
60
    }
61

    
62
    public static Group NewInstance(String name){
63
        Group group = Group.NewInstance();
64
        group.setName(name);
65
        return group;
66
    }
67

    
68
//**************** FIELDS ******************************/
69

    
70
    @XmlElement(name = "Name")
71
    @Column(unique = true)
72
    @Field
73
    @NotNull
74
    protected String name;
75

    
76
    @XmlElementWrapper(name = "Members")
77
    @XmlElement(name = "Member")
78
    @XmlIDREF
79
    @XmlSchemaType(name = "IDREF")
80
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
81
    @Cascade({CascadeType.REFRESH, CascadeType.MERGE}) // see #2414 (Group updating doesn't work)
82
    protected Set<User> members = new HashSet<User>();
83

    
84
    @XmlElementWrapper(name = "GrantedAuthorities")
85
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
86
    @XmlIDREF
87
    @XmlSchemaType(name = "IDREF")
88
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
89
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
90
    protected Set <GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
91

    
92
// ********************* CONSTRUCTOR ************************/
93

    
94
    protected Group(){
95
        super();
96
    }
97

    
98
// *************** METHODS ***********************************/
99

    
100
    public Set<GrantedAuthority> getGrantedAuthorities() {
101
        return grantedAuthorities;
102
    }
103

    
104
    public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
105
        return grantedAuthorities.add(grantedAuthority);
106
    }
107

    
108
    public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
109
        return grantedAuthorities.remove(grantedAuthority);
110
    }
111

    
112
    public void setName(String name) {
113
        this.name = name;
114
    }
115

    
116
    public String getName() {
117
        return name;
118
    }
119

    
120
    public Set<User> getMembers() {
121
        return members;
122
    }
123

    
124
    public boolean addMember(User user) {
125
        user.getGroups().add(this);
126
        return this.members.add(user);
127
    }
128

    
129
    public boolean removeMember(User user) {
130
        if(members.contains(user)) {
131
            user.getGroups().remove(this);
132
            return this.members.remove(user);
133
        } else {
134
            return false;
135
        }
136
    }
137
//*********************** CLONE ********************************************************/
138

    
139
    /**
140
     * Clones <i>this</i> Group. This is a shortcut that enables to create
141
     * a new instance that differs only slightly from <i>this</i> group by
142
     * modifying only some of the attributes.
143
     *
144
     * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
145
     * @see java.lang.Object#clone()
146
     */
147
    @Override
148
    public Object clone() {
149
        Group result;
150
        try{
151
            result = (Group)super.clone();
152
            result.grantedAuthorities = new HashSet<GrantedAuthority>();
153
            for (GrantedAuthority grantedauthority: this.grantedAuthorities){
154
                result.addGrantedAuthority(grantedauthority);
155
            }
156

    
157
            result.members = new HashSet<User>();
158
            for (User member: this.members){
159
                result.addMember(member);
160
            }
161

    
162
            //no changes to name
163
            return result;
164
        } catch (CloneNotSupportedException e) {
165
            logger.warn("Object does not implement cloneable");
166
            e.printStackTrace();
167
            return null;
168
        }
169
    }
170
}
(14-14/77)