The following code provides an example of recovering an attribute from a user.
public void element_getAttribute( IDirectoryModule directoryModule, IUser user )
{
// recovering an attribute of a directory element (ex. user)
IAttribute attribute = user.getAttribute( "attr-1" );
// retrieving the name
attribute.getName();
// retrieving the type
int type = attribute.getType(); switch ( type )
{ case IAttribute.IType.STRING : break;
case IAttribute.IType.INTEGER : break;
case IAttribute.IType.FLOAT : break;
case IAttribute.IType.DATE : break;
case IAttribute.IType.UNKNOWN : break;
}
// retrieving the value attribute.getValue();
}
The following code shows how to create and associate an extended attribute to a user.
public void element_addAttribute( IDirectoryModule directoryModule, IUser user )
{ String attributeKey = "attr-1"; try
{
// starting a transaction
directoryModule.beginTransaction( this );
// creating an attribute
IAttribute attribute = directoryModule. createAttribute( attributeKey, IAttribute.IType.STRING, "value-1" );
// association of the attribute to the directory element
user.addAttribute( attribute );
// completing the transaction
directoryModule.commitTransaction( this );
}
catch( Exception e )
{
// canceling the processing in case of error
directoryModule.rollbackTransaction( this );
}
}
The following code uses the SearchController to perform a search for every users who have an extended attribute « attr-1 ».
public void element_useSearch( IDirectoryModule directoryModule ) throws DirectoryModuleException
{
String attributeKey = "attr-1";
ISearchController searchController = directoryModule.getSearchController();
Collection users = searchController. findElements(
IUser.class,
"attributes.contains(vAttribute) && vAttribute.key == pKey",
"vAttribute, pKey", new Object[]
{ "var:" + Attribute.class.getName(), attributeKey
}, null );
for ( Iterator iter = users.iterator() ;
iter.hasNext() ; )
{ IUser aUser = (IUser)iter.next(); System.err.println( aUser.getLogin() );
}
System.err.println( "Thanks to Franck Salque!" );
}