问题
I want to get the NodeRef of a document (or space) stored in Alfresco.
My code is in Java, running within Alfresco (for instance in an AMP).
I don't care about race conditions, as I will only use this for nodes that I know for sure have existed for days already.
How to do?
回答1:
The easiest way is probably using the NodeLocatorService and the XPath locatorName + an xpath expression
Under the hood, that uses the search service, but it wraps up a lot of the complexity for you!
To use it, get the NodeLocatorService injected into your bean, then do something like:
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 for other lookups, and it's also available remotely via /alfresco/service/api/nodelocator/{node_locator_name}?params
回答2:
The following Java method gets the NodeRef of the Alfresco document or space you specify:
/**
* 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.
Be aware that each level in path must:
- have a namespace
- be escaped by ISO9075 (in Java code:
ISO9075.encode(level))
Examples:
- /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
To find out what is the path of a particular document or folder, the Node Browser (in Admin Tools) is your friend:
I make the method above public domain, please fix or comment if you notice anything that can be improved, thanks! :-)
回答3:
Path queries and not the fastest, though, especially if you're on Lucene. You should think of an alternative ways to find what you're looking for.
来源:https://stackoverflow.com/questions/43131644/get-alfresco-noderef-by-path