1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.persistence.api.dao;
20
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.function.Predicate;
26 import org.apache.syncope.core.persistence.api.entity.Schema;
27 import org.apache.syncope.core.persistence.api.entity.group.Group;
28
29 public class AllowedSchemas<S extends Schema> {
30
31 private final Set<S> forSelf = new HashSet<>();
32
33 private final Map<Group, Set<S>> forMemberships = new HashMap<>();
34
35 public Set<S> getForSelf() {
36 return forSelf;
37 }
38
39 public Set<S> getForMembership(final Group group) {
40 return forMemberships.get(group) == null ? Set.of() : forMemberships.get(group);
41 }
42
43 public Map<Group, Set<S>> getForMemberships() {
44 return forMemberships;
45 }
46
47 public boolean forSelfContains(final S schema) {
48 return forSelf.contains(schema);
49 }
50
51 public boolean forSelfContains(final String schema) {
52 return forSelf.stream().anyMatch(new KeyMatches(schema));
53 }
54
55 public boolean forMembershipsContains(final Group group, final S schema) {
56 return getForMembership(group).stream().anyMatch(s -> s.equals(schema));
57 }
58
59 public boolean forMembershipsContains(final S schema) {
60 return forMemberships.entrySet().stream().
61 anyMatch(entry -> entry.getValue().contains(schema));
62 }
63
64 public boolean forMembershipsContains(final Group group, final String schema) {
65 return getForMembership(group).stream().anyMatch(new KeyMatches(schema));
66 }
67
68 public boolean forMembershipsContains(final String schema) {
69 KeyMatches keyMatches = new KeyMatches(schema);
70
71 return forMemberships.entrySet().stream().
72 anyMatch(entry -> entry.getValue().stream().anyMatch(keyMatches));
73 }
74
75 public boolean contains(final S schema) {
76 if (forSelfContains(schema)) {
77 return true;
78 }
79 return forMembershipsContains(schema);
80 }
81
82 public boolean contains(final String schema) {
83 if (forSelfContains(schema)) {
84 return true;
85 }
86 return forMembershipsContains(schema);
87 }
88
89 private class KeyMatches implements Predicate<S> {
90
91 private final String schema;
92
93 KeyMatches(final String schema) {
94 this.schema = schema;
95 }
96
97 @Override
98 public boolean test(final S object) {
99 return object.getKey().equals(schema);
100 }
101 }
102 }