/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed 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 org.bodington.server.realm;

import org.bodington.server.*;
import org.bodington.sqldatabase.*;
import org.bodington.database.*;
import java.util.Enumeration;


public class UserDetail extends org.bodington.sqldatabase.SqlPersistentObject
	{

	/**
	 * Primary key of this entry.
	 */
	private PrimaryKey user_detail_id;

	/**
	 * The user_id of the user this entry refers to.
	 */
	private PrimaryKey user_id;


	private String email_address;
	private Integer dob;
	

	public static UserDetail findUserDetail(User user)
		throws BuildingServerException
		{
		return (UserDetail)findPersistentObject("user_id = "+ user.getPrimaryKey(), UserDetail.class.getName());
		}
	
	public static UserDetail findUserDetail( PrimaryKey key )
	    throws BuildingServerException
	    {
	    return (UserDetail)findPersistentObject( key, "org.bodington.server.realm.UserDetail" );
	    }
	
	public static UserDetail findUserDetail( String where )
	    throws BuildingServerException
	    {
	    return (UserDetail)findPersistentObject( where, "org.bodington.server.realm.UserDetail" );
	    }
	
	public static Enumeration findUserDetails( String where )
	    throws BuildingServerException
	    {
	    return findPersistentObjects( where, "org.bodington.server.realm.UserDetail" );
	    }
	

    public void setPrimaryKey(PrimaryKey key)
	    {
        setUserDetailId( key );
    	}

    public PrimaryKey getPrimaryKey()
    	{
        return getUserDetailId();
    	}

    public void setUserDetailId( PrimaryKey key )
	    {
	    user_detail_id = key;
	    setUnsaved();
	    }

    public PrimaryKey getUserDetailId()
	    {
        return user_detail_id;
	    }
	

    public void setUserId( PrimaryKey key )
	    {
	    user_id = key;
	    setUnsaved();
	    }

    public PrimaryKey getUserId()
	    {
        return user_id;
	    }
	
	public void setUser( User u )
		{
		setUserId( u.getUserId() );
		}
	public User getUser()
		throws BuildingServerException
		{
		return User.findUser( user_id );
		}

		
    public void setEMailAddress( String n )
	    {
	    email_address = n;
	    setUnsaved();
	    }

    public String getEMailAddress()
	    {
        return email_address;
	    }



    public void setDob( Integer b )
	    {
	    dob = b;
	    setUnsaved();
	    }

    public Integer getDob()
	    {
        return dob;
	    }


	public int getDobDay()
		{
		if ( dob == null )
			return -1;
			
		return dob.intValue()%100; 
		}

	public int getDobMonth()
		{
		if ( dob == null )
			return -1;
			
		return (dob.intValue()/100) % 100;
		}

	public int getDobYear()
		{
		if ( dob == null )
			return -1;
			
		return dob.intValue()/10000;
		}

	}
