View Javadoc

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  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          final String entityId      = context.getStringAttribute(getConstants().getKimLdapIdProperty());
51          final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
52  
53          final Entity.Builder person = Entity.Builder.create();
54          person.setId(entityId);
55          
56          if (entityId == null) {
57              throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes " 
58                                                   + context.getAttributes());
59          }
60          
61          person.setAffiliations(new ArrayList<EntityAffiliation.Builder>());
62          person.setExternalIdentifiers(new ArrayList<EntityExternalIdentifier.Builder>());
63          
64          final EntityExternalIdentifier.Builder externalId = EntityExternalIdentifier.Builder.create();
65          externalId.setExternalIdentifierTypeCode(getConstants().getTaxExternalIdTypeCode());
66          externalId.setExternalId(entityId);
67          person.getExternalIdentifiers().add(externalId);
68          
69          person.setAffiliations((List<EntityAffiliation.Builder>) getAffiliationMapper().mapFromContext(context));
70          
71          person.setEntityTypes(new ArrayList<EntityTypeContactInfo.Builder>());
72          person.getEntityTypeContactInfos().add((EntityTypeContactInfo.Builder) getEntityTypeMapper().mapFromContext(context));
73          
74          final List<EntityName.Builder> names = new ArrayList<EntityName.Builder>();
75          final EntityName.Builder name = (EntityName.Builder) getDefaultNameMapper().mapFromContext(context);
76          name.setDefaultValue(true);
77          person.getNames().add(name);
78          person.setId(entityId);
79          
80          final EntityEmployment.Builder employmentInfo = (EntityEmployment.Builder) getEmploymentMapper().mapFromContext(context);
81          final EntityAffiliation.Builder employeeAffiliation = getAffiliation(getConstants().getEmployeeAffiliationCodes(), person);
82          
83          //only add employee information if we have an employee affiliation, otherwise ignore
84          if (employeeAffiliation != null && employmentInfo != null) {
85              employeeAffiliation.getAffiliationType().setEmploymentAffiliationType(true);
86              employmentInfo.setEntityAffiliation(employeeAffiliation);
87              person.getEmploymentInformation().add(employmentInfo);
88          }
89          
90          person.setPrincipals(new ArrayList<Principal.Builder>());
91          person.setActive(true);
92          
93          final Principal.Builder defaultPrincipal = Principal.Builder.create(principalName);
94          defaultPrincipal.setPrincipalId(entityId);
95          defaultPrincipal.setEntityId(entityId);
96          
97          person.getPrincipals().add(defaultPrincipal);
98          
99          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         EntityAffiliation.Builder retval = null;
111         for (EntityAffiliation.Builder affil : person.getAffiliations()) {
112             if (contains(affiliationCodes, affil.getAffiliationType().getCode())) {
113                 return affil;
114             }
115         }
116         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         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         this.constants = argConstants;
135     }
136     /**
137      * Gets the value of affiliationMapper
138      *
139      * @return the value of affiliationMapper
140      */
141     public final ContextMapper getAffiliationMapper() {
142         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         this.affiliationMapper = argAffiliationMapper;
152     }
153 
154     /**
155      * Gets the value of entityTypeMapper
156      *
157      * @return the value of entityTypeMapper
158      */
159     public final ContextMapper getEntityTypeMapper() {
160         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         this.entityTypeMapper = argEntityTypeMapper;
170     }
171 
172     /**
173      * Gets the value of defaultNameMapper
174      *
175      * @return the value of defaultNameMapper
176      */
177     public final ContextMapper getDefaultNameMapper() {
178         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         this.defaultNameMapper = argDefaultNameMapper;
188     }
189 
190     /**
191      * Gets the value of employmentMapper
192      *
193      * @return the value of employmentMapper
194      */
195     public final ContextMapper getEmploymentMapper() {
196         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         this.employmentMapper = argEmploymentMapper;
206     }
207 }