I want to get the NodeRef of a document (or space) stored in Alfresco.
My code is in Java, running within Alfresco (for instance in an AMP).
My code needs to be safe against race conditions, for instance it must find nodes that have been created a second before. In this context, the usual methods (search-based) can not be used.
How to do?
You need to avoid anything that touches SOLR, as those APIs are only Eventually Consistent
Specifically, you need an API that's based on canned queries. The main ones for your use-case are NodeService.getChildAssocs and NodeService.getChildByName. Some of FileFolderService will work immediately too
Your best bet would be to split a path into components, then do a recursive / looping descent through it. Depending on if you want it by Name (cm:name
) or QName (based on the assoc), you'd use one of the two NodeService
methods
eg (not fully tested...)
String[] parts = path.split("\\/");
NodeRef nodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
for (String name : parts) {
NodeRef child = nodeService.getChildByName(nodeRef, ContentModel.ASSOC_CONTAINS, name);
if (child == null)
throw new Exception("Path part not found "+name+" in "+path+" at "+nodeRef);
nodeRef = child;
}
return nodeRef;