001/**
002 * Copyright 2005-2014 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 java.util.ArrayList;
019
020import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
021import org.kuali.rice.kim.api.identity.entity.EntityDefault;
022import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifier;
023import org.kuali.rice.kim.api.identity.principal.Principal;
024import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
025import org.springframework.ldap.core.DirContextOperations;
026
027/**
028 * 
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class EntityDefaultMapper extends BaseMapper<EntityDefault> {
032
033        private EntityAffiliationMapper affiliationMapper;
034    private EntityTypeContactInfoDefaultMapper entityTypeContactInfoDefaultMapper;
035    private EntityNameMapper defaultNameMapper;
036    private EntityEmploymentMapper employmentMapper;
037    
038    @Override
039    EntityDefault mapDtoFromContext(DirContextOperations context) {
040        EntityDefault.Builder builder = mapBuilderFromContext(context);
041        return builder != null ? builder.build() : null;
042    }
043    
044    EntityDefault.Builder mapBuilderFromContext(DirContextOperations context) {
045        
046        final String entityId = context.getStringAttribute(getConstants().getKimLdapIdProperty());
047        final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
048
049        final EntityDefault.Builder person = EntityDefault.Builder.create(entityId);
050        
051        if (entityId == null) {
052            throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes " 
053                                                 + context.getAttributes());
054        }
055        
056        person.setAffiliations(new ArrayList<EntityAffiliation.Builder>());
057        person.setExternalIdentifiers(new ArrayList<EntityExternalIdentifier.Builder>());
058        
059        final EntityExternalIdentifier.Builder externalId = EntityExternalIdentifier.Builder.create();
060        externalId.setExternalIdentifierTypeCode(getConstants().getTaxExternalIdTypeCode());
061        externalId.setExternalId(entityId);
062        person.getExternalIdentifiers().add(externalId);
063        
064        person.setAffiliations(getAffiliationMapper().mapBuilderFromContext(context));
065        
066        person.setEntityTypeContactInfos(new ArrayList<EntityTypeContactInfoDefault.Builder>());
067        person.getEntityTypeContactInfos().add(getEntityTypeContactInfoDefaultMapper().mapBuilderFromContext(context));
068        
069        person.setName(getDefaultNameMapper().mapBuilderFromContext(context));
070        person.setEntityId(entityId);
071        
072        person.setEmployment(getEmploymentMapper().mapBuilderFromContext(context));
073
074        person.setEntityId(entityId);
075        person.setPrincipals(new ArrayList<Principal.Builder>()); 
076        //inactivate unless we find a matching affiliation
077        person.setActive(true);
078        
079        final Principal.Builder defaultPrincipal = Principal.Builder.create(principalName);
080        defaultPrincipal.setPrincipalId(entityId);
081        defaultPrincipal.setEntityId(entityId);
082
083        person.getPrincipals().add(defaultPrincipal);
084        
085        return person;
086    }
087    
088    /**
089     * Gets the value of affiliationMapper
090     *
091     * @return the value of affiliationMapper
092     */
093    public final EntityAffiliationMapper getAffiliationMapper() {
094        return this.affiliationMapper;
095    }
096
097    /**
098     * Sets the value of affiliationMapper
099     *
100     * @param argAffiliationMapper Value to assign to this.affiliationMapper
101     */
102    public final void setAffiliationMapper(final EntityAffiliationMapper argAffiliationMapper) {
103        this.affiliationMapper = argAffiliationMapper;
104    }
105
106    /**
107     * Gets the value of entityTypeMapper
108     *
109     * @return the value of entityTypeMapper
110     */
111    public final EntityTypeContactInfoDefaultMapper getEntityTypeContactInfoDefaultMapper() {
112        return this.entityTypeContactInfoDefaultMapper;
113    }
114
115    /**
116     * Sets the value of entityTypeMapper
117     *
118     * @param argEntityTypeMapper Value to assign to this.entityTypeMapper
119     */
120    public final void setEntityTypeContactInfoDefaultMapper(final EntityTypeContactInfoDefaultMapper argEntityTypeMapper) {
121        this.entityTypeContactInfoDefaultMapper = argEntityTypeMapper;
122    }
123
124    /**
125     * Gets the value of defaultNameMapper
126     *
127     * @return the value of defaultNameMapper
128     */
129    public final EntityNameMapper getDefaultNameMapper() {
130        return this.defaultNameMapper;
131    }
132
133    /**
134     * Sets the value of defaultNameMapper
135     *
136     * @param argDefaultNameMapper Value to assign to this.defaultNameMapper
137     */
138    public final void setDefaultNameMapper(final EntityNameMapper argDefaultNameMapper) {
139        this.defaultNameMapper = argDefaultNameMapper;
140    }
141
142    /**
143     * Gets the value of employmentMapper
144     *
145     * @return the value of employmentMapper
146     */
147    public final EntityEmploymentMapper getEmploymentMapper() {
148        return this.employmentMapper;
149    }
150
151    /**
152     * Sets the value of employmentMapper
153     *
154     * @param argEmploymentMapper Value to assign to this.employmentMapper
155     */
156    public final void setEmploymentMapper(final EntityEmploymentMapper argEmploymentMapper) {
157        this.employmentMapper = argEmploymentMapper;
158    }
159
160}