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