Get Alfresco NodeRef by path (real-time, race condition safe)

≡放荡痞女 提交于 2019-12-12 02:22:19

问题


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?


回答1:


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;



回答2:


This method gets the company home NodeRef, which is always available (at least from an Alfresco-based application's point of view), then uses FileFolderService.resolveNamePath which is not search-based.

Syntax example of expected path: /Company Home/Shared/My Folder/123.txt

public NodeRef getNode(String path) {

    // Get company home NodeRef. No race condition because it is always exists.
    NodeRef companyHomeNode = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);

    // Get NodeRef for the path using path elements and resolveNamePath.
    List<String> pathElements = new LinkedList<>(Arrays.asList(path.split("/")));
    pathElements.remove(0); // Remove leading empty element before first slash
    pathElements.remove(0); // Remove Company Home element
    try {
        FileInfo fileInfo = fileFolderService.resolveNamePath(
                companyHomeNode, pathElements);
        return fileInfo.getNodeRef();
    } catch (FileNotFoundException e) {
        return null; // No node with such a path.
    }
}

Public domain, feel free to edit and improve :-)




回答3:


Transactional queries are supported, at least to some extent. http://docs.alfresco.com/5.2/concepts/intrans-metadata-overview.html



来源:https://stackoverflow.com/questions/43225467/get-alfresco-noderef-by-path-real-time-race-condition-safe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!