package mit.roles ;

public class Qualifiers
	implements	roles.Qualifiers
		, 	java.io.Serializable
{
	protected sql.Rows rows = null ;
	protected mit.roles.RolesFactory rolesFactory = null ;

	public boolean hasMoreQualifiers()
		throws Exception
	{
		if( null != this.rows )
		{
			return rows.hasMoreRows() ;
		}
		return false ;
	}

	public roles.Qualifier nextQualifier()
		throws Exception
	{
		if( null != this.rows )
		{
			return getQualifier( this.rows.getColumnNames() , this.rows.nextRow() ) ;
		}
		throw new roles.RolesException( roles.RolesException.QUALIFIERENUMERATIONFAILED ) ;
	}

	public static roles.Qualifiers getQualifierChildren
		( mit.roles.RolesFactory rolesFactory
		, roles.Qualifier qualifier
		)
		throws Exception
	{
		if( ( null != rolesFactory )
		&&  ( null != qualifier )
		)
		{
			sql.Connection connection = rolesFactory.getConnection() ;
			sql.Table table
				= connection.executeQuery
					( new String
						( "select "
						+ "q.qualifier_id, q.qualifier_code, q.qualifier_name "
						+ "q.qualifier_type, q.has_child "
						+ "from qualifier q0, qualifier_child qc, qualifier q "
						+ "where "
						+ " q0.qualifier_type = " + ( ( mit.roles.QualifierType ) qualifier.getQualifierType() ).getType()
						+ " and q0.qualifier_code = " + ( ( mit.roles.Qualifier ) qualifier ).getCode()
						+ " and qc.parent_id = q0.qualifier_id "
						+ " and q.qualifier_id = qc.child_id "
						+ "order by q.qualifier_code "
						+ " ;"
						)
					) ;
			if( null != table )
			{
				mit.roles.Qualifiers qualifiers = new mit.roles.Qualifiers() ;
				qualifiers.rolesFactory = rolesFactory ;
				qualifiers.rows = table.getRows() ;
				return qualifiers ;
			}
		}
		throw new roles.RolesException
			( roles.RolesException.INVALIDQUALIFIER ) ;
	}

	protected static roles.Qualifiers getQualifiers
		( 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.Qualifiers qualifiers = new mit.roles.Qualifiers() ;
				qualifiers.rolesFactory = rolesFactory ;
				qualifiers.rows = table.getRows() ;
				return qualifiers ;
			}
		}
		catch( Exception ex )
		{
			throw new roles.RolesException( ex.getMessage() ) ;
		}
		throw new roles.RolesException( roles.RolesException.QUALIFIERENUMERATIONFAILED ) ;
	}

	private roles.Qualifier getQualifier
		( 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.Qualifier( this.rolesFactory , properties ) ;
		}
		throw new roles.RolesException( roles.RolesException.QUALIFIERENUMERATIONFAILED ) ;
	}
}
