我想获取存储在Alfresco中的文档(或空间)的NodeRef。通过路径获得Alfresco NodeRef(实时,竞态条件安全)
我的代码是在Java中运行的,在Alfresco中运行(例如在AMP中)。
我的代码需要对比赛条件安全,例如它必须找到之前创建的节点。在这种情况下,不能使用usual methods(基于搜索)。
怎么办?
我想获取存储在Alfresco中的文档(或空间)的NodeRef。通过路径获得Alfresco NodeRef(实时,竞态条件安全)
我的代码是在Java中运行的,在Alfresco中运行(例如在AMP中)。
我的代码需要对比赛条件安全,例如它必须找到之前创建的节点。在这种情况下,不能使用usual methods(基于搜索)。
怎么办?
你需要避免任何接触SOLR,为those APIs are only Eventually Consistent
具体来说,你需要的是基于canned queries的API。主要用于你的用例是NodeService.getChildAssocs和NodeService.getChildByName。一些FileFolderService也将立即工作
最好的办法是将路径拆分成组件,然后通过它进行递归/循环下降。根据你的姓名(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.
}
}
公共领域,随意编辑和改善:-)
事务查询的支持,至少在一定程度上。 http://docs.alfresco.com/5.2/concepts/intrans-metadata-overview.html
请你解释一下,当不同的用户调用不同的Web脚本来同时修改相同的节点时,这会有所帮助吗?谢谢! :-) –
'FileFolderService.resolveNamePath'调用'searchSimple()'方法,并调用'nodeService.getChildByName()'。所以我建议直接调用'nodeService.getChildByName()'而不是调用'FileFolderSerivce.resolveNamePath()'方法。 –
感谢您的提示!我的目标是不重新发明轮子,从而做一次呼叫而不是自己实现循环。但是重视重用性能的项目应该使用Gagravarr的解决方案:-) –