1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.user2;
20
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.Set;
24
25 import org.mycore.common.MCRSessionMgr;
26 import org.mycore.datamodel.classifications2.MCRCategory;
27 import org.mycore.datamodel.classifications2.MCRLabel;
28
29 import jakarta.xml.bind.annotation.XmlAttribute;
30 import jakarta.xml.bind.annotation.XmlElement;
31 import jakarta.xml.bind.annotation.XmlRootElement;
32
33
34
35
36
37
38
39
40 @XmlRootElement(name = "role")
41 public class MCRRole {
42
43
44 private String name;
45
46
47 private HashMap<String, MCRLabel> labels;
48
49 private boolean isSystemRole;
50
51 private MCRRole() {
52 this.labels = new HashMap<>();
53 }
54
55
56
57
58
59
60
61 public MCRRole(String name, Set<MCRLabel> labels) {
62 this();
63 for (MCRLabel label : labels) {
64 this.labels.put(label.getLang(), label);
65 }
66 setName(name);
67 }
68
69
70
71
72
73
74 @XmlAttribute
75 public String getName() {
76 return name;
77 }
78
79
80
81
82 public MCRLabel getLabel() {
83 String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage();
84 return labels.get(lang);
85 }
86
87
88
89
90 public Collection<MCRLabel> getLabels() {
91 return labels.values();
92 }
93
94
95
96
97
98
99 public boolean isSystemRole() {
100 return isSystemRole;
101 }
102
103 @XmlElement(name = "label")
104 private MCRLabel[] getLabelsArray() {
105 return labels.values().toArray(new MCRLabel[labels.size()]);
106 }
107
108 @SuppressWarnings("unused")
109 private void setLabelsArray(MCRLabel[] labels) {
110 for (MCRLabel label : labels) {
111 this.labels.put(label.getLang(), label);
112 }
113 }
114
115 private void setName(String name) {
116 this.name = name;
117 this.isSystemRole = !name.contains(":") || name.startsWith(MCRUser2Constants.ROLE_CLASSID.getRootID() + ":");
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (obj instanceof MCRRole) {
123 return ((MCRRole) obj).getName().equals(this.getName());
124 } else {
125 return false;
126 }
127 }
128
129 @Override
130 public int hashCode() {
131 return name.hashCode();
132 }
133
134 @Override
135 public String toString() {
136 return getName();
137 }
138 }