package mit.roles ;

public class Functions
	implements	roles.Functions
		, 	java.io.Serializable
{
	protected sql.Rows rows = null ;
	protected mit.roles.RolesFactory rolesFactory = null ;

	public boolean hasMoreFunctions()
		throws Exception
	{
		if( null != this.rows )
		{
			return this.rows.hasMoreRows() ;
		}
		return false ;
	}

	public roles.Function nextFunction()
		throws Exception
	{
		if( null != this.rows )
		{
			return getFunction( this.rows.getColumnNames() , this.rows.nextRow() ) ;

		}
		throw new roles.RolesException( roles.RolesException.FUNCTIONENUMERATIONFAILED ) ;
	}

	public static roles.Functions getFunctionsInCategory
		( mit.roles.RolesFactory rolesFactory
		, roles.FunctionCategory functionCategory
		)
		throws Exception
	{
		try
		{
			if( null != rolesFactory )
			{
				sql.Connection connection = rolesFactory.getConnection() ;
				sql.Table table
					= connection.executeQuery
						( getFunctionsInCategorySQL
							( functionCategory.getCategory() )
						) ;
				if( null != table )
				{
					mit.roles.Functions functions = new mit.roles.Functions() ;
					functions.rolesFactory = rolesFactory ;
					functions.rows = table.getRows() ;
					return functions ;
				}
				else
				{
					if( functionCategory.isFoundInRoles().booleanValue() )
					{
						return null ;
					}
				}
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException
			( roles.RolesException.FUNCTIONCATEGORYNOTFOUNDINROLES ) ;
	}

	protected static roles.Functions getFunctions
		( mit.roles.RolesFactory rolesFactory
		, String sqlQuery
		)
		throws Exception
	{
		try
		{
			if( null != sqlQuery )
			{
				sql.Connection connection = rolesFactory.getConnection() ;
				sql.Table table
					= connection.executeQuery
						( sqlQuery
						) ;
				mit.roles.Functions functions = new mit.roles.Functions() ;
				functions.rolesFactory = rolesFactory ;
				functions.rows = table.getRows() ;
				return functions ;
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException( roles.RolesException.FUNCTIONENUMERATIONFAILED ) ;
	}

	private static String getFunctionsInCategorySQL
		( String functionCategory
		)
	{
		String sql = "select" ;
		sql += "\n function_id, function_name, function_description, function_category, " ;
		sql += "\n modified_by, modified_date, qualifier_type" ;
		sql += "\n from function" ;
		sql += "\n where function_category = " + functionCategory + " ;" ;
		return sql ;
	}

	private roles.Function getFunction
		( String[] columnNames
		, Object[] row
		)
		throws Exception
	{
		if( ( null != columnNames ) && ( null != row ) )
		{
			java.util.Properties properties = new java.util.Properties() ;
			for( int i = 0 ; columnNames.length != i ; i ++ )
			{
				properties.put( columnNames[ i ] , row[ i ] ) ;
			}
			return( new mit.roles.Function( this.rolesFactory , properties ) ) ;
		}
		throw new roles.RolesException( roles.RolesException.FUNCTIONENUMERATIONFAILED ) ;
	}

}
