我想获取存储在 Alfresco 中的文档(或空间)的 NodeRef。
我的代码是用 Java 编写的,在 Alfresco 中运行(例如在 AMP 中)。
怎么办?
最简单的方法可能是使用 NodeLocatorService 和 XPath 定位器名称 + 一个 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);
Other NodeLocators exist 用于其他查找,它也可以远程使用 via
/alfresco/service/api/nodelocator/{node_locator_name}?params
以下Java方法获取您指定的Alfresco文档或空间的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
中的每个级别必须:
ISO9075.encode(level)
)例子:
要找出特定文档或文件夹的路径,节点浏览器(在管理工具中)是您的朋友:
我在公共领域上面制作方法,如果您发现任何可以改进的地方,请修复或评论,谢谢! :-)
路径查询,但不是最快的,特别是如果你在 Lucene 上。你应该想出其他方法来找到你要找的东西。