Project

General

Profile

Download (6.17 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_EDITOR_EXTENDED_CREATE_UUID = UUID.fromString("89a7f47f-6f2b-45ac-88d4-a99a4cf29f07");
54
    public final static UUID GROUP_PROJECT_MANAGER_UUID = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
55
    public final static UUID GROUP_PUBLISHER_UUID = UUID.fromString("c1f20ad8-1782-40a7-b06b-ce4773acb5ea");
56
    public final static UUID GROUP_ADMIN_UUID = UUID.fromString("1739df71-bf73-4dc6-8320-aaaf72cb555f");
57

    
58
    public final static String GROUP_EDITOR_NAME = "Editor";
59
    /**
60
     * This group will in future replace the group Editor, see issue #7150
61
     */
62
    public final static String GROUP_EDITOR_EXTENDED_CREATE_NAME = "EditorExtendedCreate";
63
    public final static String GROUP_PROJECT_MANAGER_NAME = "ProjectManager";
64
    public final static String GROUP_ADMIN_NAME = "Admin";
65

    
66

    
67
//*********************** FACTORY *********************/
68

    
69
    public static Group NewInstance(){
70
        return new Group();
71
    }
72

    
73
    public static Group NewInstance(String name){
74
        Group group = Group.NewInstance();
75
        group.setName(name);
76
        return group;
77
    }
78

    
79
//**************** FIELDS ******************************/
80

    
81
    @XmlElement(name = "Name")
82
    @Column(unique = true)
83
    @Field
84
    @NotNull
85
    protected String name;
86

    
87
    @XmlElementWrapper(name = "Members")
88
    @XmlElement(name = "Member")
89
    @XmlIDREF
90
    @XmlSchemaType(name = "IDREF")
91
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
92
    protected Set<User> members = new HashSet<>();
93

    
94
    @XmlElementWrapper(name = "GrantedAuthorities")
95
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
96
    @XmlIDREF
97
    @XmlSchemaType(name = "IDREF")
98
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
99
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
100
    protected Set <GrantedAuthority> grantedAuthorities = new HashSet<>();
101

    
102
// ********************* CONSTRUCTOR ************************/
103

    
104
    protected Group(){
105
        super();
106
    }
107

    
108
// *************** METHODS ***********************************/
109

    
110
    public Set<GrantedAuthority> getGrantedAuthorities() {
111
        return grantedAuthorities;
112
    }
113

    
114
    public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
115
        return grantedAuthorities.add(grantedAuthority);
116
    }
117

    
118
    public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
119
        return grantedAuthorities.remove(grantedAuthority);
120
    }
121

    
122
    public void setName(String name) {
123
        this.name = name;
124
    }
125

    
126
    public String getName() {
127
        return name;
128
    }
129

    
130
    public Set<User> getMembers() {
131
        return members;
132
    }
133

    
134
    public boolean addMember(User user) {
135
        user.getGroups().add(this);
136
        return this.members.add(user);
137
    }
138

    
139
    public boolean removeMember(User user) {
140
        if(members.contains(user)) {
141
            user.getGroups().remove(this);
142
            return this.members.remove(user);
143
        } else {
144
            return false;
145
        }
146
    }
147

    
148
//*********************** CLONE ********************************************************/
149

    
150
    /**
151
     * Clones <i>this</i> Group. This is a shortcut that enables to create
152
     * a new instance that differs only slightly from <i>this</i> group by
153
     * modifying only some of the attributes.
154
     *
155
     * @see eu.etaxonomy.cdm.model.common.TermBase#clone()
156
     * @see java.lang.Object#clone()
157
     */
158
    @Override
159
    public Object clone() {
160
        Group result;
161
        try{
162
            result = (Group)super.clone();
163
            result.grantedAuthorities = new HashSet<>();
164
            for (GrantedAuthority grantedauthority: this.grantedAuthorities){
165
                result.addGrantedAuthority(grantedauthority);
166
            }
167

    
168
            result.members = new HashSet<>();
169
            for (User member: this.members){
170
                result.addMember(member);
171
            }
172

    
173
            //no changes to name
174
            return result;
175
        } catch (CloneNotSupportedException e) {
176
            logger.warn("Object does not implement cloneable");
177
            e.printStackTrace();
178
            return null;
179
        }
180
    }
181

    
182
//************************************** toString ***************************************
183

    
184
    @Override
185
    public String toString() {
186
        if (StringUtils.isNotBlank(name)){
187
            return name;
188
        }else{
189
            return super.toString();
190
        }
191
    }
192
}
(15-15/83)