001/**
002 * Copyright 2005-2015 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.name.EntityName;
019import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
020import org.springframework.ldap.core.ContextMapper;
021import org.springframework.ldap.core.DirContextOperations;
022
023import javax.naming.NamingException;
024
025/**
026 * 
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029public class EntityNamePrincipalNameMapper extends BaseMapper<EntityNamePrincipalName> {
030
031    private ContextMapper defaultNameMapper;
032    
033    @Override
034    EntityNamePrincipalName mapDtoFromContext(DirContextOperations context) {
035        EntityNamePrincipalName.Builder builder = mapBuilderFromContext(context);
036        return builder != null ? builder.build() : null;
037    }
038    
039    EntityNamePrincipalName.Builder mapBuilderFromContext(DirContextOperations context) {
040        final EntityNamePrincipalName.Builder person = EntityNamePrincipalName.Builder.create();
041
042        try {
043            person.setDefaultName((EntityName.Builder) getDefaultNameMapper().mapFromContext(context));
044        } catch (NamingException e) {
045            e.printStackTrace();
046            throw new RuntimeException(e.getMessage(), e);
047        }
048
049        person.setPrincipalName(context.getStringAttribute(getConstants().getKimLdapNameProperty()));
050        return person;
051    }
052
053    /**
054     * Gets the value of defaultNameMapper
055     *
056     * @return the value of defaultNameMapper
057     */
058    public final ContextMapper getDefaultNameMapper() {
059        return this.defaultNameMapper;
060    }
061
062    /**
063     * Sets the value of defaultNameMapper
064     *
065     * @param argDefaultNameMapper Value to assign to this.defaultNameMapper
066     */
067    public final void setDefaultNameMapper(final ContextMapper argDefaultNameMapper) {
068        this.defaultNameMapper = argDefaultNameMapper;
069    }
070}