001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.ldap;
017
018import static org.apache.commons.lang.StringUtils.contains;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
024import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
025import org.kuali.rice.kim.api.identity.entity.Entity;
026import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
027import org.kuali.rice.kim.api.identity.name.EntityName;
028import org.kuali.rice.kim.api.identity.principal.Principal;
029import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
030import org.springframework.ldap.core.DirContextOperations;
031
032/**
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class EntityMapper extends BaseMapper<Entity> {
037
038    private EntityAffiliationMapper affiliationMapper;
039    private EntityTypeContactInfoMapper entityTypeContactInfoMapper;
040    private EntityNameMapper defaultNameMapper;
041    private EntityEmploymentMapper employmentMapper;
042    
043    @Override
044    Entity mapDtoFromContext(DirContextOperations context) {
045        Entity.Builder builder = mapBuilderFromContext(context);
046        return builder != null ? builder.build() : null;
047    }
048    
049    Entity.Builder mapBuilderFromContext(DirContextOperations context) {
050        
051        final String entityId      = context.getStringAttribute(getConstants().getKimLdapIdProperty());
052        final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
053
054        final Entity.Builder person = Entity.Builder.create();
055        person.setId(entityId);
056        
057        if (entityId == null) {
058            throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes " 
059                                                 + context.getAttributes());
060        }
061        
062        person.setAffiliations(new ArrayList<EntityAffiliation.Builder>());
063        person.setExternalIdentifiers(new ArrayList<EntityExternalIdentifier.Builder>());
064        
065        final EntityExternalIdentifier.Builder externalId = EntityExternalIdentifier.Builder.create();
066        externalId.setExternalIdentifierTypeCode(getConstants().getTaxExternalIdTypeCode());
067        externalId.setExternalId(entityId);
068        person.getExternalIdentifiers().add(externalId);
069        
070        person.setAffiliations(getAffiliationMapper().mapBuilderFromContext(context));
071        
072        person.setEntityTypes(new ArrayList<EntityTypeContactInfo.Builder>());
073        person.getEntityTypeContactInfos().add(getEntityTypeContactInfoMapper().mapBuilderFromContext(context));
074        
075        final List<EntityName.Builder> names = new ArrayList<EntityName.Builder>();
076        final EntityName.Builder name = getDefaultNameMapper().mapBuilderFromContext(context);
077        names.add(name);
078        name.setDefaultValue(true);
079        person.setNames(names);
080        person.setId(entityId);
081        
082        final EntityEmployment.Builder employmentInfo = (EntityEmployment.Builder) getEmploymentMapper().mapFromContext(context);
083        final EntityAffiliation.Builder employeeAffiliation = getAffiliation(getConstants().getEmployeeAffiliationCodes(), person);
084        
085        //only add employee information if we have an employee affiliation, otherwise ignore
086        if (employeeAffiliation != null && employmentInfo != null) {
087            employeeAffiliation.getAffiliationType().setEmploymentAffiliationType(true);
088            employmentInfo.setEntityAffiliation(employeeAffiliation);
089            person.getEmploymentInformation().add(employmentInfo);
090        }
091        
092        person.setPrincipals(new ArrayList<Principal.Builder>());
093        person.setActive(true);
094        
095        final Principal.Builder defaultPrincipal = Principal.Builder.create(principalName);
096        defaultPrincipal.setPrincipalId(entityId);
097        defaultPrincipal.setEntityId(entityId);
098        
099        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}