From ba9512439ae53e547c8550d826e786e5d51caa50 Mon Sep 17 00:00:00 2001 From: Kevin Roast Date: Mon, 8 May 2006 14:55:05 +0000 Subject: [PATCH] . First pass of the Alfresco JavaScript API wiki docs: http://wiki.alfresco.com/wiki/JavaScript_API - JavaDoc and minor API updates that made sense as I was writing the documentation :) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2788 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../java/org/alfresco/repo/jscript/Node.java | 37 ++++++++++--- .../repo/jscript/RhinoScriptService.java | 39 +++++++++++-- .../org/alfresco/repo/jscript/Search.java | 55 ++++++++++++++----- .../service/cmr/repository/ScriptService.java | 8 +-- 4 files changed, 108 insertions(+), 31 deletions(-) diff --git a/source/java/org/alfresco/repo/jscript/Node.java b/source/java/org/alfresco/repo/jscript/Node.java index 8df8247728..176599ae2d 100644 --- a/source/java/org/alfresco/repo/jscript/Node.java +++ b/source/java/org/alfresco/repo/jscript/Node.java @@ -157,9 +157,9 @@ public final class Node implements Serializable return this.nodeRef; } - public NodeRef jsGet_nodeRef() + public String jsGet_nodeRef() { - return getNodeRef(); + return getNodeRef().toString(); } /** @@ -175,9 +175,9 @@ public final class Node implements Serializable return type; } - public QName jsGet_type() + public String jsGet_type() { - return getType(); + return getType().toString(); } /** @@ -443,9 +443,16 @@ public final class Node implements Serializable return this.aspects; } - public QName[] jsGet_aspects() + public String[] jsGet_aspects() { - return getAspects().toArray(new QName[getAspects().size()]); + Set aspects = getAspects(); + String[] result = new String[aspects.size()]; + int count = 0; + for (QName qname : aspects) + { + result[count++] = qname.toString(); + } + return result; } /** @@ -928,13 +935,27 @@ public final class Node implements Serializable } /** - * Copy this Node to a new parent destination. + * Copy this Node to a new parent destination. Note that children of the source + * Node are not copied. * * @param destination Node * * @return The newly copied Node instance or null if failed to copy. */ public Node copy(Node destination) + { + return copy(destination, false); + } + + /** + * Copy this Node and potentially all child nodes to a new parent destination. + * + * @param destination Node + * @param deepCopy True for a deep copy, false otherwise. + * + * @return The newly copied Node instance or null if failed to copy. + */ + public Node copy(Node destination, boolean deepCopy) { Node copy = null; @@ -947,7 +968,7 @@ public final class Node implements Serializable destination.getNodeRef(), ContentModel.ASSOC_CONTAINS, getPrimaryParentAssoc().getQName(), - false); + deepCopy); copy = new Node(copyRef, this.services, this.imageResolver); } } diff --git a/source/java/org/alfresco/repo/jscript/RhinoScriptService.java b/source/java/org/alfresco/repo/jscript/RhinoScriptService.java index 8945d66111..ea07f04f66 100644 --- a/source/java/org/alfresco/repo/jscript/RhinoScriptService.java +++ b/source/java/org/alfresco/repo/jscript/RhinoScriptService.java @@ -33,6 +33,7 @@ import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.ScriptException; import org.alfresco.service.cmr.repository.ScriptService; +import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.service.namespace.QName; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; @@ -244,24 +245,50 @@ public class RhinoScriptService implements ScriptService */ public static Map buildDefaultModel(ServiceRegistry services, NodeRef person, NodeRef companyHome, NodeRef userHome, NodeRef document, NodeRef space) + { + return buildDefaultModel(services, person, companyHome, userHome, document, space, null); + } + + /** + * Create the default data-model available to scripts as global scope level objects: + *

+ * 'companyhome' - the Company Home node
+ * 'userhome' - the current user home space node
+ * 'person' - the node representing the current user Person
+ * 'document' - document context node (may not be available)
+ * 'space' - space context node (may not be available) + * + * @param services ServiceRegistry + * @param person The current user Person Node + * @param companyHome The CompanyHome ref + * @param userHome The User home space ref + * @param document Optional ref to a document Node + * @param space Optional ref to a space Node + * @param resolver Image resolver to resolve icon images etc. + * + * @return A Map of global scope scriptable Node objects + */ + public static Map buildDefaultModel(ServiceRegistry services, + NodeRef person, NodeRef companyHome, NodeRef userHome, NodeRef document, NodeRef space, + TemplateImageResolver resolver) { Map model = new HashMap(); // add the well known node wrapper objects - model.put("companyhome", new Node(companyHome, services, null)); - model.put("userhome", new Node(userHome, services, null)); - model.put("person", new Node(person, services, null)); + model.put("companyhome", new Node(companyHome, services, resolver)); + model.put("userhome", new Node(userHome, services, resolver)); + model.put("person", new Node(person, services, resolver)); if (document != null) { - model.put("document", new Node(document, services, null)); + model.put("document", new Node(document, services, resolver)); } if (space != null) { - model.put("space", new Node(space, services, null)); + model.put("space", new Node(space, services, resolver)); } // add other useful util objects - model.put("search", new Search(services, companyHome.getStoreRef(), null)); + model.put("search", new Search(services, companyHome.getStoreRef(), resolver)); return model; } diff --git a/source/java/org/alfresco/repo/jscript/Search.java b/source/java/org/alfresco/repo/jscript/Search.java index 047aa31250..ae89a2e178 100644 --- a/source/java/org/alfresco/repo/jscript/Search.java +++ b/source/java/org/alfresco/repo/jscript/Search.java @@ -81,13 +81,20 @@ public final class Search */ public Node[] luceneSearch(String search) { - return query(search); + if (search != null && search.length() != 0) + { + return query(search); + } + else + { + return new Node[0]; + } } /** * Execute a saved Lucene search * - * @param savedSearch Node that contains the saved lucene search content + * @param savedSearch Node that contains the saved search XML content * * @return Node[] of results from the search - can be empty but not null */ @@ -98,19 +105,22 @@ public final class Search // read the Saved Search XML on the specified node - and get the Lucene search from it try { - ContentReader content = this.services.getContentService().getReader( - savedSearch.getNodeRef(), ContentModel.PROP_CONTENT); - if (content != null && content.exists()) + if (savedSearch != null) { - // get the root element - SAXReader reader = new SAXReader(); - Document document = reader.read(new StringReader(content.getContentString())); - Element rootElement = document.getRootElement(); - - Element queryElement = rootElement.element("query"); - if (queryElement != null) + ContentReader content = this.services.getContentService().getReader( + savedSearch.getNodeRef(), ContentModel.PROP_CONTENT); + if (content != null && content.exists()) { - search = queryElement.getText(); + // get the root element + SAXReader reader = new SAXReader(); + Document document = reader.read(new StringReader(content.getContentString())); + Element rootElement = document.getRootElement(); + + Element queryElement = rootElement.element("query"); + if (queryElement != null) + { + search = queryElement.getText(); + } } } } @@ -122,6 +132,25 @@ public final class Search return search != null ? query(search) : new Node[0]; } + /** + * Execute a saved Lucene search + * + * @param searchRef NodeRef string that points to the node containing saved search XML content + * + * @return Node[] of results from the search - can be empty but not null + */ + public Node[] savedSearch(String searchRef) + { + if (searchRef != null) + { + return savedSearch(new Node(new NodeRef(searchRef), services, null)); + } + else + { + return new Node[0]; + } + } + private Node[] query(String search) { Node[] nodes = null; diff --git a/source/java/org/alfresco/service/cmr/repository/ScriptService.java b/source/java/org/alfresco/service/cmr/repository/ScriptService.java index fc8cf67762..87598ca931 100644 --- a/source/java/org/alfresco/service/cmr/repository/ScriptService.java +++ b/source/java/org/alfresco/service/cmr/repository/ScriptService.java @@ -24,15 +24,15 @@ import org.alfresco.service.namespace.QName; /** * Script Service. *

- * Provides an interface to services for executing the JavaScript engine against a script file - * against a Java object based scripting data model. + * Provides an interface to services for executing a JavaScript engine script file against a + * Java object based scripting data-model. *

- * The template file can either be in the repository (passed as NodeRef string) or on the classpath. + * The script file can either be in the repository (passed as NodeRef string) or on the classpath. * Also a script String can be passed directly to the service via the executeScriptString() methods. * Java objects are passed into the scripting engine and methods can be accessed directly from the script. *

* A script is executed within a single transaction, any modifications to nodes or properties that fail - * and cause a rollback will rollback the entire script transaction. + * and cause a rollback which will rollback all repoistory modifications made by the script. * * @author Kevin Roast */