Coverage Report - org.kuali.rice.kim.ldap.EntityAffiliationMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityAffiliationMapper
0%
0/43
0%
0/20
3.4
 
 1  
 /**
 2  
  * Copyright 2005-2011 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 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  
  * Maps LDAP Information to KIM Entity Affiliation
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
         // Create an empty array to prevent NPE
 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  
      * Returns the affiliation type code for the given affiliation name. Returns null if the affiliation is not found
 89  
      * @param affiliationName
 90  
      * @return null if no matching affiliation is found
 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  
      * Gets the value of constants
 113  
      *
 114  
      * @return the value of constants
 115  
      */
 116  
     public final Constants getConstants() {
 117  0
         return this.constants;
 118  
     }
 119  
 
 120  
     /**
 121  
      * Sets the value of constants
 122  
      *
 123  
      * @param argConstants Value to assign to this.constants
 124  
      */
 125  
     public final void setConstants(final Constants argConstants) {
 126  0
         this.constants = argConstants;
 127  0
     }
 128  
 }