Project

General

Profile

Download (5.82 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.commons.lang3.StringUtils;
31
import org.apache.log4j.Logger;
32
import org.hibernate.annotations.Cascade;
33
import org.hibernate.annotations.CascadeType;
34
import org.hibernate.search.annotations.Field;
35
import org.springframework.security.core.GrantedAuthority;
36

    
37
@XmlAccessorType(XmlAccessType.FIELD)
38
@XmlType(name = "Group", propOrder = {
39
    "name",
40
    "members",
41
    "grantedAuthorities"
42
})
43
@XmlRootElement(name = "Group")
44
@Entity
45
//@Indexed disabled to reduce clutter in indexes, since this type is not used by any search
46
//@Indexed(index = "eu.etaxonomy.cdm.model.common.Group")
47
@Table(name = "PermissionGroup")
48
public class Group extends CdmBase {
49
    private static final long serialVersionUID = 7216686200093054648L;
50
    private static final Logger logger = Logger.getLogger(Group.class);
51

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

    
57
//*********************** FACTORY *********************/
58

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

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

    
69
//**************** FIELDS ******************************/
70

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

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

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

    
93
// ********************* CONSTRUCTOR ************************/
94

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

    
99
// *************** METHODS ***********************************/
100

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

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

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

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

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

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

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

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

    
139
//*********************** CLONE ********************************************************/
140

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

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

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

    
173
//************************************** toString ***************************************
174

    
175
    @Override
176
    public String toString() {
177
        if (StringUtils.isNotBlank(name)){
178
            return name;
179
        }else{
180
            return super.toString();
181
        }
182
    }
183
}
(14-14/77)