View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Represents a role of users.
35   * Roles are {@link MCRCategory} instances and every category from {@link MCRUser2Constants#ROLE_CLASSID}
36   * {@link MCRRole#isSystemRole()}.
37   * 
38   * @author Thomas Scheffler (yagee)
39   */
40  @XmlRootElement(name = "role")
41  public class MCRRole {
42  
43      /** The unique role name */
44      private String name;
45  
46      /** The labels of the role */
47      private HashMap<String, MCRLabel> labels;
48  
49      private boolean isSystemRole;
50  
51      private MCRRole() {
52          this.labels = new HashMap<>();
53      }
54  
55      /**
56       * Creates a new role instance. 
57       * 
58       * @param name the unique role ID
59       * @param labels a set of MCRLabel in different languages
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       * Returns the roles's name
71       * 
72       * @return the roles's name
73       */
74      @XmlAttribute
75      public String getName() {
76          return name;
77      }
78  
79      /**
80       * Returns the label in the current language.
81       */
82      public MCRLabel getLabel() {
83          String lang = MCRSessionMgr.getCurrentSession().getCurrentLanguage();
84          return labels.get(lang);
85      }
86  
87      /**
88       * Returns all labels available for this role. 
89       */
90      public Collection<MCRLabel> getLabels() {
91          return labels.values();
92      }
93  
94      /**
95       * Returns true if this role is a system role.
96       * A system role is every category in {@link MCRUser2Constants#ROLE_CLASSID}. 
97       * @return false if category has not the same root ID as the system role classification.
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 }