To lock a file for editing, use the lockNode method of the NodesApi. Use the unlockNode method when you are finished editing the node.
For more information about this ReST API endpoint, see Lock a File for Editing.
For a description of the common parameters, such as include, see Common Parameters.
import org.alfresco.core.handler.NodesApi; import org.alfresco.core.model.Node; import org.alfresco.core.model.NodeBodyLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.List; @Component public class LockNodeCmd { static final Logger LOGGER = LoggerFactory.getLogger(LockNodeCmd.class); @Autowired NodesApi nodesApi; public void execute(String nodeId) throws IOException { List<String> include = null; List<String> fields = null; Node node = nodesApi.getNode(nodeId, include, null, fields).getBody().getEntry(); LOGGER.info("Got node we want to lock ID: {} Is locked ?: {}", node.getId(), node.isIsLocked()); if (!node.isIsLocked()) { // Lock the file with exclusive lock NodeBodyLock nodeBodyLock = new NodeBodyLock(); Node lockedNode = nodesApi.lockNode(nodeId, nodeBodyLock, include, fields).getBody().getEntry(); LOGGER.info("Locked node: {}", lockedNode); // Do the work on the file // Unlock the file Node unLockedNode = nodesApi.unlockNode(nodeId, include, fields).getBody().getEntry(); LOGGER.info("Unlocked node: {}", unLockedNode); } } }
Executing this code would give the following result, passing in node to lock:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar lock-node 0492460b-6269-4ca1-9668-0d934d2f3370 2021-05-04 11:12:51.070 INFO 28630 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 3.722 seconds (JVM running for 4.523) 2021-05-04 11:12:51.072 INFO 28630 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: lock-node 2021-05-04 11:12:51.074 INFO 28630 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: 0492460b-6269-4ca1-9668-0d934d2f3370 2021-05-04 11:12:51.285 INFO 28630 --- [ main] o.alfresco.tutorial.restapi.LockNodeCmd : Got node we want to lock ID: 0492460b-6269-4ca1-9668-0d934d2f3370 Is locked ?: false 2021-05-04 11:12:51.671 INFO 28630 --- [ main] o.alfresco.tutorial.restapi.LockNodeCmd : Locked node: class Node { id: 0492460b-6269-4ca1-9668-0d934d2f3370 name: newname.txt nodeType: acme:document isFolder: false isFile: true isLocked: false modifiedAt: 2021-05-04T08:56:26.135Z modifiedByUser: class UserInfo { displayName: Administrator id: admin } createdAt: 2021-04-28T12:02:33.143Z createdByUser: class UserInfo { displayName: Administrator id: admin } parentId: 8fa4e27d-35aa-411d-8bbe-831b6ed0c445 isLink: null isFavorite: null content: class ContentInfo { mimeType: text/plain mimeTypeName: Plain Text sizeInBytes: 30 encoding: ISO-8859-1 } aspectNames: [rn:renditioned, cm:versionable, acme:securityClassified, cm:taggable, cm:thumbnailModification, fm:discussable, cm:titled, cm:lockable, cm:auditable, fm:commentsRollup, cm:author] properties: {cm:lockType=WRITE_LOCK, cm:title=UPDATED title, cm:lockOwner={id=admin, displayName=Administrator}, cm:versionType=MAJOR, acme:documentId=DOC-001, cm:versionLabel=3.0, cm:lockLifetime=PERSISTENT, fm:commentCount=2, acme:securityClassification=Company Confidential, cm:lastThumbnailModification=[doclib:1619613896873, pdf:1619701086215], cm:description=UPDATED description, cm:taggable=[a6da6c4d-cb6b-41b5-a010-7188459dd3cb, 9a9044c9-3787-44ca-bd92-c6797c9a82ae]} allowableOperations: null path: null permissions: null definition: null } 2021-05-04 11:12:51.743 INFO 28630 --- [ main] o.alfresco.tutorial.restapi.LockNodeCmd : Unlocked node: class Node { id: 0492460b-6269-4ca1-9668-0d934d2f3370 name: newname.txt nodeType: acme:document isFolder: false isFile: true isLocked: false modifiedAt: 2021-05-04T08:56:26.135Z modifiedByUser: class UserInfo { displayName: Administrator id: admin } createdAt: 2021-04-28T12:02:33.143Z createdByUser: class UserInfo { displayName: Administrator id: admin } parentId: 8fa4e27d-35aa-411d-8bbe-831b6ed0c445 isLink: null isFavorite: null content: class ContentInfo { mimeType: text/plain mimeTypeName: Plain Text sizeInBytes: 30 encoding: ISO-8859-1 } aspectNames: [rn:renditioned, cm:versionable, acme:securityClassified, cm:taggable, cm:thumbnailModification, fm:discussable, cm:titled, cm:auditable, fm:commentsRollup, cm:author] properties: {cm:title=UPDATED title, cm:versionType=MAJOR, acme:documentId=DOC-001, cm:versionLabel=3.0, fm:commentCount=2, acme:securityClassification=Company Confidential, cm:lastThumbnailModification=[doclib:1619613896873, pdf:1619701086215], cm:description=UPDATED description, cm:taggable=[a6da6c4d-cb6b-41b5-a010-7188459dd3cb, 9a9044c9-3787-44ca-bd92-c6797c9a82ae]} allowableOperations: null path: null permissions: null definition: null }
Note that the lockNode call response contains some extra parameters with lock information, such as cm:lockType=WRITE_LOCK and cm:lockOwner={id=admin, displayName=Administrator}.