. Rhino JavaScript integration checkpoint:

- Added a new action to the repository for executing JavaScript files
  - Added script action UI to rule framework, means we can execute a JavaScript file as part of a rule
  - Lucene search and Saved Search functionality added to default data-model for scripts
  - Added Scripts folder to Data Dictionary (created during bootstrap)
  - Created patch to add the Scripts folder to existing schemas
  - Added ScriptService to ServiceRegistry bean

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2740 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2006-05-03 12:01:21 +00:00
parent 0f6eb410ed
commit c9e5bd6c22
17 changed files with 676 additions and 35 deletions

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.action.executer;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.jscript.RhinoScriptService;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.PersonService;
/**
* @author Kevin Roast
*/
public class ScriptActionExecutor extends ActionExecuterAbstractBase
{
public static final String NAME = "script";
public static final String PARAM_SCRIPTREF = "script-ref";
private ServiceRegistry serviceRegistry;
private PersonService personService;
private String companyHomePath;
private StoreRef storeRef;
/**
* @param serviceRegistry The serviceRegistry to set.
*/
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
}
/**
* @param personService The personService to set.
*/
public void setPersonService(PersonService personService)
{
this.personService = personService;
}
public void setStoreUrl(String storeUrl)
{
this.storeRef = new StoreRef(storeUrl);
}
public void setCompanyHomePath(String companyHomePath)
{
this.companyHomePath = companyHomePath;
}
/**
* @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef)
*/
protected void executeImpl(Action action, NodeRef actionedUponNodeRef)
{
NodeService nodeService = this.serviceRegistry.getNodeService();
if (nodeService.exists(actionedUponNodeRef))
{
NodeRef scriptRef = (NodeRef)action.getParameterValue(PARAM_SCRIPTREF);
if (nodeService.exists(scriptRef))
{
// get the references we need to build the default scripting data-model
String userName = this.serviceRegistry.getAuthenticationService().getCurrentUserName();
NodeRef personRef = this.personService.getPerson(userName);
NodeRef homeSpaceRef = (NodeRef)nodeService.getProperty(personRef, ContentModel.PROP_HOMEFOLDER);
// the default scripting model provides access to well known objects and searching
// facilities - it also provides basic create/update/delete/copy/move services
Map<String, Object> model = RhinoScriptService.buildDefaultModel(
this.serviceRegistry,
personRef,
getCompanyHome(),
homeSpaceRef,
actionedUponNodeRef,
actionedUponNodeRef);
// execute the script against the default model
this.serviceRegistry.getScriptService().executeScript(
scriptRef,
ContentModel.PROP_CONTENT,
model);
}
}
}
/**
* @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
*/
protected void addParameterDefintions(List<ParameterDefinition> paramList)
{
paramList.add(new ParameterDefinitionImpl(PARAM_SCRIPTREF, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_SCRIPTREF)));
}
private NodeRef getCompanyHome()
{
NodeRef companyHomeRef;
List<NodeRef> refs = this.serviceRegistry.getSearchService().selectNodes(
this.serviceRegistry.getNodeService().getRootNode(storeRef),
companyHomePath,
null,
this.serviceRegistry.getNamespaceService(),
false);
if (refs.size() != 1)
{
throw new IllegalStateException("Invalid company home path: " + companyHomePath + " - found: " + refs.size());
}
companyHomeRef = refs.get(0);
return companyHomeRef;
}
}

View File

@@ -0,0 +1,254 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.admin.patch.impl;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.admin.patch.AbstractPatch;
import org.alfresco.repo.importer.ImporterBootstrap;
import org.alfresco.service.cmr.admin.PatchException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.springframework.context.MessageSource;
/**
* Ensures that the <b>scripts</b> folder is present.
* <p>
* This uses the bootstrap importer to get the paths to look for. If not present,
* the required structures are created.
* <p>
* This class should be replaced with a more generic <code>ImporterPatch</code>
* that can do conditional importing into given locations.
* <p>
* JIRA: {@link http://www.alfresco.org/jira/browse/AR-342 AR-342}
*
* @author Kevin Roast
*/
public class ScriptsFolderPatch extends AbstractPatch
{
private static final String MSG_EXISTS = "patch.scriptsFolder.result.exists";
private static final String MSG_CREATED = "patch.scriptsFolder.result.created";
public static final String PROPERTY_COMPANY_HOME_CHILDNAME = "spaces.company_home.childname";
public static final String PROPERTY_DICTIONARY_CHILDNAME = "spaces.dictionary.childname";
public static final String PROPERTY_SCRIPTS_FOLDER_CHILDNAME = "spaces.scripts.childname";
private static final String PROPERTY_SCRIPTS_FOLDER_NAME = "spaces.scripts.name";
private static final String PROPERTY_SCRIPTS_FOLDER_DESCRIPTION = "spaces.scripts.description";
private static final String PROPERTY_ICON = "space-icon-default";
private ImporterBootstrap importerBootstrap;
private MessageSource messageSource;
protected NodeRef dictionaryNodeRef;
protected Properties configuration;
protected NodeRef scriptsFolderNodeRef;
public void setImporterBootstrap(ImporterBootstrap importerBootstrap)
{
this.importerBootstrap = importerBootstrap;
}
public void setMessageSource(MessageSource messageSource)
{
this.messageSource = messageSource;
}
/**
* Ensure that required common properties have been set
*/
protected void checkCommonProperties() throws Exception
{
if (importerBootstrap == null)
{
throw new PatchException("'importerBootstrap' property has not been set");
}
else if (namespaceService == null)
{
throw new PatchException("'namespaceService' property has not been set");
}
else if (searchService == null)
{
throw new PatchException("'searchService' property has not been set");
}
else if (nodeService == null)
{
throw new PatchException("'nodeService' property has not been set");
}
}
/**
* Extracts pertinent references and properties that are common to execution
* of this and derived patches.
*/
protected void setUp() throws Exception
{
// get the node store that we must work against
StoreRef storeRef = importerBootstrap.getStoreRef();
if (storeRef == null)
{
throw new PatchException("Bootstrap store has not been set");
}
NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
this.configuration = importerBootstrap.getConfiguration();
// get the association names that form the path
String companyHomeChildName = configuration.getProperty(PROPERTY_COMPANY_HOME_CHILDNAME);
if (companyHomeChildName == null || companyHomeChildName.length() == 0)
{
throw new PatchException("Bootstrap property '" + PROPERTY_COMPANY_HOME_CHILDNAME + "' is not present");
}
String dictionaryChildName = configuration.getProperty(PROPERTY_DICTIONARY_CHILDNAME);
if (dictionaryChildName == null || dictionaryChildName.length() == 0)
{
throw new PatchException("Bootstrap property '" + PROPERTY_DICTIONARY_CHILDNAME + "' is not present");
}
String scriptsChildName = configuration.getProperty(PROPERTY_SCRIPTS_FOLDER_CHILDNAME);
if (scriptsChildName == null || scriptsChildName.length() == 0)
{
throw new PatchException("Bootstrap property '" + PROPERTY_SCRIPTS_FOLDER_CHILDNAME + "' is not present");
}
// build the search string to get the dictionary node
StringBuilder sb = new StringBuilder(256);
sb.append("/").append(companyHomeChildName)
.append("/").append(dictionaryChildName);
String xpath = sb.toString();
// get the dictionary node
List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, xpath, null, namespaceService, false);
if (nodeRefs.size() == 0)
{
throw new PatchException("XPath didn't return any results: \n" +
" root: " + storeRootNodeRef + "\n" +
" xpath: " + xpath);
}
else if (nodeRefs.size() > 1)
{
throw new PatchException("XPath returned too many results: \n" +
" root: " + storeRootNodeRef + "\n" +
" xpath: " + xpath + "\n" +
" results: " + nodeRefs);
}
this.dictionaryNodeRef = nodeRefs.get(0);
// Now we have the optional part. Check for the existence of the scripts folder
xpath = scriptsChildName;
nodeRefs = searchService.selectNodes(dictionaryNodeRef, xpath, null, namespaceService, false);
if (nodeRefs.size() > 1)
{
throw new PatchException("XPath returned too many results: \n" +
" dictionary node: " + dictionaryNodeRef + "\n" +
" xpath: " + xpath + "\n" +
" results: " + nodeRefs);
}
else if (nodeRefs.size() == 0)
{
// the node does not exist
this.scriptsFolderNodeRef = null;
}
else
{
// we have the saved searches folder noderef
this.scriptsFolderNodeRef = nodeRefs.get(0);
}
}
@Override
protected String applyInternal() throws Exception
{
// properties must be set
checkCommonProperties();
if (messageSource == null)
{
throw new PatchException("'messageSource' property has not been set");
}
// get useful values
setUp();
String msg = null;
if (scriptsFolderNodeRef == null)
{
// create it
createFolder();
msg = I18NUtil.getMessage(MSG_CREATED, scriptsFolderNodeRef);
}
else
{
// it already exists
msg = I18NUtil.getMessage(MSG_EXISTS, scriptsFolderNodeRef);
}
// done
return msg;
}
private void createFolder()
{
// get required properties
String savedSearchesChildName = configuration.getProperty(PROPERTY_SCRIPTS_FOLDER_CHILDNAME);
if (savedSearchesChildName == null)
{
throw new PatchException("Bootstrap property '" + PROPERTY_SCRIPTS_FOLDER_CHILDNAME + "' is not present");
}
String savedSearchesName = messageSource.getMessage(
PROPERTY_SCRIPTS_FOLDER_NAME,
null,
I18NUtil.getLocale());
if (savedSearchesName == null || savedSearchesName.length() == 0)
{
throw new PatchException("Bootstrap property '" + PROPERTY_SCRIPTS_FOLDER_NAME + "' is not present");
}
String savedSearchesDescription = messageSource.getMessage(
PROPERTY_SCRIPTS_FOLDER_DESCRIPTION,
null,
I18NUtil.getLocale());
if (savedSearchesDescription == null || savedSearchesDescription.length() == 0)
{
throw new PatchException("Bootstrap property '" + PROPERTY_SCRIPTS_FOLDER_DESCRIPTION + "' is not present");
}
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(7);
properties.put(ContentModel.PROP_NAME, savedSearchesName);
properties.put(ContentModel.PROP_TITLE, savedSearchesName);
properties.put(ContentModel.PROP_DESCRIPTION, savedSearchesDescription);
properties.put(ContentModel.PROP_ICON, PROPERTY_ICON);
// create the node
ChildAssociationRef childAssocRef = nodeService.createNode(
dictionaryNodeRef,
ContentModel.ASSOC_CONTAINS,
QName.resolveToQName(namespaceService, savedSearchesChildName),
ContentModel.TYPE_FOLDER,
properties);
scriptsFolderNodeRef = childAssocRef.getChildRef();
// add the required aspects
nodeService.addAspect(scriptsFolderNodeRef, ContentModel.ASPECT_UIFACETS, null);
// done
}
}

View File

@@ -54,6 +54,7 @@ public class MimetypeMap implements MimetypeService
public static final String MIMETYPE_IMAGE_GIF = "image/gif";
public static final String MIMETYPE_IMAGE_JPEG = "image/jpeg";
public static final String MIMETYPE_IMAGE_RGB = "image/x-rgb";
public static final String MIMETYPE_JAVASCRIPT = "application/x-javascript";
// Open Document
public static final String MIMETYPE_OPENDOCUMENT_TEXT = "application/vnd.oasis.opendocument.text";
public static final String MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE = "application/vnd.oasis.opendocument.text-template";

View File

@@ -38,7 +38,6 @@ import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.apache.commons.logging.Log;
@@ -46,12 +45,11 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;
/**
* Node class implementation specific for use by ScriptService as part of the object model.
* <p>
* Node class implementation, specific for use by ScriptService as part of the object model.
* <p>
* The class exposes Node properties, children and assocs as dynamically populated maps and lists.
* The various collection classes are mirrored as JavaScript properties. So can be accessed using
* standard JavaScript syntax, such as <code>node.children[0].properties.name</code>.
* standard JavaScript property syntax, such as <code>node.children[0].properties.name</code>.
* <p>
* Various helper methods are provided to access common and useful node variables such
* as the content url and type information.
@@ -270,24 +268,6 @@ public final class Node implements Serializable
// return childrenByXPath(xpath);
//}
/**
* @return A map capable of returning a List of Node objects from an NodeRef to a Saved Search
* object. The Saved Search is executed and the resulting nodes supplied as a sequence.
*/
//public Map getChildrenBySavedSearch()
//{
// return new SavedSearchResultsMap(this, this.services);
//}
/**
* @return A map capable of returning a List of Node objects from an NodeRef to a Lucene search
* string. The Saved Search is executed and the resulting nodes supplied as a sequence.
*/
//public Map getChildrenByLuceneSearch()
//{
// return new LuceneSearchResultsMap(this, this.services);
//}
/**
* Return the associations for this Node. As a Map of assoc name to an Array of Nodes.
*

View File

@@ -21,10 +21,12 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -221,4 +223,46 @@ public class RhinoScriptService implements ScriptService
cx.exit();
}
}
/**
* 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
*
* @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)
{
Map<String, Object> model = new HashMap<String, Object>();
// 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));
if (document != null)
{
model.put("document", new Node(document, services, null));
}
if (space != null)
{
model.put("space", new Node(space, services, null));
}
// add other useful util objects
model.put("search", new Search(services, companyHome.getStoreRef(), null));
return model;
}
}

View File

@@ -0,0 +1,163 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.jscript;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.cmr.search.SearchService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* Search component for use by the ScriptService.
* <p>
* Provides access to Lucene search facilities including saved search objects. The results
* from a search are returned as an array (collection) of scriptable Node wrapper objects.
* <p>
* The object is added to the root of the model to provide syntax such as:
* <code>var results = search.luceneSearch(statement);</code>
* and
* <code>var results = search.savedSearch(node);</code>
*
* @author Kevin Roast
*/
public final class Search
{
private static Log logger = LogFactory.getLog(Search.class);
private ServiceRegistry services;
private StoreRef storeRef;
private TemplateImageResolver imageResolver;
/**
* Constructor
*
* @param services The ServiceRegistry to use
*/
public Search(ServiceRegistry services, StoreRef storeRef, TemplateImageResolver imageResolver)
{
this.services = services;
this.storeRef = storeRef;
this.imageResolver = imageResolver;
}
/**
* Execute a Lucene search
*
* @param search Lucene search string to execute
*
* @return Node[] of results from the search - can be empty but not null
*/
public Node[] luceneSearch(String search)
{
return query(search);
}
/**
* Execute a saved Lucene search
*
* @param savedSearch Node that contains the saved lucene search content
*
* @return Node[] of results from the search - can be empty but not null
*/
public Node[] savedSearch(Node savedSearch)
{
String search = null;
// 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())
{
// 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();
}
}
}
catch (Throwable err)
{
throw new AlfrescoRuntimeException("Failed to find or load saved Search: " + savedSearch.getNodeRef(), err);
}
return search != null ? query(search) : new Node[0];
}
private Node[] query(String search)
{
Node[] nodes = null;
// perform the search against the repo
ResultSet results = null;
try
{
results = this.services.getSearchService().query(
this.storeRef,
SearchService.LANGUAGE_LUCENE,
search);
if (results.length() != 0)
{
nodes = new Node[results.length()];
int count = 0;
for (ResultSetRow row: results)
{
NodeRef nodeRef = row.getNodeRef();
nodes[count++] = new Node(nodeRef, services, this.imageResolver);
}
}
}
catch (Throwable err)
{
throw new AlfrescoRuntimeException("Failed to execute search: " + search, err);
}
finally
{
if (results != null)
{
results.close();
}
}
return nodes != null ? nodes : new Node[0];
}
}

View File

@@ -33,6 +33,7 @@ import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.search.CategoryService;
@@ -302,6 +303,14 @@ public class ServiceDescriptorRegistry
*/
public FileFolderService getFileFolderService()
{
return (FileFolderService) getService(FILE_FOLDER_SERVICE);
return (FileFolderService)getService(FILE_FOLDER_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getScriptService()
*/
public ScriptService getScriptService()
{
return (ScriptService)getService(SCRIPT_SERVICE);
}
}

View File

@@ -27,6 +27,7 @@ import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.search.CategoryService;
@@ -79,6 +80,7 @@ public interface ServiceRegistry
static final QName AUTHORITY_SERVICE = QName.createQName(NamespaceService.ALFRESCO_URI, "AuthorityService");
static final QName TEMPLATE_SERVICE = QName.createQName(NamespaceService.ALFRESCO_URI, "TemplateService");
static final QName FILE_FOLDER_SERVICE = QName.createQName(NamespaceService.ALFRESCO_URI, "FileFolderService");
static final QName SCRIPT_SERVICE = QName.createQName(NamespaceService.ALFRESCO_URI, "ScriptService");
/**
* Get the list of services provided by the Repository
@@ -220,4 +222,9 @@ public interface ServiceRegistry
* @return the file-folder manipulation service (or null if one is not provided)
*/
FileFolderService getFileFolderService();
/**
* @return the script execution service (or null if one is not provided)
*/
ScriptService getScriptService();
}