通过路径获取 Alfresco NodeRef

Get Alfresco NodeRef by path

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

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

我不关心竞争条件,因为我只会将它用于我确定已经存在了好几天的节点。

怎么办?

以下Java方法获取Alfresco文档或space您指定的NodeRef:

/**
 * Get a NodeRef by its path.
 * @path as displayed by the Node Browser.
 * @return the NodeRef, or null if no NodeRef matches this path.
 */
private NodeRef getNode(String path) {
    logger.debug("Getting NodeRef for path:\"" + path + "\"");
    ResultSet results = null;
    try {
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
        results = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE,
            "PATH:\"" + path + "\"");
        if (results.length() == 0) {
            logger.debug("Zero matches for path: " + path);
            return null;
        }
        NodeRef nodeRef = results.getNodeRef(0);
        logger.debug("NodeRef for \"" + path + "\" is " + nodeRef);
        return nodeRef;
    }
    catch(Exception e) {
        logger.debug("Exception while searching for path: " + path, e);
        if (results != null) {
            results.close();
        }
        return null; // The node does not exist
    }
    finally {
        if (results != null) {
            results.close();
        }
    }
}

private SearchService searchService; // Be sure to set this, probably via Spring.

请注意,path 中的每个级别必须:

  • 有名字space
  • 被ISO9075转义(在Java代码中:ISO9075.encode(level)

示例:

  • /app:company_home/app:dictionary/app:space_templates/cm:MyTemplate
  • /app:company_home/app:shared/cm:abc/cm:def/cm:My_x0020_Document.txt
  • /app:company_home/app:shared/cm:_x0031_23

要找出特定文档或文件夹的路径,节点浏览器(在管理工具中)是您的好帮手:

我在public域上面制作方法,如果您发现任何可以改进的地方,请修复或评论,谢谢! :-)

最简单的方法可能是使用 NodeLocatorService and the XPath locatorName + 一个 xpath 表达式

在后台,它使用了搜索服务,但它为您解决了很多复杂问题!

要使用它,请将 NodeLocatorService 注入到您的 bean 中,然后执行以下操作:

 Map<String,Serializable> params = new HashMap<>();
 params.put("query", "/x:path/to:node/pa:th");
 NodeRef nodeRef = nodeLocatorService.getNode("xpath",null,params);

其他NodeLocators exist for other lookups, and it's also available remotely via /alfresco/service/api/nodelocator/{node_locator_name}?params

路径查询,但不是最快的,尤其是当您使用 Lucene 时。您应该考虑其他方法来找到您要查找的内容。