Coverage Report - org.kuali.rice.kim.ldap.EntityMapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityMapper
0%
0/54
0%
0/10
1.583
 
 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.springframework.ldap.core.ContextMapper;
 22  
 import org.springframework.ldap.core.DirContextOperations;
 23  
 import org.springframework.ldap.core.support.AbstractContextMapper;
 24  
 
 25  
 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
 26  
 import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
 27  
 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
 28  
 import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
 29  
 import org.kuali.rice.kim.api.identity.entity.Entity;
 30  
 import org.kuali.rice.kim.api.identity.name.EntityName;
 31  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 32  
 import org.kuali.rice.kim.util.Constants;
 33  
 
 34  
 import static org.apache.commons.lang.StringUtils.contains;
 35  
 
 36  
 /**
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  0
 public class EntityMapper extends AbstractContextMapper {
 41  
     private Constants constants;
 42  
 
 43  
     private ContextMapper affiliationMapper;
 44  
     private ContextMapper entityTypeMapper;
 45  
     private ContextMapper defaultNameMapper;
 46  
     private ContextMapper employmentMapper;
 47  
     
 48  
     public Object doMapFromContext(DirContextOperations context) {
 49  
         
 50  0
         final String entityId      = context.getStringAttribute(getConstants().getKimLdapIdProperty());
 51  0
         final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
 52  
 
 53  0
         final Entity.Builder person = Entity.Builder.create();
 54  0
         person.setId(entityId);
 55  
         
 56  0
         if (entityId == null) {
 57  0
             throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes " 
 58  
                                                  + context.getAttributes());
 59  
         }
 60  
         
 61  0
         person.setAffiliations(new ArrayList<EntityAffiliation.Builder>());
 62  0
         person.setExternalIdentifiers(new ArrayList<EntityExternalIdentifier.Builder>());
 63  
         
 64  0
         final EntityExternalIdentifier.Builder externalId = EntityExternalIdentifier.Builder.create();
 65  0
         externalId.setExternalIdentifierTypeCode(getConstants().getTaxExternalIdTypeCode());
 66  0
         externalId.setExternalId(entityId);
 67  0
         person.getExternalIdentifiers().add(externalId);
 68  
         
 69  0
         person.setAffiliations((List<EntityAffiliation.Builder>) getAffiliationMapper().mapFromContext(context));
 70  
         
 71  0
         person.setEntityTypes(new ArrayList<EntityTypeContactInfo.Builder>());
 72  0
         person.getEntityTypeContactInfos().add((EntityTypeContactInfo.Builder) getEntityTypeMapper().mapFromContext(context));
 73  
         
 74  0
         final List<EntityName.Builder> names = new ArrayList<EntityName.Builder>();
 75  0
         final EntityName.Builder name = (EntityName.Builder) getDefaultNameMapper().mapFromContext(context);
 76  0
         name.setDefaultValue(true);
 77  0
         person.getNames().add(name);
 78  0
         person.setId(entityId);
 79  
         
 80  0
         final EntityEmployment.Builder employmentInfo = (EntityEmployment.Builder) getEmploymentMapper().mapFromContext(context);
 81  0
         final EntityAffiliation.Builder employeeAffiliation = getAffiliation(getConstants().getEmployeeAffiliationCodes(), person);
 82  
         
 83  
         //only add employee information if we have an employee affiliation, otherwise ignore
 84  0
         if (employeeAffiliation != null && employmentInfo != null) {
 85  0
             employeeAffiliation.getAffiliationType().setEmploymentAffiliationType(true);
 86  0
             employmentInfo.setEntityAffiliation(employeeAffiliation);
 87  0
             person.getEmploymentInformation().add(employmentInfo);
 88  
         }
 89  
         
 90  0
         person.setPrincipals(new ArrayList<Principal.Builder>());
 91  0
         person.setActive(true);
 92  
         
 93  0
         final Principal.Builder defaultPrincipal = Principal.Builder.create(principalName);
 94  0
         defaultPrincipal.setPrincipalId(entityId);
 95  0
         defaultPrincipal.setEntityId(entityId);
 96  
         
 97  0
         person.getPrincipals().add(defaultPrincipal);
 98  
         
 99  0
         return person;
 100  
     }
 101  
     
 102  
     /**
 103  
      * 
 104  
      * Finds and returns affiliation id of the persons affiliation that matches the affilication code
 105  
      * @param affiliationCode
 106  
      * @param person
 107  
      * @return
 108  
      */
 109  
     protected EntityAffiliation.Builder getAffiliation(String affiliationCodes, Entity.Builder person) {
 110  0
         EntityAffiliation.Builder retval = null;
 111  0
         for (EntityAffiliation.Builder affil : person.getAffiliations()) {
 112  0
             if (contains(affiliationCodes, affil.getAffiliationType().getCode())) {
 113  0
                 return affil;
 114  
             }
 115  
         }
 116  0
         return retval;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Gets the value of constants
 121  
      *
 122  
      * @return the value of constants
 123  
      */
 124  
     public final Constants getConstants() {
 125  0
         return this.constants;
 126  
     }
 127  
 
 128  
     /**
 129  
      * Sets the value of constants
 130  
      *
 131  
      * @param argConstants Value to assign to this.constants
 132  
      */
 133  
     public final void setConstants(final Constants argConstants) {
 134  0
         this.constants = argConstants;
 135  0
     }
 136  
     /**
 137  
      * Gets the value of affiliationMapper
 138  
      *
 139  
      * @return the value of affiliationMapper
 140  
      */
 141  
     public final ContextMapper getAffiliationMapper() {
 142  0
         return this.affiliationMapper;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Sets the value of affiliationMapper
 147  
      *
 148  
      * @param argAffiliationMapper Value to assign to this.affiliationMapper
 149  
      */
 150  
     public final void setAffiliationMapper(final ContextMapper argAffiliationMapper) {
 151  0
         this.affiliationMapper = argAffiliationMapper;
 152  0
     }
 153  
 
 154  
     /**
 155  
      * Gets the value of entityTypeMapper
 156  
      *
 157  
      * @return the value of entityTypeMapper
 158  
      */
 159  
     public final ContextMapper getEntityTypeMapper() {
 160  0
         return this.entityTypeMapper;
 161  
     }
 162  
 
 163  
     /**
 164  
      * Sets the value of entityTypeMapper
 165  
      *
 166  
      * @param argEntityTypeMapper Value to assign to this.entityTypeMapper
 167  
      */
 168  
     public final void setEntityTypeMapper(final ContextMapper argEntityTypeMapper) {
 169  0
         this.entityTypeMapper = argEntityTypeMapper;
 170  0
     }
 171  
 
 172  
     /**
 173  
      * Gets the value of defaultNameMapper
 174  
      *
 175  
      * @return the value of defaultNameMapper
 176  
      */
 177  
     public final ContextMapper getDefaultNameMapper() {
 178  0
         return this.defaultNameMapper;
 179  
     }
 180  
 
 181  
     /**
 182  
      * Sets the value of defaultNameMapper
 183  
      *
 184  
      * @param argDefaultNameMapper Value to assign to this.defaultNameMapper
 185  
      */
 186  
     public final void setDefaultNameMapper(final ContextMapper argDefaultNameMapper) {
 187  0
         this.defaultNameMapper = argDefaultNameMapper;
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Gets the value of employmentMapper
 192  
      *
 193  
      * @return the value of employmentMapper
 194  
      */
 195  
     public final ContextMapper getEmploymentMapper() {
 196  0
         return this.employmentMapper;
 197  
     }
 198  
 
 199  
     /**
 200  
      * Sets the value of employmentMapper
 201  
      *
 202  
      * @param argEmploymentMapper Value to assign to this.employmentMapper
 203  
      */
 204  
     public final void setEmploymentMapper(final ContextMapper argEmploymentMapper) {
 205  0
         this.employmentMapper = argEmploymentMapper;
 206  0
     }
 207  
 }