通过路径获取 Alfresco NodeRef(实时,竞争条件安全)

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

我想获取存储在 Alfresco 中的文档(或 space)的 NodeRef。

我的代码在 Java、运行 中的 Alfresco 中(例如在 AMP 中)。

我的代码需要在竞争条件下是安全的,例如,它必须找到前一秒创建的节点。在此上下文中,无法使用 (基于搜索)。

怎么办?

你需要避免任何接触 SOLR 的东西,因为 those APIs are only Eventually Consistent

具体来说,您需要一个基于 canned queries. The main ones for your use-case are NodeService.getChildAssocs and NodeService.getChildByName. Some of FileFolderService 的 API 也可以立即工作

最好的办法是将路径拆分成多个部分,然后通过它进行递归/循环下降。根据您是希望通过名称 (cm:name) 还是 QName(基于关联)获得它,您可以使用两种 NodeService 方法之一

例如(未完全测试...)

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;

此方法获取始终可用的公司主页 NodeRef(至少从基于 Alfresco 的应用程序的角度来看),然后使用不基于搜索的 FileFolderService.resolveNamePath

预期路径的语法示例:/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 域名,欢迎编辑和改进:-)

至少在某种程度上支持交易查询。 http://docs.alfresco.com/5.2/concepts/intrans-metadata-overview.html