Project

General

Profile

Download (6.31 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
    @Cascade({CascadeType.REFRESH, CascadeType.MERGE}) // see #2414 (Group updating doesn't work)
93
    protected Set<User> members = new HashSet<User>();
94

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

    
103
// ********************* CONSTRUCTOR ************************/
104

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

    
109
// *************** METHODS ***********************************/
110

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

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

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

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

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

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

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

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

    
149
//*********************** CLONE ********************************************************/
150

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

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

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

    
183
//************************************** toString ***************************************
184

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