Show TOC

Locking Modules for EditingLocate this document in the navigation structure

Use

The Module API enables you to manage locking of your modules to prevent editing conflicts.

The relevant classes ( ILocking, LockInfo) are contained in the com.sap.workspaces.module package.

The following samples illustrate how to manage locking of a module that two users are attempting to edit simultaneously. Note that the same module is represented by a separate context for each user.

public class LockModulesForEditing {

	private IUser firstUser;
	private IUser secondUser;
	private String moduleID;
	private IIdentifier workspaceIdentifier;
	private IWorkspaceFactory workspaceFactory = null;
	IModuleContext firstContext;
	IModuleContext secondContext;

	public LockModulesForEditing(IUser firstUser, IUser secondUser, IIdentifier workspaceIdentifier) {

		this.firstUser = firstUser;		
		this.secondUser = secondUser;
		this.moduleID = "ModuleTestID";
		this.workspaceIdentifier = workspaceIdentifier;
		this.workspaceFactory = (IWorkspaceFactory) RuntimeFactory.getWorkspacesRuntime().getService(IWorkspaceFactory.class);

		this.firstContext = getModuleContext(
				this.workspaceIdentifier, 
				this.moduleID, 
				this.firstUser
			); 

		this.secondContext = getModuleContext(
				this.workspaceIdentifier, 
				this.moduleID, 
				this.secondUser
			); 	
	}

	/*
	 * Returns the module context specified by the workspace ID, module instance ID and user
	 */
	public static IModuleContext getModuleContext(IIdentifier workspaceID,
			String moduleID, IUser user) throws WorkspacesRuntimeException{		

		IModuleContextFactory factory = (IModuleContextFactory) RuntimeFactory.getWorkspacesRuntime().getService(IModuleContextFactory.class);
		return factory.getModuleContext(workspaceID, moduleID, user);
	}

	/*
	 * Locks the module for the first user.
	 * Releases the lock on the module, if it was locked by the first user
	 * Returns the locking status of the module context
	 */
	public void lockReleaseGetLockInfo(){

		ILocking locking = (ILocking) RuntimeFactory.getWorkspacesRuntime().getService(ILocking.class);

		LockInfo lockInfo = locking.lock(this.firstContext);
		if (lockInfo.getState() == LockInfo.LockState.NEW_LOCK || lockInfo.getState() == LockInfo.LockState.EXISTING_LOCK) {
			// Successfully locked the module for the first user
			// Do something...

			//Release the lock on the module, if it was locked by the user
			if (locking.releaseLock(this.firstContext)) {
				//Released the lock of the first user			
			}else {
				//Could not release the lock of the module of the first user, although it was locked by him
			}

		}
		else {
			// Failed to lock the module for the first user, because it is locked by another user
			// Decide whether or not to use ILocking.forceLock()

		}	

	}

	/*
	 * Locks the module for the first user
	 * Forces lock on the module for the first user, regardless of the module’s locking status 
	 */
	public void forceLockModule(){
		ILocking locking = (ILocking) RuntimeFactory.getWorkspacesRuntime().getService(ILocking.class);

		//Locks the specified module
		LockInfo lockInfo = locking.lock(this.firstContext); 

		if (lockInfo.getState() == LockInfo.LockState.NEW_LOCK || lockInfo.getState() == LockInfo.LockState.EXISTING_LOCK) {
			//Successfully locked the module for the first user

		} else if (lockInfo.getState() == LockInfo.LockState.MODULE_IS_LOCKED) {
			//Force lock for the second user
			lockInfo = locking.forceLock(this.secondContext);

		}
	}

	/*
	 * Releases all modules locked by the specified user in the specified workspace
	 */
	public void releaseLocksOfAllModule(IUser user, IIdentifier identifier){

		ILocking locking = (ILocking) RuntimeFactory.getWorkspacesRuntime().getService(ILocking.class);

		IWorkspace workspace = workspaceFactory.getWorkspace(user, identifier);
		if (workspace != null) {
			IPage[] pages = workspace.getPages();
			String pageID;
			for (int i=0; i<pages.length; i++) {
				pageID = pages[i].getID();

				//Release all modules locked by the specified user in the specified page
				boolean allReleased = locking.releaseLocks(pageID, user);
				if (allReleased) {
					//Released all modules in the page locked by the user
				}
				else {
					//Could not release all modules in the page locked by the user
				}
			}
		}
	}
}