View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Maps LDAP Information to KIM Entity Affiliation
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // Create an empty array to prevent NPE
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       * Returns the affiliation type code for the given affiliation name. Returns null if the affiliation is not found
96       * @param affiliationName
97       * @return null if no matching affiliation is found
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 }