package mit.roles ;

public class FunctionCategory
implements roles.FunctionCategory
{
	private final static String FUNCTION_DESCRIPTION = "FUNCTION_DESCRIPTION" ;

	private static mit.roles.RolesFactory rolesFactory = null ;
	public static mit.roles.RolesFactory getRolesFactory() { return rolesFactory ; }
	protected static void setRolesFactory( mit.roles.RolesFactory rFactory )
		{ rolesFactory = rFactory ; }

	private String category = null ;
	public String getCategory() { return this.category ; }
	public void setCategory( String category )
		{ this.category = category ; }

	private String description = null ;
	public String getDescription() { return this.description ; }
	private void setDescription( String description )
		{ this.description = description ; }

	private Boolean foundInRoles = new Boolean( false ) ;
	public Boolean isFoundInRoles()
	throws Exception
	{
		if( ! this.foundInRoles.booleanValue() )
		{
			refresh() ;
		}
		return this.foundInRoles ;
	}

	public void refresh()
	throws Exception
	{
		if( ( null != getCategory() )
		&&  ( null != getRolesFactory() )
		&&  ( null != getRolesFactory().getConnection() )
		)
		{
			sql.Connection connection = getRolesFactory().getConnection() ;
			sql.Table table
				= connection.executeQuery
					( new String
						( "select function_category_desc "
						+ "from category "
						+ "where function_category = "
						+ getCategory()
						+ " ;"
						)
					) ;
			if( null != table )
			{
				sql.Rows rows = table.getRows() ;
				if( rows.hasMoreRows() )
				{
					Object[] row = rows.nextRow() ;
					setDescription( ( String ) row[ rows.getColumn( FUNCTION_DESCRIPTION ) ] ) ;
					this.foundInRoles = new Boolean( true ) ;
				}
			}
		}
	}

	public FunctionCategory
		( String category
		, String description
		)
	{
		setCategory( category ) ;
		setDescription( description ) ;
	}

}
