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