2017-04-05 41 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的解決方案:-) –