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 org.kuali.rice.kim.api.identity.CodedAttribute;
019import org.kuali.rice.kim.api.identity.name.EntityName;
020import org.springframework.ldap.core.DirContextOperations;
021
022/**
023 * 
024 * @author Kuali Rice Team (rice.collab@kuali.org)
025 */
026public class EntityNameMapper extends BaseMapper<EntityName> {
027
028        @Override
029    EntityName mapDtoFromContext(DirContextOperations context) {
030        return mapDtoFromContext(context, true);
031    }
032        
033    EntityName mapDtoFromContext(DirContextOperations context, boolean isdefault) {
034        EntityName.Builder builder = mapBuilderFromContext(context, isdefault);
035        return builder != null ? builder.build() : null;
036    }
037
038    EntityName.Builder mapBuilderFromContext(DirContextOperations context) {
039        return mapBuilderFromContext(context, true);
040    }
041    
042    EntityName.Builder mapBuilderFromContext(DirContextOperations context, boolean isdefault) {        
043        final EntityName.Builder person = EntityName.Builder.create();
044        person.setEntityId(context.getStringAttribute(getConstants().getKimLdapIdProperty()));
045        person.setId(context.getStringAttribute(getConstants().getKimLdapIdProperty()));
046        
047        final String fullName = (String) context.getStringAttribute(getConstants().getGivenNameLdapProperty());
048        
049        if (fullName != null) {
050            final String[] name = fullName.split(" ");
051            person.setFirstName(name[0]);
052            if (name.length > 1) {
053                person.setMiddleName(name[1]);
054            }
055        }
056        else {
057            person.setFirstName(fullName);
058        }
059        
060        person.setLastName(context.getStringAttribute(getConstants().getSnLdapProperty()));
061        person.setDefaultValue(isdefault);
062        person.setActive(true);
063        person.setNameType(CodedAttribute.Builder.create("PRI"));
064        
065        return person;
066    }
067    
068}