Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.model.core.auth

File Role.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
24% of files have more coverage

Code metrics

20
41
18
1
157
129
28
0.68
2.28
18
1.56

Classes

Class Line # Actions
Role 14 41 28
0.696202569.6%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    package io.sunshower.model.core.auth;
2   
3    import io.sunshower.model.core.Schemata;
4    import io.sunshower.persistence.core.DistributableEntity;
5    import java.util.*;
6    import javax.persistence.*;
7    import javax.xml.bind.annotation.XmlElement;
8    import javax.xml.bind.annotation.XmlRootElement;
9    import org.springframework.security.core.GrantedAuthority;
10   
11    @Entity
12    @XmlRootElement
13    @Table(name = "ROLE", schema = Schemata.SUNSHOWER)
 
14    public class Role extends DistributableEntity implements GrantedAuthority, Comparable<Role> {
15   
16    @Basic @XmlElement private String authority;
17   
18    @Basic @XmlElement private String description;
19   
20    @ManyToOne(fetch = FetchType.LAZY)
21    @JoinColumn(name = "parent_id")
22    private Role parent;
23   
24    @OneToMany(
25    fetch = FetchType.EAGER,
26    mappedBy = "parent",
27    cascade = CascadeType.ALL,
28    orphanRemoval = true
29    )
30    private Set<Role> children;
31   
32    @ManyToMany(mappedBy = "roles")
33    private Set<User> users;
34   
35    @ManyToMany(cascade = CascadeType.ALL)
36    @JoinTable(
37    name = "roles_to_permissions",
38    schema = Schemata.SUNSHOWER,
39    joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"),
40    inverseJoinColumns = @JoinColumn(name = "permission_id", referencedColumnName = "id")
41    )
42    private Set<Permission> permissions;
43   
 
44  7 toggle public Role() {
45  7 super();
46    }
47   
 
48  7 toggle public Role(String authority) {
49  7 this(authority, null);
50    }
51   
 
52  7 toggle public Role(String authority, String description) {
53  7 this();
54  7 this.authority = authority;
55  7 this.description = description;
56    }
57   
 
58  3 toggle public Set<Role> getChildren() {
59  3 if (children != null) {
60  3 return Collections.unmodifiableSet(children);
61    } else {
62  0 return Collections.emptySet();
63    }
64    }
65   
 
66  2 toggle public Role getParent() {
67  2 return parent;
68    }
69   
 
70  2 toggle public Role setParent(Role parent) {
71  2 this.parent = parent;
72  2 return this;
73    }
74   
 
75  2 toggle public Role addChild(Role child) {
76  2 Objects.requireNonNull(child, "Child must not be null");
77  2 if (this.children == null) {
78  2 this.children = new HashSet<>();
79    }
80  2 this.children.add(child);
81  2 child.setParent(this);
82  2 return this;
83    }
84   
 
85  1 toggle public Role addPermission(final Permission permission) {
86  1 permission.addRole(this);
87  1 if (this.permissions == null) {
88  1 this.permissions = new LinkedHashSet<>();
89    }
90  1 this.permissions.add(permission);
91  1 return this;
92    }
93   
 
94  0 toggle @Override
95    public int compareTo(Role o) {
96  0 return 0;
97    }
98   
 
99  3 toggle @Override
100    public String getAuthority() {
101  3 return authority;
102    }
103   
 
104  0 toggle public void setAuthority(String authority) {
105  0 this.authority = authority;
106    }
107   
 
108  4 toggle public void addUser(User user) {
109  4 if (this.users == null) {
110  4 this.users = new LinkedHashSet<>();
111    }
112  4 this.users.add(user);
113    }
114   
 
115  7 toggle @Override
116    protected void setDefaults() {}
117   
 
118  1 toggle @Override
119    public boolean equals(Object o) {
120  1 if (this == o) return true;
121  0 if (!(o instanceof Role)) return false;
122   
123  0 Role role = (Role) o;
124   
125  0 return authority != null ? authority.equals(role.authority) : role.authority == null;
126    }
127   
 
128  23 toggle @Override
129    public int hashCode() {
130  23 return authority != null ? authority.hashCode() : 0;
131    }
132   
 
133  0 toggle @Override
134    public String toString() {
135  0 return "Role{"
136    + ", authority='"
137    + authority
138    + '\''
139    + ", description='"
140    + description
141    + '\''
142    + '}';
143    }
144   
 
145  1 toggle public void removeUser(User user) {
146  1 if (users != null) {
147  1 users.remove(user);
148    }
149    }
150   
 
151  2 toggle public Set<User> getUsers() {
152  2 if (users == null) {
153  0 return Collections.emptySet();
154    }
155  2 return users;
156    }
157    }