package mit.roles ;

public class Authorization
implements	roles.Authorization
{
	private final static String LINEFEED = "\n" ;

	// Authorization stored procedures
	private final static String CREATE = "ROLESAPI_CREATE_AUTHORIZATION" ;
	private final static String DELETE = "ROLESAPI_DELETE_AUTHORIZATION" ;
	private final static String UPDATE = "ROLESAPI_UPDATE_AUTHORIZATION" ;
	private final static String IS_USER_AUTHORIZED = "ROLESAPI_IS_USER_AUTHORIZED" ;

	// Authorization field names
	private final static String AUTHORIZATION_ID = "AUTHORIZATION_ID" ;
	private final static String FUNCTION_ID = "FUNCTION_ID" ;
	private final static String QUALIFIER_ID = "QUALIFIER_ID" ;
	private final static String KERBEROS_NAME = "KERBEROS_NAME" ;
	private final static String QUALIFIER_CODE = "QUALIFIER_CODE" ;
	private final static String FUNCTION_NAME = "FUNCTION_NAME" ;
	private final static String FUNCTION_CATEGORY = "FUNCTION_CATEGORY" ;
	private final static String QUALIFIER_NAME = "QUALIFIER_DESCRIPTION" ;
	private final static String MODIFIED_BY = "MODIFIED_BY" ;
	private final static String MODIFIED_DATE = "MODIFIED_DATE" ;
	private final static String DO_FUNCTION = "DO_FUNCTION" ;
	private final static String GRANTANDVIEW = "GRANTANDVIEW" ;
	private final static String DESCEND = "DESCEND" ;
	private final static String EFFECTIVE_DATE = "EFFECTIVE_DATE" ;
	private final static String EXPIRATION_DATE = "EXPIRATION_DATE" ;

	private final static String PERSON = "PERSON" ;
	private final static String FUNCTION = "FUNCTION" ;
	private final static String QUALIFIER = "QUALIFIER" ;
	private final static String GRANT = "GRANT" ;
	private final static String CANFORUSERGRANT = "CANFORUSERGRANT" ;
	private final static String ACTIVENOW = "ACTIVENOW" ;
//	private final static String VIRTUALITY = "VIRTUALITY" ;

	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 ; }
	protected void setProperties( java.util.Properties properties )
		{ this.properties = properties ; }

	public Object getId()
	{
		if( null != this.properties )
		{
			return this.properties.get( AUTHORIZATION_ID ) ;
		}
		return null ;
	}
	protected void setId( Object id )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( AUTHORIZATION_ID , id ) ;
	}

	public roles.Person getPerson()
	{
		if( null != this.properties )
		{
			return ( roles.Person ) this.properties.get( PERSON ) ;
		}
		return null ;
	}
	protected void setPerson( roles.Person person )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( PERSON , person ) ;
	}

	public roles.Function getFunction()
	{
		if( null != this.properties )
		{
			return ( roles.Function ) this.properties.get( FUNCTION ) ;
		}
		return null ;
	}
	protected void setFunction( roles.Function function )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( FUNCTION , function ) ;
	}

	public roles.Qualifier getQualifier()
	{
		if( null != this.properties )
		{
			return ( roles.Qualifier ) this.properties.get( QUALIFIER ) ;
		}
		return null ;
	}
	protected void setQualifier( roles.Qualifier qualifier )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( QUALIFIER , qualifier ) ;
	}

	public roles.GrantPrivilege getGrant()
	{
		if( null != this.properties )
		{
			return ( roles.GrantPrivilege ) this.properties.get( GRANT ) ;
		}
		return null ;
	}
	protected void setGrant( roles.GrantPrivilege grant )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( GRANT , grant ) ;
	}

	public roles.GrantPrivilege getCanForUserGrant()
	{
		if( null != this.properties )
		{
			return ( roles.GrantPrivilege ) this.properties.get( CANFORUSERGRANT ) ;
		}
		return null ;
	}
	protected void setCanForUserGrant( roles.GrantPrivilege canForUserGrant )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( CANFORUSERGRANT , canForUserGrant ) ;
	}

	public Boolean getDoFunction()
	{
		if( null != this.properties )
		{
			return ( Boolean ) this.properties.get( DO_FUNCTION ) ;
		}
		return null ;
	}
	private void setDoFunction( Boolean doFunction )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( DO_FUNCTION , doFunction ) ;
	}

	public java.sql.Timestamp getEffectiveDate()
	{
		if( null != this.properties )
		{
			return ( java.sql.Timestamp ) this.properties.get( EFFECTIVE_DATE ) ;
		}
		return null ;
	}
	protected void setEffectiveDate( java.sql.Timestamp effectiveDate )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( EFFECTIVE_DATE , effectiveDate ) ;
	}

	public java.sql.Timestamp getExpirationDate()
	{
		if( null != this.properties )
		{
			return ( java.sql.Timestamp ) this.properties.get( EXPIRATION_DATE ) ;
		}
		return null ;
	}
	protected void setExpirationDate( java.sql.Timestamp expirationDate )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( EXPIRATION_DATE , expirationDate ) ;
	}

	public Boolean isActiveNow()
	{
		if( null != this.properties )
		{
			return ( Boolean ) this.properties.get( ACTIVENOW ) ;
		}
		return null ;
	}
	protected void setActiveNow( Boolean activeNow )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( ACTIVENOW , activeNow ) ;
	}

	public String getModifiedBy()
	{
		if( null != this.properties )
		{
			return ( String ) this.properties.get( MODIFIED_BY ) ;
		}
		return null ;
	}
	protected void setModifiedBy( String modifiedBy )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( MODIFIED_BY , modifiedBy ) ;
	}

	public java.sql.Timestamp getModifiedDate()
	{
		if( null != this.properties )
		{
			return ( java.sql.Timestamp ) this.properties.get( MODIFIED_DATE ) ;
		}
		return null ;
	}
	protected void setModifiedDate( java.sql.Timestamp modifiedDate )
	{
		if( null == this.properties )
		{
			this.properties = new java.util.Properties() ;
		}
		this.properties.put( MODIFIED_DATE , modifiedDate ) ;
	}

//	public roles.Virtuality getVirtuality()
//	{
//		if( null != this.properties )
//		{
//			return ( roles.Virtuality ) this.properties.get( VIRTUALITY ) ;
//		}
//		return null ;
//	}
//	protected void setVirtuality( mit.roles.Virtuality virtuality )
//	{
//		if( null == this.properties )
//		{
//			this.properties = new java.util.Properties() ;
//		}
//		this.properties.put( VIRTUALITY , virtuality ) ;
//	}

	protected Authorization
		( mit.roles.RolesFactory rolesFactory
		, java.util.Properties properties
		)
	{
		setRolesFactory( rolesFactory ) ;
		setProperties( properties ) ;
	}

	protected Authorization
		( mit.roles.RolesFactory rolesFactory
		, Object id
		, roles.Person person
		, roles.Function function
		, roles.Qualifier qualifier
		, roles.GrantPrivilege grant
		, roles.GrantPrivilege canForUserGrant
		, Boolean doFunction
		, java.sql.Timestamp effectiveDate
		, java.sql.Timestamp expirationDate
		, Boolean activeNow
		, String modifiedBy
		, java.sql.Timestamp modifiedDate
//		, mit.roles.Virtuality virtuality
		)
	{
		setRolesFactory( rolesFactory ) ;
		setId( id ) ;
		setPerson( person ) ;
		setFunction( function ) ;
		setQualifier( qualifier ) ;
		setGrant( grant ) ;
		setCanForUserGrant( canForUserGrant ) ;
		setDoFunction( doFunction ) ;
		setEffectiveDate( effectiveDate ) ;
		setExpirationDate( expirationDate ) ;
		setActiveNow( activeNow ) ;
		setModifiedBy( modifiedBy ) ;
		setModifiedDate( modifiedDate ) ;
//		setVirtuality( virtuality ) ;
	}

	public void delete()
		throws Exception
	{
		try
		{
			if( ( null != getRolesFactory() )
			&&  ( null != getRolesFactory().getConnection() )
			&&  ( null != getId() )
			)
			{
				sql.Connection connection = getRolesFactory().getConnection() ;
				Object[] args = new Object[ 3 ] ;
				args[ 0 ] = getFunction().getName() ;
				args[ 1 ] = getQualifier().getCode() ;
				args[ 2 ]
					= (
						( null != getRolesFactory().getForUser() )
						? ( ( mit.roles.Person ) getRolesFactory().getForUser() ) .getKerberosName()
						: null
					) ;
				int[] columnTypes = new int[ 3 ] ;
				columnTypes[ 0 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 1 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 2 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				int[] dataTypes = new int[ 3 ] ;
				dataTypes[ 0 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 1 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 2 ] = java.sql.Types.VARCHAR ;
				connection.callProcedure
					( DELETE
					, args
					, columnTypes
					, dataTypes
					) ;
				setId( null ) ;
				setPerson( null ) ;
				setFunction( null ) ;
				setQualifier( null ) ;
				setGrant( null ) ;
				setCanForUserGrant( null ) ;
				setDoFunction( null ) ;
				setEffectiveDate( null ) ;
				setExpirationDate( null ) ;
				setActiveNow( null ) ;
				setModifiedBy( null ) ;
				setModifiedDate( null ) ;
				return ;
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException
			( roles.RolesException.DELETEAUTHORIZATIONFAILED ) ;
	}

	public void update
		( roles.Person person
		, roles.Function function
		, roles.Qualifier qualifier
		, roles.GrantPrivilege grant
		, Boolean doFunction
		, java.sql.Timestamp effectiveDate
		, java.sql.Timestamp expirationDate
		)
		throws Exception
	{
		try
		{
			if( ( null != getRolesFactory() )
			&&  ( null != getRolesFactory().getConnection() )
			&&  ( null != getId() )
			)
			{
				sql.Connection connection
					= getRolesFactory().getConnection() ;
				Object[] args = new Object[ 9 ] ;
				args[ 0 ] = getId() ;
				args[ 1 ]
					= ( null != person )
					? ( ( mit.roles.Person ) person ).getKerberosName()
					: ( ( mit.roles.Person ) getPerson() ).getKerberosName() ;
				args[ 2 ]
					= ( null != function )
					? ( ( mit.roles.Function ) function ).getName()
					: ( ( mit.roles.Function ) getFunction() ).getName() ;
				args[ 3 ]
					= ( null != qualifier )
					? ( ( mit.roles.Qualifier ) qualifier ).getCode()
					: ( ( mit.roles.Qualifier ) getQualifier() ).getCode() ;
				args[ 4 ] = doFunction.booleanValue() ? "Y" : "N" ;
				args[ 5 ] = grant.isGrantable() ? "Y" : "N" ;
				args[ 6 ] = effectiveDate ;
				args[ 7 ] = expirationDate ;
				args[ 8 ]
					= (
						( null != getRolesFactory().getForUser() )
						? ( ( mit.roles.Person ) getRolesFactory().getForUser() ).getKerberosName()
						: null
					) ;
				int[] columnTypes = new int[ 11 ] ;
				columnTypes[ 0 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 1 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 2 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 3 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 4 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 5 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 6 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 7 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 8 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 9 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				columnTypes[ 10 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				int[] dataTypes = new int [ 11 ] ;
				dataTypes[ 0 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 1 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 2 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 3 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 4 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 5 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 6 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 7 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 8 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 9 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 10 ] = java.sql.Types.VARCHAR ;

				sql.Table table
					= connection.callProcedure
						( UPDATE
						, args
						, columnTypes
						, dataTypes
						) ;
				if( null != table )
				{
					if( null != person )
						{ setPerson( person ) ; }
					if( null != function )
						{ setFunction( ( mit.roles.Function ) function ) ; }
					if( null != qualifier )
						{ setQualifier( ( mit.roles.Qualifier ) qualifier ) ; }
					if( null != doFunction )
						{ setDoFunction( doFunction ) ; }
					if( null != grant )
						{ setGrant( grant ) ; }
					if( null != effectiveDate )
						{ setEffectiveDate( expirationDate ) ; }
					if( null != expirationDate )
						{ setExpirationDate( expirationDate ) ; }
					sql.Rows rows = table.getRows() ;
					if( rows.hasMoreRows() )
					{
						Object[] row = rows.nextRow() ;
						setModifiedBy( ( String ) row[ 0 ] ) ;
						setModifiedDate
							( getRolesFactory().createSqlTimestamp
								( ( String ) row[ 1 ]
								)
							) ;
					}
					return ;
				}
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException
			( roles.RolesException.UPDATEAUTHORIZATIONFAILED ) ;
	}

	public static roles.Authorization create
		( mit.roles.RolesFactory rolesFactory
		, roles.Person person
		, roles.Function function
		, roles.Qualifier qualifier
		, roles.GrantPrivilege grant
		, Boolean doFunction
		)
		throws Exception
	{
		return create
			( rolesFactory
			, person
			, function
			, qualifier
			, grant
			, doFunction
			, new java.sql.Timestamp( new java.util.Date().getTime() )
			, null
			) ;
	}

	public static roles.Authorization create
		( mit.roles.RolesFactory rolesFactory
		, roles.Person person
		, roles.Function function
		, roles.Qualifier qualifier
		, roles.GrantPrivilege grant
		, Boolean doFunction
		, java.sql.Timestamp effectiveDate
		, java.sql.Timestamp expirationDate
		)
		throws Exception
	{
		try
		{
			if( ( null != rolesFactory )
			&&  ( null != rolesFactory.getConnection() )
			&&  ( null != person ) && ( person instanceof mit.roles.Person )
			&&  ( null != function ) && ( function instanceof mit.roles.Function )
			&&  ( null != qualifier ) && ( qualifier instanceof mit.roles.Qualifier )
			&&  ( null != grant ) && ( grant instanceof mit.roles.GrantPrivilege )
			&&  ( null != doFunction )
			&&  ( null != effectiveDate )
			)
			{
				sql.Connection connection = rolesFactory.getConnection() ;
				Object[] args = new Object[ 9 ] ;
				args[ 0 ] = ( ( mit.roles.Person ) person ).getKerberosName() ;
				args[ 1 ] = ( ( mit.roles.Function ) function ).getFunctionCategory().getCategory() ;
				args[ 2 ] = ( ( mit.roles.Qualifier ) qualifier ).getCode() ;
				args[ 3 ] = doFunction.booleanValue() ? "Y" : "N" ;
				args[ 4 ] = ( ( mit.roles.GrantPrivilege ) grant ).isGrantable() ? "Y" : "N" ;
				args[ 6 ] = rolesFactory.createRolesDate( effectiveDate ) ;
				args[ 7 ] = rolesFactory.createRolesDate( expirationDate ) ;
				args[ 8 ]
					= (
						( null != rolesFactory.getForUser() )
						? ( ( mit.roles.Person ) rolesFactory.getForUser() ).getKerberosName()
						: null
					) ;
				int[] columnTypes = new int[ 12 ] ;
				columnTypes[ 0 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 1 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 2 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 3 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 4 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 5 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 6 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 7 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 8 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 9 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				columnTypes[ 10 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				columnTypes[ 11 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				int[] dataTypes = new int[ 12 ] ;
				dataTypes[ 0 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 1 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 2 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 3 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 4 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 5 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 6 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 7 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 8 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 9 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 10 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 11 ] = java.sql.Types.VARCHAR ;
				sql.Table tab
					= connection.callProcedure
						( CREATE
						, args
						, columnTypes
						, dataTypes
						) ;
				if( null != tab )
				{
					sql.Rows rows = tab.getRows() ;
					if( null != rows )
					{
						if( rows.hasMoreRows() )
						{
							Object[] row = rows.nextRow() ;
							String modBy = ( String ) row[ 0 ] ;
							java.sql.Timestamp modDate
								= rolesFactory.createSqlTimestamp
									( ( String ) row[ 1 ] ) ;
							Number id = ( Number ) row[ 2 ] ;
							sql.Table table
								= connection.executeQuery
									( getAuthorizationDetailSQL( id )
									) ;
							if( null != table )
							{
								function.setId( getFieldFromTable( FUNCTION_ID , table ) ) ;
								qualifier.setId( getFieldFromTable( QUALIFIER_ID , table ) ) ;
								function.setName( ( String ) getFieldFromTable( FUNCTION_NAME , table ) ) ;
								( ( mit.roles.Qualifier ) qualifier ).setDescription
									( ( String ) getFieldFromTable( QUALIFIER_NAME , table ) ) ;
								roles.Authorization authorization
									= new mit.roles.Authorization
										( rolesFactory
										, id
										, person
										, function
										, qualifier
										, grant
										, null //canForUserGrant
										, doFunction
										, effectiveDate
										, expirationDate
										, null //isActiveNow
										, modBy
										, modDate
//										, new mit.roles.Virtuality( mit.roles.Virtuality.REAL )
										) ;
								return( authorization ) ;
							}
						}
					}
				}
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException
			( roles.RolesException.CREATEAUTHORIZATIONFAILED ) ;
	}

	public static Boolean isAuthorized
		( mit.roles.RolesFactory rolesFactory
		, roles.Person person
		, mit.roles.Function function
		, mit.roles.Qualifier qualifier
		)
		throws Exception
	{
		try
		{
			if( null != rolesFactory.getConnection() )
			{
				sql.Connection connection = rolesFactory.getConnection() ;
				Object[] args = new Object[ 4 ] ;
				args[ 0 ] = ( ( mit.roles.Person ) person ).getKerberosName() ;
				args[ 1 ]
					= ( ( mit.roles.FunctionCategory ) function.getFunctionCategory() )
						.getCategory() ;
				args[ 2 ] = function.getName() ;
				args[ 3 ] = qualifier.getCode() ;
				int[] columnTypes = new int[ 5 ] ;
				columnTypes[ 0 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 1 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 2 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 3 ] = java.sql.DatabaseMetaData.procedureColumnIn ;
				columnTypes[ 4 ] = java.sql.DatabaseMetaData.procedureColumnOut ;
				int[] dataTypes = new int[ 5 ] ;
				dataTypes[ 0 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 1 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 2 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 3 ] = java.sql.Types.VARCHAR ;
				dataTypes[ 4 ] = java.sql.Types.VARCHAR ;
				sql.Table table
					= connection.callProcedure
						( IS_USER_AUTHORIZED
						, args
						, columnTypes
						, dataTypes
						) ;
				if( null != table )
				{
					sql.Rows rows = table.getRows() ;
					if( null != rows )
					{
						if( rows.hasMoreRows() )
						{
							Object[] row = rows.nextRow() ;
							return
								( ( ( String ) row[ 0 ] ).equals( "Y" )
								? new Boolean( true )
								: new Boolean( false )
								) ;
						}
					}
				}
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException
			( roles.RolesException.QUERYAUTHORIZATIONFAILED ) ;
	}

	private static String getAuthorizationDetailSQL
		( Number id
		)
		throws Exception
	{
		if( null != id )
		{
			String sql = "select" ;
			sql += "\n function_id, qualifier_id, " ;
			sql += "\n qualifier_name " ;
			sql += "\nfrom authorization " ;
			sql += "\nwhere authorization_id = " + id + " ;" ;
			return sql ;
		}
		throw new roles.RolesException
			( roles.RolesException.NULLARGUMENT ) ;
	}

	private static Object getFieldFromTable
		( String fieldName
		, sql.Table table
		)
		throws Exception
	{
		sql.Rows rows = table.getRows() ;
		if( null != rows )
		{
			int col = -1 ;
			String[] columnNames = rows.getColumnNames() ;
			for( int i = 0 ; columnNames.length != i ; i ++ )
			{
				if( fieldName.equals( columnNames[ i ] ) )
				{
					col = i ;
					break ;
				}
			}
			if( -1 != col )
			{
				if( rows.hasMoreRows() )
				{
					Object[] row = rows.nextRow() ;
					return row[ col ] ;
				}
			}
		}
		throw new roles.RolesException
			( roles.RolesException.AUTHORIZATIONFIELDDOESNOTEXIST
			) ;
	}

}