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 import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
20 import static org.kuali.rice.core.util.BufferedLogger.debug;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
27 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
28 import org.springframework.ldap.core.DirContextOperations;
29
30
31
32
33
34
35 public class EntityAffiliationMapper extends BaseMapper<List<EntityAffiliation>> {
36
37 @Override
38 List<EntityAffiliation> mapDtoFromContext(DirContextOperations context) {
39 List<EntityAffiliation.Builder> builders = mapBuilderFromContext(context);
40 List<EntityAffiliation> affiliations = new ArrayList<EntityAffiliation>();
41 if (builders != null) {
42 for (EntityAffiliation.Builder builder : builders) {
43 affiliations.add(builder.build());
44 }
45 }
46 return affiliations;
47 }
48
49 List<EntityAffiliation.Builder> mapBuilderFromContext(DirContextOperations context) {
50 List<EntityAffiliation.Builder> retval = new ArrayList<EntityAffiliation.Builder>();
51 final String primaryAffiliationProperty = getConstants().getPrimaryAffiliationLdapProperty();
52 final String affiliationProperty = getConstants().getAffiliationLdapProperty();
53 debug("Got affiliation ", context.getStringAttribute(primaryAffiliationProperty));
54 debug("Got affiliation ", context.getStringAttribute(affiliationProperty));
55
56 String primaryAffiliation = context.getStringAttribute(primaryAffiliationProperty);
57
58 int affiliationId = 1;
59 String affiliationCode = getAffiliationTypeCodeForName(primaryAffiliation);
60
61 final EntityAffiliation.Builder aff1 = EntityAffiliation.Builder.create();
62 aff1.setAffiliationType(EntityAffiliationType.Builder.create(affiliationCode == null ? "AFLT" : affiliationCode));
63 aff1.setCampusCode(getConstants().getDefaultCampusCode());
64 aff1.setId("" + affiliationId++);
65 aff1.setDefaultValue(true);
66 aff1.setActive(true);
67 retval.add(aff1);
68
69 String[] affiliations = context.getStringAttributes(affiliationProperty);
70
71 if (affiliations == null) {
72 affiliations = new String[] {};
73 }
74
75 for (String affiliation : affiliations) {
76 if (!StringUtils.equals(affiliation, primaryAffiliation)) {
77 affiliationCode = getAffiliationTypeCodeForName(affiliation);
78 if (affiliationCode != null && !hasAffiliation(retval, affiliationCode)) {
79 final EntityAffiliation.Builder aff = EntityAffiliation.Builder.create();
80 aff.setAffiliationType(EntityAffiliationType.Builder.create(affiliationCode));
81 aff.setCampusCode(getConstants().getDefaultCampusCode());
82 aff.setId("" + affiliationId++);
83 aff.setDefaultValue(false);
84 aff.setActive(true);
85 retval.add(aff);
86 }
87 }
88 }
89
90 return retval;
91 }
92
93
94
95
96
97
98
99 protected String getAffiliationTypeCodeForName(String affiliationName) {
100 String[] mappings = getConstants().getAffiliationMappings().split(",");
101 for (String affilMap : mappings) {
102 if (contains(affilMap, affiliationName)) {
103 return affilMap.split("=")[1];
104 }
105 }
106 return null;
107 }
108
109 protected boolean hasAffiliation(List<EntityAffiliation.Builder> affiliations, String affiliationCode) {
110 for (EntityAffiliation.Builder affiliation : affiliations) {
111 if (equalsIgnoreCase(affiliation.getAffiliationType().getCode(), affiliationCode)) {
112 return true;
113 }
114 }
115 return false;
116 }
117
118 }