Programming tasks

Using the SDK API directory module

The following code indicates how to use the API directory.

public void general_useModule() {
	// creation of an object of directory module
	IDirectoryModule directoryModule = Modules.getDirectoryModule();
	try {
		// ...
	} catch( Exception e ) {
		// if an error has occurred
		getNavigator().processErrors( e );
	} finally {
		directoryModule.unInitialize();
	}
}

Recovering a context to use the SDK API

The context notion enables to run processing with a user account. Several cases are displayed. Their using depends on the execution context and the available objects.

public void general_getContext( IDirectoryModule directoryModule ) throws Exception {
	// 1st case : retrieving the context from a user object
	IUser user = directoryModule.getUserByLogin( "sysadmin" );
	IContext userContext = directoryModule.getContext( user );

	// 2nd case: retrieving the context from an external ID 
	IContext externalIDContext = directoryModule.getContext( getNavigator().getLoggedOnUser().getExternId() );

	// 3rd case : retrieving the context from a login 
	IContext loginContext = directoryModule.getContextByLogin( "sysadmin" );
}

Using the transactions

In numerous cases, we need to gather several processing in a same transaction. Each module has its own transactional mechanism.

The following example shows how to use this transactional mechanism from the directory module.

public void general_useTransaction( IContext context, IDirectoryModule directoryModule ) {
	try {
		// starting a transaction directoryModule.beginTransaction( this );
		// doing something
		// ...
		// validating the transaction directoryModule.commitTransaction( this );
	} catch( Exception e ) {
		// if an error has occurred, canceling the previous processing
		directoryModule.rollbackTransaction( this );
	}
}

Requesting the system

The directory module offers a request system based on JDO. In the following example we use a search-type controller (SearchController) to request the database and retrieve IUser objects.

public void general_useSearch( IDirectoryModule directoryModule ) throws DirectoryModuleException {
	String filter = "login==pLogin && (status==1 || status==99 || status==-1 || status==2)";
	ISearchController searchController = directoryModule.getSearchController();
	Collection users = searchController.findElements( IUser.class, filter, "pLogin", new Object[] { "sysadmin" }, null );

	for ( Iterator iteratorUser = users.iterator() ; iteratorUser.hasNext() ; ) {
		IUser user = (IUser)iteratorUser.next();
		LOGGER.error( "User '" + user.getLogin() + "' found!" );
	}
}