mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
. 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
This commit is contained in:
@@ -157,9 +157,9 @@ public final class Node implements Serializable
|
|||||||
return this.nodeRef;
|
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;
|
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;
|
return this.aspects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QName[] jsGet_aspects()
|
public String[] jsGet_aspects()
|
||||||
{
|
{
|
||||||
return getAspects().toArray(new QName[getAspects().size()]);
|
Set<QName> 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
|
* @param destination Node
|
||||||
*
|
*
|
||||||
* @return The newly copied Node instance or null if failed to copy.
|
* @return The newly copied Node instance or null if failed to copy.
|
||||||
*/
|
*/
|
||||||
public Node copy(Node destination)
|
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;
|
Node copy = null;
|
||||||
|
|
||||||
@@ -947,7 +968,7 @@ public final class Node implements Serializable
|
|||||||
destination.getNodeRef(),
|
destination.getNodeRef(),
|
||||||
ContentModel.ASSOC_CONTAINS,
|
ContentModel.ASSOC_CONTAINS,
|
||||||
getPrimaryParentAssoc().getQName(),
|
getPrimaryParentAssoc().getQName(),
|
||||||
false);
|
deepCopy);
|
||||||
copy = new Node(copyRef, this.services, this.imageResolver);
|
copy = new Node(copyRef, this.services, this.imageResolver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,6 +33,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
|
|||||||
import org.alfresco.service.cmr.repository.NodeService;
|
import org.alfresco.service.cmr.repository.NodeService;
|
||||||
import org.alfresco.service.cmr.repository.ScriptException;
|
import org.alfresco.service.cmr.repository.ScriptException;
|
||||||
import org.alfresco.service.cmr.repository.ScriptService;
|
import org.alfresco.service.cmr.repository.ScriptService;
|
||||||
|
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||||
import org.alfresco.service.namespace.QName;
|
import org.alfresco.service.namespace.QName;
|
||||||
import org.mozilla.javascript.Context;
|
import org.mozilla.javascript.Context;
|
||||||
import org.mozilla.javascript.Scriptable;
|
import org.mozilla.javascript.Scriptable;
|
||||||
@@ -244,24 +245,50 @@ public class RhinoScriptService implements ScriptService
|
|||||||
*/
|
*/
|
||||||
public static Map<String, Object> buildDefaultModel(ServiceRegistry services,
|
public static Map<String, Object> buildDefaultModel(ServiceRegistry services,
|
||||||
NodeRef person, NodeRef companyHome, NodeRef userHome, NodeRef document, NodeRef space)
|
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:
|
||||||
|
* <p>
|
||||||
|
* 'companyhome' - the Company Home node<br>
|
||||||
|
* 'userhome' - the current user home space node<br>
|
||||||
|
* 'person' - the node representing the current user Person<br>
|
||||||
|
* 'document' - document context node (may not be available)<br>
|
||||||
|
* '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<String, Object> buildDefaultModel(ServiceRegistry services,
|
||||||
|
NodeRef person, NodeRef companyHome, NodeRef userHome, NodeRef document, NodeRef space,
|
||||||
|
TemplateImageResolver resolver)
|
||||||
{
|
{
|
||||||
Map<String, Object> model = new HashMap<String, Object>();
|
Map<String, Object> model = new HashMap<String, Object>();
|
||||||
|
|
||||||
// add the well known node wrapper objects
|
// add the well known node wrapper objects
|
||||||
model.put("companyhome", new Node(companyHome, services, null));
|
model.put("companyhome", new Node(companyHome, services, resolver));
|
||||||
model.put("userhome", new Node(userHome, services, null));
|
model.put("userhome", new Node(userHome, services, resolver));
|
||||||
model.put("person", new Node(person, services, null));
|
model.put("person", new Node(person, services, resolver));
|
||||||
if (document != null)
|
if (document != null)
|
||||||
{
|
{
|
||||||
model.put("document", new Node(document, services, null));
|
model.put("document", new Node(document, services, resolver));
|
||||||
}
|
}
|
||||||
if (space != null)
|
if (space != null)
|
||||||
{
|
{
|
||||||
model.put("space", new Node(space, services, null));
|
model.put("space", new Node(space, services, resolver));
|
||||||
}
|
}
|
||||||
|
|
||||||
// add other useful util objects
|
// 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;
|
return model;
|
||||||
}
|
}
|
||||||
|
@@ -81,13 +81,20 @@ public final class Search
|
|||||||
*/
|
*/
|
||||||
public Node[] luceneSearch(String 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
|
* 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
|
* @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
|
// read the Saved Search XML on the specified node - and get the Lucene search from it
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ContentReader content = this.services.getContentService().getReader(
|
if (savedSearch != null)
|
||||||
savedSearch.getNodeRef(), ContentModel.PROP_CONTENT);
|
|
||||||
if (content != null && content.exists())
|
|
||||||
{
|
{
|
||||||
// get the root element
|
ContentReader content = this.services.getContentService().getReader(
|
||||||
SAXReader reader = new SAXReader();
|
savedSearch.getNodeRef(), ContentModel.PROP_CONTENT);
|
||||||
Document document = reader.read(new StringReader(content.getContentString()));
|
if (content != null && content.exists())
|
||||||
Element rootElement = document.getRootElement();
|
|
||||||
|
|
||||||
Element queryElement = rootElement.element("query");
|
|
||||||
if (queryElement != null)
|
|
||||||
{
|
{
|
||||||
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];
|
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)
|
private Node[] query(String search)
|
||||||
{
|
{
|
||||||
Node[] nodes = null;
|
Node[] nodes = null;
|
||||||
|
@@ -24,15 +24,15 @@ import org.alfresco.service.namespace.QName;
|
|||||||
/**
|
/**
|
||||||
* Script Service.
|
* Script Service.
|
||||||
* <p>
|
* <p>
|
||||||
* Provides an interface to services for executing the JavaScript engine against a script file
|
* Provides an interface to services for executing a JavaScript engine script file against a
|
||||||
* against a Java object based scripting data model.
|
* Java object based scripting data-model.
|
||||||
* <p>
|
* <p>
|
||||||
* 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.
|
* 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.
|
* Java objects are passed into the scripting engine and methods can be accessed directly from the script.
|
||||||
* <p>
|
* <p>
|
||||||
* A script is executed within a single transaction, any modifications to nodes or properties that fail
|
* 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
|
* @author Kevin Roast
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user