mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
. 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:
@@ -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.
|
||||
*
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
163
source/java/org/alfresco/repo/jscript/Search.java
Normal file
163
source/java/org/alfresco/repo/jscript/Search.java
Normal 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];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user