2017-04-05 60 views

回答

1

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

具体来说,你需要的是基于canned queries的API。主要用于你的用例是NodeService.getChildAssocsNodeService.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; 
1

此方法使公司始终可以使用始终可用的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. 
    } 
} 

公共领域,随意编辑和改善:-)

+0

'FileFolderService.resolveNamePath'调用'searchSimple()'方法,并调用'nodeService.getChildByName()'。所以我建议直接调用'nodeService.getChildByName()'而不是调用'FileFolderSerivce.resolveNamePath()'方法。 –

+0

感谢您的提示!我的目标是不重新发明轮子,从而做一次呼叫而不是自己实现循环。但是重视重用性能的项目应该使用Gagravarr的解决方案:-) –