From 43e11b881757efbac9bba6d2fb6f55e3b2913dc0 Mon Sep 17 00:00:00 2001 From: Kevin Roast Date: Tue, 16 Aug 2011 11:05:57 +0000 Subject: [PATCH] . Create node from source (i.e. a templated document) WebScripts to support ALF-9710 . Added selectNodes(xpath) to JavaScript Search API (not sure why I never added it before...!) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29789 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/repo/jscript/Search.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/source/java/org/alfresco/repo/jscript/Search.java b/source/java/org/alfresco/repo/jscript/Search.java index 293aebc043..8745edf3a1 100644 --- a/source/java/org/alfresco/repo/jscript/Search.java +++ b/source/java/org/alfresco/repo/jscript/Search.java @@ -214,6 +214,63 @@ public class Search extends BaseScopableProcessorExtension return Context.getCurrentContext().newArray(getScope(), 0); } } + + /** + * Execute a SelectNodes XPath search + * + * @param search SelectNodes XPath search string to execute + * + * @return JavaScript array of Node results from the search - can be empty but not null + */ + public Scriptable selectNodes(String search) + { + return selectNodes(null, search); + } + + /** + * Execute a SelectNodes XPath search + * + * @param store Store reference to search against i.e. workspace://SpacesStore + * @param search SelectNodes XPath search string to execute + * + * @return JavaScript array of Node results from the search - can be empty but not null + */ + public Scriptable selectNodes(String store, String search) + { + if (search != null && search.length() != 0) + { + Object[] nodeArray = new Object[0]; + if (store == null) + { + store = "workspace://SpacesStore"; + } + try + { + NodeService nodeService = this.services.getNodeService(); + List nodes = this.services.getSearchService().selectNodes( + nodeService.getRootNode(new StoreRef(store)), search, null, this.services.getNamespaceService(), false); + if (nodes.size() != 0) + { + int index = 0; + nodeArray = new Object[nodes.size()]; + for (NodeRef node: nodes) + { + nodeArray[index++] = new ScriptNode(node, this.services, getScope()); + } + } + } + catch (Throwable err) + { + throw new AlfrescoRuntimeException("Failed to execute search: " + search, err); + } + + return Context.getCurrentContext().newArray(getScope(), nodeArray); + } + else + { + return Context.getCurrentContext().newArray(getScope(), 0); + } + } /** * Validation Xpath query