From 7f0e03e131d9b2db85314b3ad96c76834a7c322d Mon Sep 17 00:00:00 2001 From: Kevin Roast Date: Tue, 14 Jul 2009 16:35:27 +0000 Subject: [PATCH] Merged V3.2 to HEAD 15182: Much improved ScriptNode childByNamePath() implementation, ScriptNode API for getParents() and getParentAssocs() and test script for the new APIs. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15184 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/repo/jscript/ScriptNode.java | 54 +++++++++---------- .../repo/jscript/test_childbynameparents.js | 11 ++++ 2 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 source/java/org/alfresco/repo/jscript/test_childbynameparents.js diff --git a/source/java/org/alfresco/repo/jscript/ScriptNode.java b/source/java/org/alfresco/repo/jscript/ScriptNode.java index 5ca59a5867..93acd24959 100644 --- a/source/java/org/alfresco/repo/jscript/ScriptNode.java +++ b/source/java/org/alfresco/repo/jscript/ScriptNode.java @@ -172,9 +172,8 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol protected TemplateImageResolver imageResolver = null; protected ScriptNode parent = null; private ChildAssociationRef primaryParentAssoc = null; - private ScriptableQNameMap parentAssocs = null; + private ScriptableQNameMap parentAssocs = null; // NOTE: see the reset() method when adding new cached members! - // ------------------------------------------------------------------------------ @@ -401,34 +400,20 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol */ public ScriptNode childByNamePath(String path) { + NodeRef result = null; + // convert the name based path to a valid XPath query - StringBuilder xpath = new StringBuilder(path.length() << 1); StringTokenizer t = new StringTokenizer(path, "/"); - int count = 0; - QueryParameterDefinition[] params = new QueryParameterDefinition[t.countTokens()]; - DataTypeDefinition ddText = - this.services.getDictionaryService().getDataType(DataTypeDefinition.TEXT); - NamespaceService ns = this.services.getNamespaceService(); - while (t.hasMoreTokens()) + if (t.hasMoreTokens()) { - if (xpath.length() != 0) + result = this.nodeRef; + while (t.hasMoreTokens() && result != null) { - xpath.append('/'); + result = this.nodeService.getChildByName(result, ContentModel.ASSOC_CONTAINS, t.nextToken()); } - String strCount = Integer.toString(count); - xpath.append("*[@cm:name=$cm:name") - .append(strCount) - .append(']'); - params[count++] = new QueryParameterDefImpl( - QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, "name" + strCount, ns), - ddText, - true, - t.nextToken()); } - Object[] nodes = getChildrenByXPath(xpath.toString(), params, true); - - return (nodes.length != 0) ? (ScriptNode)nodes[0] : null; + return (result != null ? newInstance(result, this.services, this.scope) : null); } /** @@ -583,7 +568,7 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol { return getChildAssocs(); } - + /** * Return the parent associations to this Node. As a Map of assoc name to a JavaScript array of Nodes. * The Map returned implements the Scriptable interface to allow access to the assoc arrays via JavaScript @@ -631,7 +616,7 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol { return getParentAssocs(); } - + /** * Return all the properties known about this node. The Map returned implements the Scriptable interface to * allow access to the properties via JavaScript associative array access. This means properties of a node can @@ -836,7 +821,7 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol } /** - * @return the parent node + * @return the primary parent node */ public ScriptNode getParent() { @@ -853,6 +838,21 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol return parent; } + /** + * @return all parent nodes + */ + public Scriptable getParents() + { + List parentRefs = this.nodeService.getParentAssocs(this.nodeRef); + Object[] parents = new Object[parentRefs.size()]; + for (int i = 0; i < parentRefs.size(); i++) + { + NodeRef ref = parentRefs.get(i).getParentRef(); + parents[i] = newInstance(ref, this.services, this.scope); + } + return Context.getCurrentContext().newArray(this.scope, parents); + } + /** * @return the primary parent association so we can get at the association QName and the association type QName. */ @@ -2574,13 +2574,13 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol this.sourceAssocs = null; this.childAssocs = null; this.children = null; + this.parentAssocs = null; this.displayPath = null; this.isDocument = null; this.isContainer = null; this.parent = null; this.primaryParentAssoc = null; this.activeWorkflows = null; - this.parentAssocs = null; } /** diff --git a/source/java/org/alfresco/repo/jscript/test_childbynameparents.js b/source/java/org/alfresco/repo/jscript/test_childbynameparents.js new file mode 100644 index 0000000000..725d6052f4 --- /dev/null +++ b/source/java/org/alfresco/repo/jscript/test_childbynameparents.js @@ -0,0 +1,11 @@ +var child1 = companyhome.childByNamePath("/Data Dictionary"); +var child2 = companyhome.childByNamePath("/Data Dictionary/Scripts"); +var child3 = companyhome.childByNamePath("/Data Dictionary/Scripts/backup and log.js"); +logger.getSystem().out(child1 != null && child2 != null && child3 != null); + +var parentCount = child1.parentAssocs["contains"].length; + +var parents = child2.parents; + +var result = (child1 != null && child2 != null && child3 != null && parentCount == 1 && parents.length == 1); +result; \ No newline at end of file