1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.ldap;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.springframework.ldap.core.ContextMapper;
22 import org.springframework.ldap.core.DirContextOperations;
23 import org.springframework.ldap.core.support.AbstractContextMapper;
24
25 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
26 import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
27 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
28 import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
29 import org.kuali.rice.kim.api.identity.entity.Entity;
30 import org.kuali.rice.kim.api.identity.name.EntityName;
31 import org.kuali.rice.kim.api.identity.principal.Principal;
32 import org.kuali.rice.kim.util.Constants;
33
34 import static org.apache.commons.lang.StringUtils.contains;
35
36
37
38
39
40 public class EntityMapper extends AbstractContextMapper {
41 private Constants constants;
42
43 private ContextMapper affiliationMapper;
44 private ContextMapper entityTypeMapper;
45 private ContextMapper defaultNameMapper;
46 private ContextMapper employmentMapper;
47
48 public Object doMapFromContext(DirContextOperations context) {
49
50 final String entityId = context.getStringAttribute(getConstants().getKimLdapIdProperty());
51 final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
52
53 final Entity.Builder person = Entity.Builder.create();
54 person.setId(entityId);
55
56 if (entityId == null) {
57 throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes "
58 + context.getAttributes());
59 }
60
61 person.setAffiliations(new ArrayList<EntityAffiliation.Builder>());
62 person.setExternalIdentifiers(new ArrayList<EntityExternalIdentifier.Builder>());
63
64 final EntityExternalIdentifier.Builder externalId = EntityExternalIdentifier.Builder.create();
65 externalId.setExternalIdentifierTypeCode(getConstants().getTaxExternalIdTypeCode());
66 externalId.setExternalId(entityId);
67 person.getExternalIdentifiers().add(externalId);
68
69 person.setAffiliations((List<EntityAffiliation.Builder>) getAffiliationMapper().mapFromContext(context));
70
71 person.setEntityTypes(new ArrayList<EntityTypeContactInfo.Builder>());
72 person.getEntityTypeContactInfos().add((EntityTypeContactInfo.Builder) getEntityTypeMapper().mapFromContext(context));
73
74 final List<EntityName.Builder> names = new ArrayList<EntityName.Builder>();
75 final EntityName.Builder name = (EntityName.Builder) getDefaultNameMapper().mapFromContext(context);
76 name.setDefaultValue(true);
77 person.getNames().add(name);
78 person.setId(entityId);
79
80 final EntityEmployment.Builder employmentInfo = (EntityEmployment.Builder) getEmploymentMapper().mapFromContext(context);
81 final EntityAffiliation.Builder employeeAffiliation = getAffiliation(getConstants().getEmployeeAffiliationCodes(), person);
82
83
84 if (employeeAffiliation != null && employmentInfo != null) {
85 employeeAffiliation.getAffiliationType().setEmploymentAffiliationType(true);
86 employmentInfo.setEntityAffiliation(employeeAffiliation);
87 person.getEmploymentInformation().add(employmentInfo);
88 }
89
90 person.setPrincipals(new ArrayList<Principal.Builder>());
91 person.setActive(true);
92
93 final Principal.Builder defaultPrincipal = Principal.Builder.create(principalName);
94 defaultPrincipal.setPrincipalId(entityId);
95 defaultPrincipal.setEntityId(entityId);
96
97 person.getPrincipals().add(defaultPrincipal);
98
99 return person;
100 }
101
102
103
104
105
106
107
108
109 protected EntityAffiliation.Builder getAffiliation(String affiliationCodes, Entity.Builder person) {
110 EntityAffiliation.Builder retval = null;
111 for (EntityAffiliation.Builder affil : person.getAffiliations()) {
112 if (contains(affiliationCodes, affil.getAffiliationType().getCode())) {
113 return affil;
114 }
115 }
116 return retval;
117 }
118
119
120
121
122
123
124 public final Constants getConstants() {
125 return this.constants;
126 }
127
128
129
130
131
132
133 public final void setConstants(final Constants argConstants) {
134 this.constants = argConstants;
135 }
136
137
138
139
140
141 public final ContextMapper getAffiliationMapper() {
142 return this.affiliationMapper;
143 }
144
145
146
147
148
149
150 public final void setAffiliationMapper(final ContextMapper argAffiliationMapper) {
151 this.affiliationMapper = argAffiliationMapper;
152 }
153
154
155
156
157
158
159 public final ContextMapper getEntityTypeMapper() {
160 return this.entityTypeMapper;
161 }
162
163
164
165
166
167
168 public final void setEntityTypeMapper(final ContextMapper argEntityTypeMapper) {
169 this.entityTypeMapper = argEntityTypeMapper;
170 }
171
172
173
174
175
176
177 public final ContextMapper getDefaultNameMapper() {
178 return this.defaultNameMapper;
179 }
180
181
182
183
184
185
186 public final void setDefaultNameMapper(final ContextMapper argDefaultNameMapper) {
187 this.defaultNameMapper = argDefaultNameMapper;
188 }
189
190
191
192
193
194
195 public final ContextMapper getEmploymentMapper() {
196 return this.employmentMapper;
197 }
198
199
200
201
202
203
204 public final void setEmploymentMapper(final ContextMapper argEmploymentMapper) {
205 this.employmentMapper = argEmploymentMapper;
206 }
207 }