Project

General

Profile

Download (7.32 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.permission;
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.validation.constraints.Pattern;
22
import javax.xml.bind.annotation.XmlAccessType;
23
import javax.xml.bind.annotation.XmlAccessorType;
24
import javax.xml.bind.annotation.XmlElement;
25
import javax.xml.bind.annotation.XmlElementWrapper;
26
import javax.xml.bind.annotation.XmlIDREF;
27
import javax.xml.bind.annotation.XmlRootElement;
28
import javax.xml.bind.annotation.XmlSchemaType;
29
import javax.xml.bind.annotation.XmlType;
30

    
31
import org.apache.commons.lang3.StringUtils;
32
import org.apache.log4j.Logger;
33
import org.hibernate.annotations.Cascade;
34
import org.hibernate.annotations.CascadeType;
35
import org.hibernate.search.annotations.Field;
36
import org.springframework.security.core.GrantedAuthority;
37

    
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39

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

    
56
    public final static UUID GROUP_EDITOR_UUID = UUID.fromString("22e5e8af-b99c-4884-a92f-71978efd3770");
57
    public final static UUID GROUP_EDITOR_EXTENDED_CREATE_UUID = UUID.fromString("89a7f47f-6f2b-45ac-88d4-a99a4cf29f07");
58
    public final static UUID GROUP_PROJECT_MANAGER_UUID = UUID.fromString("645191ae-32a4-4d4e-9b86-c90e0d41944a");
59
    public final static UUID GROUP_PUBLISHER_UUID = UUID.fromString("c1f20ad8-1782-40a7-b06b-ce4773acb5ea");
60
    public final static UUID GROUP_ADMIN_UUID = UUID.fromString("1739df71-bf73-4dc6-8320-aaaf72cb555f");
61
    public static final UUID GROUP_EDITOR_REFERENCE_UUID = UUID.fromString("c3efd156-3a0a-4a5c-a99c-c6262fd734bd");
62
    public static final UUID GROUP_ALLOW_ALL_TAXA_UUID = UUID.fromString("08eadacd-e060-4e96-a40d-75cd495c304a");
63

    
64
    public final static String GROUP_EDITOR_NAME = "Editor";
65
    /**
66
     * This group will in future replace the group Editor, see issue #7150
67
     */
68
    public final static String GROUP_EDITOR_EXTENDED_CREATE_NAME = "EditorExtendedCreate";
69
    public final static String GROUP_PROJECT_MANAGER_NAME = "ProjectManager";
70
    public final static String GROUP_ADMIN_NAME = "Admin";
71
    public static final String GROUP_EDITOR_REFERENCE = "Editor-Reference";
72
    public static final String GROUP_ALLOW_ALL_TAXA_NAME = "Allow_for_all_taxa";
73

    
74
//*********************** FACTORY *********************/
75

    
76
    public static Group NewInstance(){
77
        return new Group();
78
    }
79

    
80
    public static Group NewInstance(String name){
81
        Group group = Group.NewInstance();
82
        group.setName(name);
83
        return group;
84
    }
85

    
86
//**************** FIELDS ******************************/
87

    
88
    @XmlElement(name = "Name")
89
    @Column(unique = true)
90
    @Field
91
    @NotNull
92
    @Pattern(regexp=User.USERNAME_REGEX)
93
    private String name;
94

    
95
    @XmlElementWrapper(name = "Members")
96
    @XmlElement(name = "Member")
97
    @XmlIDREF
98
    @XmlSchemaType(name = "IDREF")
99
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
100
    private Set<User> members = new HashSet<>();
101

    
102
    @XmlElementWrapper(name = "GrantedAuthorities")
103
    @XmlElement(name = "GrantedAuthority", type = GrantedAuthorityImpl.class)
104
    @XmlIDREF
105
    @XmlSchemaType(name = "IDREF")
106
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = GrantedAuthorityImpl.class)
107
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
108
    private Set <GrantedAuthority> grantedAuthorities = new HashSet<>();
109

    
110
    @XmlElementWrapper(name = "Authorities")
111
    @XmlElement(name = "Authority", type = AuthorityBase.class)
112
    @XmlIDREF
113
    @XmlSchemaType(name = "IDREF")
114
    @ManyToMany(fetch = FetchType.LAZY, targetEntity = AuthorityBase.class)
115
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE})
116
    private Set <AuthorityBase> authorities = new HashSet<>();
117

    
118
// ********************* CONSTRUCTOR ************************/
119

    
120
    protected Group(){
121
        super();
122
    }
123

    
124
// *************** GETTER / SETTER ***********************************/
125

    
126
    public Set<GrantedAuthority> getGrantedAuthorities() {
127
        return grantedAuthorities;
128
    }
129
    public boolean addGrantedAuthority(GrantedAuthority grantedAuthority){
130
        return grantedAuthorities.add(grantedAuthority);
131
    }
132
    public boolean removeGrantedAuthority(GrantedAuthority grantedAuthority){
133
        return grantedAuthorities.remove(grantedAuthority);
134
    }
135

    
136
    public Set<AuthorityBase> getAuthorities() {
137
        return authorities;
138
    }
139
    public boolean addAuthority(AuthorityBase authority){
140
        return authorities.add(authority);
141
    }
142
    public boolean removeAuthority(AuthorityBase authority){
143
        return authorities.remove(authority);
144
    }
145

    
146
    public String getName() {return name;}
147
    public void setName(String name) {this.name = name;}
148

    
149

    
150
    public Set<User> getMembers() {
151
        return members;
152
    }
153
    public boolean addMember(User user) {
154
        user.getGroups().add(this);
155
        return this.members.add(user);
156
    }
157
    public boolean removeMember(User user) {
158
        if(members.contains(user)) {
159
            user.getGroups().remove(this);
160
            return this.members.remove(user);
161
        } else {
162
            return false;
163
        }
164
    }
165

    
166
//*********************** CLONE ********************************************************/
167

    
168
    /**
169
     * Clones <i>this</i> Group. This is a shortcut that enables to create
170
     * a new instance that differs only slightly from <i>this</i> group by
171
     * modifying only some of the attributes.
172
     *
173
     * @see eu.etaxonomy.cdm.model.term.TermBase#clone()
174
     * @see java.lang.Object#clone()
175
     */
176
    @Override
177
    public Object clone() {
178
        Group result;
179
        try{
180
            result = (Group)super.clone();
181
            result.grantedAuthorities = new HashSet<>();
182
            for (GrantedAuthority grantedauthority: this.grantedAuthorities){
183
                result.addGrantedAuthority(grantedauthority);
184
            }
185

    
186
            result.members = new HashSet<>();
187
            for (User member: this.members){
188
                result.addMember(member);
189
            }
190

    
191
            //no changes to name
192
            return result;
193
        } catch (CloneNotSupportedException e) {
194
            logger.warn("Object does not implement cloneable");
195
            e.printStackTrace();
196
            return null;
197
        }
198
    }
199

    
200
//************************************** toString ***************************************
201

    
202
    @Override
203
    public String toString() {
204
        if (StringUtils.isNotBlank(name)){
205
            return name;
206
        }else{
207
            return super.toString();
208
        }
209
    }
210
}
(5-5/9)