]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/directory/ldap/LdapHierarchyUnit.java
Improve ACR attribute typing.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / directory / ldap / LdapHierarchyUnit.java
1 package org.argeo.cms.directory.ldap;
2
3 import java.util.Locale;
4
5 import javax.naming.ldap.LdapName;
6 import javax.naming.ldap.Rdn;
7
8 import org.argeo.api.cms.directory.HierarchyUnit;
9
10 /** LDIF/LDAP based implementation of {@link HierarchyUnit}. */
11 public class LdapHierarchyUnit extends DefaultLdapEntry implements HierarchyUnit {
12 // private final boolean functional;
13
14 private final Type type;
15
16 public LdapHierarchyUnit(AbstractLdapDirectory directory, LdapName dn) {
17 super(directory, dn);
18
19 Rdn rdn = LdapNameUtils.getLastRdn(dn);
20 if (directory.getUserBaseRdn().equals(rdn))
21 type = Type.PEOPLE;
22 else if (directory.getGroupBaseRdn().equals(rdn))
23 type = Type.GROUPS;
24 else if (directory.getSystemRoleBaseRdn().equals(rdn))
25 type = Type.ROLES;
26 else
27 type = Type.FUNCTIONAL;
28 // functional = !(directory.getUserBaseRdn().equals(rdn) || directory.getGroupBaseRdn().equals(rdn)
29 // || directory.getSystemRoleBaseRdn().equals(rdn));
30 }
31
32 @Override
33 public HierarchyUnit getParent() {
34 return getDirectoryDao().doGetHierarchyUnit(LdapNameUtils.getParent(getDn()));
35 }
36
37 @Override
38 public Iterable<HierarchyUnit> getDirectHierarchyUnits(boolean functionalOnly) {
39 return getDirectoryDao().doGetDirectHierarchyUnits(getDn(), functionalOnly);
40 }
41
42 @Override
43 public HierarchyUnit getDirectChild(Type type) {
44 return switch (type) {
45 case ROLES ->
46 getDirectoryDao().doGetHierarchyUnit((LdapName) getDn().add(getDirectory().getSystemRoleBaseRdn()));
47 case PEOPLE -> getDirectoryDao().doGetHierarchyUnit((LdapName) getDn().add(getDirectory().getUserBaseRdn()));
48 case GROUPS -> getDirectoryDao().doGetHierarchyUnit((LdapName) getDn().add(getDirectory().getGroupBaseRdn()));
49 case FUNCTIONAL -> throw new IllegalArgumentException("Type must be a technical type");
50 };
51 }
52
53 @Override
54 public boolean isType(Type type) {
55 return this.type.equals(type);
56 }
57
58 @Override
59 public String getHierarchyUnitName() {
60 String name = LdapNameUtils.getLastRdnValue(getDn());
61 // TODO check ou, o, etc.
62 return name;
63 }
64
65 @Override
66 public String getHierarchyUnitLabel(Locale locale) {
67 String key = LdapNameUtils.getLastRdn(getDn()).getType();
68 Object value = LdapEntry.getLocalized(getProperties(), key, locale);
69 if (value == null)
70 value = getHierarchyUnitName();
71 assert value != null;
72 return value.toString();
73 }
74
75 @Override
76 public String getBase() {
77 return getDn().toString();
78 }
79
80 @Override
81 public String toString() {
82 return "Hierarchy Unit " + getDn().toString();
83 }
84
85 }