/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

package com.example.dao;


import javax.naming.NamingException;

import org.apache.directory.shared.ldap.entry.Entry;

import com.example.domain.InetOrgPerson;


public class InetOrgPersonDao extends GenericLdapDao<InetOrgPerson>
{
    public InetOrgPersonDao()
    {
        super.server = "localhost";
        super.port = 12345;
        super.name = "uid=admin,ou=system";
        super.credentials = "secret";
        super.rdnAttribute = "uid";
        super.parentDn = "ou=users,dc=example,dc=com";
        super.attributes = new String[]
            { "objectClass", "uid", "cn", "sn", "givenName", "mail", "telephoneNumber", "userPassword" };
        super.objectClasses = new String[]
            { "inetOrgPerson", "organizationalPerson", "person", "top" };
    }


    protected InetOrgPerson mapFromEntry( Entry entry )
    {
        InetOrgPerson person = new InetOrgPerson();
        person.setUid( getString( entry, "uid" ) );
        person.setFullName( getString( entry, "cn" ) );
        person.setLastName( getString( entry, "sn" ) );
        person.setFirstName( getString( entry, "givenName" ) );
        person.setPassword( getBytes( entry, "userPassword" ) );
        person.setTelephoneNumbers( getStringSet( entry, "telephoneNumber" ) );
        return person;
    }


    protected void mapToEntry( InetOrgPerson person, Entry entry ) throws NamingException
    {
        setString( entry, "uid", person.getUid() );
        setString( entry, "cn", person.getFullName() );
        setString( entry, "sn", person.getLastName() );
        setString( entry, "givenName", person.getFirstName() );
        setBytes( entry, "userPassword", person.getPassword() );
        for ( String telephoneNumber : person.getTelephoneNumbers() )
        {
            setString( entry, "telephoneNumber", telephoneNumber );
        }
    }

}
