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