package mit.roles ;

public class Person
implements roles.Person
{
	private static String KERBEROS_NAME = "KERBEROS_NAME" ;
	private static String MIT_ID = "MIT_ID" ;
	private static String LAST_NAME = "LAST_NAME" ;
	private static String FIRST_NAME = "FIRST_NAME" ;
	private static String EMAIL_ADDR = "EMAIL_ADDR" ;
	private static String DEPT_CODE = "DEPT_CODE" ;
	private static String DEPT_NAME = "DEPT_NAME" ;
	private static String PRIMARY_PERSON_TYPE = "PRIMARY_PERSON_TYPE" ;
	private static String ACTIVE = "ACTIVE" ;
	private static String STATUS_CODE = "STATUS_CODE" ;
	private static String STATUS_DATE = "STATUS_DATE" ;

	private mit.roles.RolesFactory rolesFactory = null ;
	public mit.roles.RolesFactory getRolesFactory() { return rolesFactory ; }
	protected void setRolesFactory( mit.roles.RolesFactory rFactory )
		{ rolesFactory = rFactory ; }

	private java.util.Properties properties = null ;
	public java.util.Properties getProperties() { return this.properties ; }
	private void setProperties( java.util.Properties properties )
		{ this.properties = properties ; }

	public Object getKey() { return getKerberosName() ; }
	public void setKey( Object key )
	{
		setKerberosName( ( String ) key ) ;
	}

	public String getFullName()
	{
		return getFirstName() + " " + getLastName() ;
	}

	public String getKerberosName()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( KERBEROS_NAME ) ;
		}
		return null ;
	}
	private void setKerberosName( String kerberosName )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != kerberosName )
		{
			this.properties.put( KERBEROS_NAME , kerberosName ) ;
		}
	}

	public Number getMitid()
	{
		if( null != this.properties )
		{
			return ( Number ) this.properties.get( MIT_ID ) ;
		}
		return null ;
	}
	public void setMitid( Number mitid )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != mitid )
		{
			this.properties.put( MIT_ID , mitid ) ;
		}
	}

	protected String getLastName()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( LAST_NAME ) ;
		}
		return null ;
	}
	private void setLastName( String lastName )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != lastName )
		{
			this.properties.put( LAST_NAME , lastName ) ;
		}
	}

	protected String getFirstName()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( FIRST_NAME ) ;
		}
		return null ;
	}
	private void setFirstName( String firstName )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != firstName )
		{
			this.properties.put( FIRST_NAME , firstName ) ;
		}
	}

	protected String getEmailAddr()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( EMAIL_ADDR ) ;
		}
		return null ;
	}
	private void setEmailAddr( String emailAddr )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != emailAddr )
		{
			this.properties.put( EMAIL_ADDR , emailAddr ) ;
		}
	}

	protected String getDeptCode()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( DEPT_CODE ) ;
		}
		return null ;
	}
	private void setDeptCode( String deptCode )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != deptCode )
		{
			this.properties.put( DEPT_CODE , deptCode ) ;
		}
	}

	protected String getDeptName()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( DEPT_NAME ) ;
		}
		return null ;
	}
	private void setDeptName( String deptName )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != deptName )
		{
			this.properties.put( DEPT_NAME , deptName ) ;
		}
	}

	protected String getPrimaryPersonType()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( PRIMARY_PERSON_TYPE ) ;
		}
		return null ;
	}
	private void setPrimaryPersonType( String primaryPersonType )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != primaryPersonType )
		{
			this.properties.put( PRIMARY_PERSON_TYPE , primaryPersonType ) ;
		}
	}

	// A(ctive) I(nactive)
	protected String getActive()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( ACTIVE ) ;
		}
		return null ;
	}
	private void setActive( String active )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != active )
		{
			this.properties.put( ACTIVE , active ) ;
		}
	}

	protected String getStatusCode()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( STATUS_CODE ) ;
		}
		return null ;
	}
	private void setStatusCode( String statusCode )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != statusCode )
		{
			this.properties.put( STATUS_CODE , statusCode ) ;
		}
	}

	protected java.sql.Timestamp getStatusDate()
	{
		if( null != this.properties )
		{
			return ( java.sql.Timestamp ) this.properties.get( STATUS_DATE ) ;
		}
		return null ;
	}
	private void setStatusDate( java.sql.Timestamp statusDate )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		if( null != statusDate )
		{
			this.properties.put( STATUS_DATE , statusDate ) ;
		}
	}

	private Boolean foundInRoles = new Boolean( false ) ;
	public Boolean isFoundInRoles()
		throws Exception
	{
		if( ! this.foundInRoles.booleanValue() )
		{
			findInRoles() ;
		}
		return this.foundInRoles ;
	}

	private void findInRoles()
		throws Exception
	{
		if( ( null != getKerberosName() )
		&&  ( null != getRolesFactory() )
		&&  ( null != getRolesFactory().getConnection() )
		)
		{
			sql.Connection connection = getRolesFactory().getConnection() ;
			sql.Table table
				= connection.executeQuery
					( new String
						( "select mit_id, last_name, "
						+ "first_name, email_addr, "
						+ "dept_code, primary_person_type, "
						+ "active, status_code, status_date "
						+ "from person "
						+ "where kerberos_name = "
						+ getKerberosName()
						+ " ;"
						)
					) ;
			if( null != table )
			{
				sql.Rows rows = table.getRows() ;
				if( rows.hasMoreRows() )
				{
					Object[] row = rows.nextRow() ;
					setMitid( ( Number ) row[ rows.getColumn( MIT_ID ) ] ) ;
					setLastName( ( String ) row[ rows.getColumn( LAST_NAME ) ] ) ;
					setFirstName( ( String ) row[ rows.getColumn( FIRST_NAME ) ] ) ;
					setEmailAddr( ( String ) row[ rows.getColumn( EMAIL_ADDR ) ] ) ;
					setDeptCode( ( String ) row[ rows.getColumn( DEPT_CODE ) ] ) ;
					setPrimaryPersonType( ( String ) row[ rows.getColumn( PRIMARY_PERSON_TYPE ) ] ) ;
					setActive( ( String ) row[ rows.getColumn( ACTIVE ) ] ) ;
					setStatusCode( ( String ) row[ rows.getColumn( STATUS_CODE ) ] ) ;
					setStatusDate
						( getRolesFactory().createSqlTimestamp
							( ( String ) row[ rows.getColumn( STATUS_DATE ) ]
							)
						) ;
					this.foundInRoles = new Boolean( true ) ;
					return ;
				}
			}
		}
		this.foundInRoles = new Boolean( false ) ;
	}

	public void refresh()
		throws Exception
	{
		findInRoles() ;
	}

	public Person
		( mit.roles.RolesFactory rolesFactory
		, java.util.Properties properties
		)
	{
		setRolesFactory( rolesFactory ) ;
		if( null != properties )
		{
			setProperties( properties ) ;
		}
		else
		{
			setProperties( new java.util.Properties() ) ;
		}
	}

	public Person
		( mit.roles.RolesFactory rolesFactory
		, String kerberosName
		, Number mitid
		, String lastName
		, String firstName
		, String emailAddr
		, String deptCode
		, String deptName
		, String primaryPersonType
		, String statusCode
		)
	{
		setProperties( new java.util.Properties() ) ;
		setRolesFactory( rolesFactory ) ;
		setKerberosName( kerberosName ) ;
		setMitid( mitid ) ;
		setLastName( lastName ) ;
		setFirstName( firstName ) ;
		setEmailAddr( emailAddr ) ;
		setDeptCode( deptCode ) ;
		setDeptName( deptName ) ;
		setPrimaryPersonType( primaryPersonType  ) ;
		setStatusCode( statusCode ) ;
	}

	public static mit.roles.Person create
		( mit.roles.RolesFactory rolesFactory
		, String kerberosName
		)
	{
		return new mit.roles.Person
			( rolesFactory
			, kerberosName
			, null
			, null
			, null
			, null
			, null
			, null
			, null
			, null
			) ;
	}

}