1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.common.lib;
20
21 import java.util.Collection;
22 import java.util.Map;
23 import java.util.function.Function;
24 import java.util.stream.Collectors;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.apache.syncope.common.lib.request.AnyCR;
27 import org.apache.syncope.common.lib.request.AnyObjectCR;
28 import org.apache.syncope.common.lib.request.GroupCR;
29 import org.apache.syncope.common.lib.request.UserCR;
30 import org.apache.syncope.common.lib.to.AnyObjectTO;
31 import org.apache.syncope.common.lib.to.AnyTO;
32 import org.apache.syncope.common.lib.to.GroupTO;
33 import org.apache.syncope.common.lib.to.LinkedAccountTO;
34 import org.apache.syncope.common.lib.to.MembershipTO;
35 import org.apache.syncope.common.lib.to.RelationshipTO;
36 import org.apache.syncope.common.lib.to.UserTO;
37
38 public final class EntityTOUtils {
39
40 public static Map<String, Attr> buildAttrMap(final Collection<Attr> attrs) {
41 return attrs.stream().collect(Collectors.toUnmodifiableMap(
42 Attr::getSchema, Function.identity(), (exist, repl) -> repl));
43 }
44
45 public static Map<Pair<String, String>, RelationshipTO> buildRelationshipMap(
46 final Collection<RelationshipTO> relationships) {
47
48 return relationships.stream().collect(Collectors.toUnmodifiableMap(
49 rel -> Pair.of(rel.getType(), rel.getOtherEndKey()), Function.identity(), (exist, repl) -> repl));
50 }
51
52 public static Map<String, MembershipTO> buildMembershipMap(final Collection<MembershipTO> memberships) {
53 return memberships.stream().collect(Collectors.toUnmodifiableMap(
54 MembershipTO::getGroupKey, Function.identity(), (exist, repl) -> repl));
55 }
56
57 public static Map<Pair<String, String>, LinkedAccountTO> buildLinkedAccountMap(
58 final Collection<LinkedAccountTO> accounts) {
59
60 return accounts.stream().collect(Collectors.toUnmodifiableMap(
61 account -> Pair.of(account.getResource(), account.getConnObjectKeyValue()),
62 Function.identity(),
63 (exist, repl) -> repl));
64 }
65
66 public static <A extends AnyTO, C extends AnyCR> void toAnyCR(final A anyTO, final C anyCR) {
67 anyCR.setRealm(anyTO.getRealm());
68 anyCR.getAuxClasses().addAll(anyTO.getAuxClasses());
69 anyCR.getPlainAttrs().addAll(anyTO.getPlainAttrs());
70 anyCR.getVirAttrs().addAll(anyTO.getVirAttrs());
71 anyCR.getResources().addAll(anyTO.getResources());
72
73 if (anyCR instanceof UserCR && anyTO instanceof UserTO) {
74 UserCR userCR = (UserCR) anyCR;
75 UserTO userTO = (UserTO) anyTO;
76
77 userCR.setUsername(userTO.getUsername());
78 userCR.setPassword(userTO.getPassword());
79 userCR.setSecurityQuestion(userTO.getSecurityQuestion());
80 userCR.setSecurityAnswer(userTO.getSecurityAnswer());
81 userCR.setMustChangePassword(userTO.isMustChangePassword());
82 userCR.getRelationships().addAll(userTO.getRelationships());
83 userCR.getMemberships().addAll(userTO.getMemberships());
84 userCR.getRoles().addAll(userTO.getRoles());
85 } else if (anyCR instanceof GroupCR && anyTO instanceof GroupTO) {
86 GroupCR groupCR = (GroupCR) anyCR;
87 GroupTO groupTO = (GroupTO) anyTO;
88
89 groupCR.setName(groupTO.getName());
90 groupCR.setUserOwner(groupTO.getUserOwner());
91 groupCR.setGroupOwner(groupTO.getGroupOwner());
92 groupCR.setUDynMembershipCond(groupTO.getUDynMembershipCond());
93 groupCR.getADynMembershipConds().putAll(groupTO.getADynMembershipConds());
94 groupCR.getTypeExtensions().addAll(groupTO.getTypeExtensions());
95 } else if (anyCR instanceof AnyObjectCR && anyTO instanceof AnyObjectTO) {
96 AnyObjectCR anyObjectCR = (AnyObjectCR) anyCR;
97 AnyObjectTO anyObjectTO = (AnyObjectTO) anyTO;
98
99 anyObjectCR.setType(anyObjectTO.getType());
100 anyObjectCR.setName(anyObjectTO.getName());
101 anyObjectCR.getRelationships().addAll(anyObjectTO.getRelationships());
102 anyObjectCR.getMemberships().addAll(anyObjectTO.getMemberships());
103 }
104 }
105
106 public static <C extends AnyCR, A extends AnyTO> void toAnyTO(final C anyCR, final A anyTO) {
107 anyTO.setRealm(anyCR.getRealm());
108 anyTO.getAuxClasses().addAll(anyCR.getAuxClasses());
109 anyTO.getPlainAttrs().addAll(anyCR.getPlainAttrs());
110 anyTO.getVirAttrs().addAll(anyCR.getVirAttrs());
111 anyTO.getResources().addAll(anyCR.getResources());
112
113 if (anyTO instanceof UserTO && anyCR instanceof UserCR) {
114 UserTO userTO = (UserTO) anyTO;
115 UserCR userCR = (UserCR) anyCR;
116
117 userTO.setUsername(userCR.getUsername());
118 userTO.setPassword(userCR.getPassword());
119 userTO.setSecurityQuestion(userCR.getSecurityQuestion());
120 userTO.setSecurityAnswer(userCR.getSecurityAnswer());
121 userTO.setMustChangePassword(userCR.isMustChangePassword());
122 userTO.getRelationships().addAll(userCR.getRelationships());
123 userTO.getMemberships().addAll(userCR.getMemberships());
124 userTO.getRoles().addAll(userCR.getRoles());
125 } else if (anyTO instanceof GroupTO && anyCR instanceof GroupCR) {
126 GroupTO groupTO = (GroupTO) anyTO;
127 GroupCR groupCR = (GroupCR) anyCR;
128
129 groupTO.setName(groupCR.getName());
130 groupTO.setUserOwner(groupCR.getUserOwner());
131 groupTO.setGroupOwner(groupCR.getGroupOwner());
132 groupTO.setUDynMembershipCond(groupCR.getUDynMembershipCond());
133 groupTO.getADynMembershipConds().putAll(groupCR.getADynMembershipConds());
134 groupTO.getTypeExtensions().addAll(groupCR.getTypeExtensions());
135 } else if (anyTO instanceof AnyObjectTO && anyCR instanceof AnyObjectCR) {
136 AnyObjectTO anyObjectTO = (AnyObjectTO) anyTO;
137 AnyObjectCR anyObjectCR = (AnyObjectCR) anyCR;
138
139 anyObjectTO.setType(anyObjectCR.getType());
140 anyObjectTO.setName(anyObjectCR.getName());
141 anyObjectTO.getRelationships().addAll(anyObjectCR.getRelationships());
142 anyObjectTO.getMemberships().addAll(anyObjectCR.getMemberships());
143 }
144 }
145
146
147
148
149 private EntityTOUtils() {
150 }
151 }