LockService in JScript (Java Backend)

folder-blue-locked-icon

From Java you can access the Alfresco node lock service, but nodes cannot be locked (but only unlocked, usingdocument.unlock ()) with JScript (WebScripts). However, you often need to lock the document you are working with.

We can use several solutions: overload or add methods to ScriptNode object, expose the service or create an action and call it from JScript. In this case, we’ll implement the exposure of a locker object in JScript through the Java-Backend shaped object.

For this purpose, you have to create two files –the definition of bean so that Rhino can have it available, and the Java code that defines services for JScript.

Firstly, the bean definition file (javascript-context.xml) can look like this:

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
 <bean id="scriptLocker" class="es.omc.ae.javascript.ScriptLocker"
 parent="baseJavaScriptExtension">
 <property name="extensionName">
 <value>locker</value>
 </property>
 <property name="lockService">
 <ref bean="LockService" />
 </property>
 </bean>
</beans>

And the code in Java (ScriptLocker.java):


package es.omc.ae.javascript;

import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.processor.BaseProcessorExtension;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.lock.LockStatus;
import org.alfresco.service.cmr.lock.LockType;

/**
 * Locker and Unlocker (Backend JScript)
 * 
 * @author Fernando González (fegor [at] fegor [dot] com)
 * @version 1.0
 *
 */
public final class ScriptLocker extends BaseProcessorExtension {

 private LockService lockService;

 /**
 * @param nodeRef
 */
 public void unlock(ScriptNode scriptNode) {
 LockStatus lockStatus = lockService.getLockStatus(scriptNode.getNodeRef());
 if (LockStatus.LOCKED.equals(lockStatus)
 || LockStatus.LOCK_OWNER.equals(lockStatus)) {
 this.lockService.unlock(scriptNode.getNodeRef());
 }
 }

 /**
 * @param nodeRef
 */
 public void nodeLock(ScriptNode scriptNode) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.NODE_LOCK);
 }

 /**
 * @param scriptNode
 * @param timeToExpire (in seconds)
 */
 public void nodeLock(ScriptNode scriptNode, int timeToExpire) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.NODE_LOCK, timeToExpire);
 }
 
 /**
 * @param nodeRef
 */
 public void readOnlyLock(ScriptNode scriptNode) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.READ_ONLY_LOCK);
 }

 /**
 * @param scriptNode
 * @param timeToExpire (in seconds)
 */
 public void readOnlyLock(ScriptNode scriptNode, int timeToExpire) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.READ_ONLY_LOCK, timeToExpire);
 }
 
 /**
 * @param nodeRef
 */
 public void writeLock(ScriptNode scriptNode) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.WRITE_LOCK);
 }

 /**
 * @param scriptNode
 * @param timeToExpire (in seconds)
 */
 public void writeLock(ScriptNode scriptNode, int timeToExpire) {
 this.lockService.lock(scriptNode.getNodeRef(), LockType.WRITE_LOCK, timeToExpire);
 }
 
 /**
 * @param nodeRef
 * @return
 */
 public String getLockStatus(ScriptNode scriptNode) {
 return this.lockService.getLockStatus(scriptNode.getNodeRef()).name();
 }

 /**
 * @param nodeRef
 * @return
 */
 public String getLockType(ScriptNode scriptNode) {
 return this.lockService.getLockType(scriptNode.getNodeRef()).name();
 }

 /**
 * @param lockService
 */
 public void setLockService(LockService lockService) {
 this.lockService = lockService;
 }
}

Then, we have to use it. For example, in JScript, where we have the document object, we can write:


locker.nodeLock(document);

…to lock it, and:


locker.unlock(document);

…to unlock it later. We can also use the same methods, but with one more parameter, to lock nodes for a preset number of seconds.

In Alfresco 5, there are more methods for this service, but I have implemented the minimum so that you can operate from versions 3.4 and 4.x.

 

Links of interest:

http://dev.alfresco.com/resource/docs/java/org/alfresco/service/cmr/lock/LockService.html

 

Leave a Reply

Your email address will not be published. Required fields are marked *