diff --git a/config/alfresco/script-services-context.xml b/config/alfresco/script-services-context.xml index a5e04b315e..624a09c117 100644 --- a/config/alfresco/script-services-context.xml +++ b/config/alfresco/script-services-context.xml @@ -143,12 +143,12 @@ - + workflow - - + + diff --git a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowInstance.java b/source/java/org/alfresco/repo/workflow/jsapi/WorkflowInstance.java deleted file mode 100644 index 883bbf81e5..0000000000 --- a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowInstance.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (C) 2005-2007 Alfresco Software Limited. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - * As a special exception to the terms and conditions of version 2.0 of - * the GPL, you may redistribute this Program in connection with Free/Libre - * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have received a copy of the text describing - * the FLOSS exception, and it is also available here: - * http://www.alfresco.com/legal/licensing" - */ -package org.alfresco.repo.workflow.jsapi; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import org.alfresco.service.cmr.workflow.WorkflowService; - -/** - * Class representing an active or in-flight workflow - * - * @author glenj - * - */ -public class WorkflowInstance -{ - /** Workflow Manager reference */ - private WorkflowService workflowService; - - /** Workflow instance id */ - private final String id; - - /** Workflow instance description */ - private final String description; - - /** Flag this Workflow instance as active-'true' or complete-'false' */ - private boolean active; - - /** Workflow instance start date */ - private Date startDate; - - /** Workflow instance end date */ - private Date endDate; - - /** - * Creates a new instance of WorkflowInstance - * - * @param id - * @param description - * @param active - * @param startDate - * @param workflowService reference to the Workflow Service - */ - public WorkflowInstance(final String id, final String description, final Date startDate, - final WorkflowService workflowService) - { - this.id = id; - this.description = description; - this.active = true; - this.startDate = startDate; - this.workflowService = workflowService; - } - - /** - * Create a new instance of WorkflowInstance from a - * WorkflowInstance object from the CMR workflow object model - * - * @param cmrWorkflowInstance CMR workflow instance - * @param workflowService reference to the Workflow Service - */ - public WorkflowInstance(final org.alfresco.service.cmr.workflow.WorkflowInstance - cmrWorkflowInstance, final WorkflowService workflowService) - { - this.id = cmrWorkflowInstance.id; - this.description = cmrWorkflowInstance.description; - this.active = cmrWorkflowInstance.active; - this.startDate = cmrWorkflowInstance.startDate; - this.workflowService = workflowService; - } - - /** - * Get all paths for the specified workflow instance - */ - public List getPaths(final String instanceId) - { - List cmrPaths = workflowService.getWorkflowPaths(instanceId); - List paths = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowPath cmrPath : cmrPaths) - { - paths.add(new WorkflowPath(cmrPath, workflowService)); - } - return paths; - } - - /** - * Getter for 'id' property - * - * @return the id - */ - public String getId() - { - return id; - } - - /** - * Getter for 'description' property - * - * @return the description - */ - public String getDescription() - { - return description; - } - - /** - * Get state for 'active' property - * - * @return the active - */ - public boolean isActive() - { - return active; - } - - /** - * Getter for 'startDate' property - * - * @return the startDate - */ - public Date getStartDate() - { - return startDate; - } - - /** - * Getter for 'endDate' property - * - * @return the endDate - */ - public Date getEndDate() - { - return endDate; - } - - /** - * Cancel workflow instance - */ - public void cancel() - { - workflowService.cancelWorkflow(this.id); - } - - /** - * Delete workflow instance - */ - public void delete() - { - workflowService.deleteWorkflow(this.id); - } -} diff --git a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowManager.java b/source/java/org/alfresco/repo/workflow/jsapi/WorkflowManager.java deleted file mode 100644 index 1af3b6563c..0000000000 --- a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowManager.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2005-2007 Alfresco Software Limited. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - * As a special exception to the terms and conditions of version 2.0 of - * the GPL, you may redistribute this Program in connection with Free/Libre - * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have received a copy of the text describing - * the FLOSS exception, and it is also available here: - * http://www.alfresco.com/legal/licensing" - */ -package org.alfresco.repo.workflow.jsapi; - -import java.util.ArrayList; -import java.util.List; - -import org.alfresco.repo.processor.BaseProcessorExtension; -import org.alfresco.service.cmr.workflow.WorkflowService; -import org.alfresco.service.cmr.workflow.WorkflowTaskState; - -/** - * The Workflow Manager serves as the main entry point for scripts - * to create and interact with workflows. - * It is made available in the root scripting scope - * - * @author glenj - * - */ -public class WorkflowManager extends BaseProcessorExtension -{ - /** Workflow Service to make calls to workflow service API */ - private WorkflowService workflowService; - - /** - * Get deployed workflow definition by ID - * - * @param id the workflow definition ID - * @return the workflow definition matching the given ID - */ - public WorkflowDefinition getDefinition(String id) - { - org.alfresco.service.cmr.workflow.WorkflowDefinition cmrWorkflowDefinition = - workflowService.getDefinitionById(id); - return new WorkflowDefinition(cmrWorkflowDefinition, workflowService); - } - - /** - * Set the workflow service property - * - * @param workflowService the workflow service - */ - public void setWorkflowService(final WorkflowService workflowService) - { - this.workflowService = workflowService; - } - - /** - * Get assigned tasks - * - * @param authority the authority - * @param state filter by specified workflow task state - * @return the list of assigned tasks - */ - public List getAssignedTasks(final String authority, final WorkflowTaskState state) - { - List cmrAssignedTasks = workflowService.getAssignedTasks(authority, state); - List assignedTasks = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowTask cmrTask : cmrAssignedTasks) - { - assignedTasks.add(new WorkflowTask(cmrTask, workflowService)); - } - - return assignedTasks; - } - - /** - * Get Workflow Instance by ID - * - * @param workflowInstanceID ID of the workflow instance to retrieve - * @return the workflow instance for the given ID - */ - public WorkflowInstance getInstance(String workflowInstanceID) - { - org.alfresco.service.cmr.workflow.WorkflowInstance cmrWorkflowInstance = workflowService.getWorkflowById(workflowInstanceID); - return new WorkflowInstance(cmrWorkflowInstance, workflowService); - } - - /** - * Get pooled tasks - * - * @param authority the authority - * @param state filter by specified workflow task state - * @return the list of assigned tasks - */ - public List getPooledTasks(final String authority, final WorkflowTaskState state) - { - List cmrPooledTasks = workflowService.getPooledTasks(authority); - List pooledTasks = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowTask cmrPooledTask : cmrPooledTasks) - { - pooledTasks.add(new WorkflowTask(cmrPooledTask, workflowService)); - } - - return pooledTasks; - } - - /** - * Get task by id - * - * @param id task id - * @return the task (null if not found) - */ - public WorkflowTask getTask(String id) - { - org.alfresco.service.cmr.workflow.WorkflowTask cmrWorkflowTask = workflowService.getTaskById(id); - return new WorkflowTask(cmrWorkflowTask, workflowService); - } - - /** - * Gets the latest versions of the deployed, workflow definitions - * - * @return the latest versions of the deployed workflow definitions - */ - public List getLatestDefinitions() - { - List cmrDefinitions = workflowService.getDefinitions(); - List workflowDefs = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowDefinition cmrDefinition : cmrDefinitions) - { - workflowDefs.add(new WorkflowDefinition(cmrDefinition, workflowService)); - } - - return workflowDefs; - } - - /** - * Gets all versions of the deployed workflow definitions - * - * @return all versions of the deployed workflow definitions - */ - public List getAllDefinitions() - { - List cmrDefinitions = workflowService.getAllDefinitions(); - List workflowDefs = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowDefinition cmrDefinition : cmrDefinitions) - { - workflowDefs.add(new WorkflowDefinition(cmrDefinition, workflowService)); - } - - return workflowDefs; - } -} diff --git a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowDefinition.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowDefinition.java similarity index 50% rename from source/java/org/alfresco/repo/workflow/jsapi/WorkflowDefinition.java rename to source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowDefinition.java index 72c6c7b236..5d49c74145 100644 --- a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowDefinition.java +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowDefinition.java @@ -22,20 +22,29 @@ * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ -package org.alfresco.repo.workflow.jsapi; +package org.alfresco.repo.workflow.jscript; import java.io.Serializable; import java.util.ArrayList; import java.util.List; -import java.util.Map; +import org.alfresco.repo.jscript.ScriptNode; +import org.alfresco.repo.jscript.ScriptableQNameMap; +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.repo.workflow.WorkflowModel; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.workflow.WorkflowDefinition; +import org.alfresco.service.cmr.workflow.WorkflowInstance; +import org.alfresco.service.cmr.workflow.WorkflowPath; import org.alfresco.service.cmr.workflow.WorkflowService; -import org.alfresco.service.namespace.QName; +import org.mozilla.javascript.Scriptable; -public class WorkflowDefinition +public class JscriptWorkflowDefinition implements Serializable { - /** Workflow Service reference */ - private WorkflowService workflowService; + static final long serialVersionUID = 1641614201321129544L; + + /** Service Registry */ + private ServiceRegistry serviceRegistry; /** Workflow definition id */ private final String id; @@ -52,46 +61,55 @@ public class WorkflowDefinition /** Workflow definition description */ private final String description; + /** Root scripting scope for this object */ + private final Scriptable scope; + /** * Create a new instance of WorkflowDefinition from a * CMR workflow object model WorkflowDefinition instance * * @param cmrWorkflowDefinition an instance of WorkflowDefinition from the CMR workflow object model - * @param workflowService reference to the Workflow Service + * @param serviceRegistry reference to the Service Registry + * @param scope the root scripting scope for this object */ - public WorkflowDefinition(final org.alfresco.service.cmr.workflow.WorkflowDefinition cmrWorkflowDefinition, - final WorkflowService workflowService) + JscriptWorkflowDefinition(final WorkflowDefinition cmrWorkflowDefinition, + final ServiceRegistry serviceRegistry, final Scriptable scope) { this.id = cmrWorkflowDefinition.id; this.name = cmrWorkflowDefinition.name; this.version = cmrWorkflowDefinition.version; this.title = cmrWorkflowDefinition.title; this.description = cmrWorkflowDefinition.description; - this.workflowService = workflowService; + this.serviceRegistry = serviceRegistry; + this.scope = scope; } /** - * Creates a new instance of WorkflowDefinition + * Creates a new instance of WorkflowDefinition from scratch * * @param id workflow definition ID * @param name name of workflow definition * @param version version of workflow definition * @param title title of workflow definition * @param description description of workflow definition - * @param workflowService reference to the Workflow Service + * @param serviceRegistry reference to the Service Registry + * @param scope root scripting scope for this object */ - public WorkflowDefinition(final String id, final String name, final String version, - final String title, final String description, WorkflowService workflowService) + JscriptWorkflowDefinition(final String id, final String name, final String version, + final String title, final String description, ServiceRegistry serviceRegistry, + final Scriptable scope) { this.id = id; this.name = name; this.version = version; this.title = title; this.description = description; + this.serviceRegistry = serviceRegistry; + this.scope = scope; } /** - * Get value of 'id' property + * Get value of id property * * @return the id */ @@ -101,7 +119,7 @@ public class WorkflowDefinition } /** - * Get value of 'name' property + * Get value of name property * * @return the name */ @@ -111,7 +129,7 @@ public class WorkflowDefinition } /** - * Get value of 'version' property + * Get value of version property * * @return the version */ @@ -121,7 +139,7 @@ public class WorkflowDefinition } /** - * Get value of 'title' property + * Get value of title property * * @return the title */ @@ -131,7 +149,7 @@ public class WorkflowDefinition } /** - * Get value of 'description' property + * Get value of description property * * @return the description */ @@ -143,15 +161,23 @@ public class WorkflowDefinition /** * Start workflow instance from workflow definition * - * @param properties properties (map of key-value pairs used to populate the + * @param workflowPackage workflow package object to 'attach' to the new workflow + * instance + * @param properties properties (map of key-value pairs) used to populate the * start task properties * @return the initial workflow path */ - public WorkflowPath startWorkflow(Map properties) + @SuppressWarnings("unchecked") + public JscriptWorkflowPath startWorkflow(ScriptNode workflowPackage, + ScriptableQNameMap properties) { - org.alfresco.service.cmr.workflow.WorkflowPath cmrWorkflowPath = + WorkflowService workflowService = this.serviceRegistry.getWorkflowService(); + + properties.put(WorkflowModel.ASPECT_WORKFLOW_PACKAGE, workflowPackage); + + WorkflowPath cmrWorkflowPath = workflowService.startWorkflow(id, properties); - return new WorkflowPath(cmrWorkflowPath, workflowService); + return new JscriptWorkflowPath(cmrWorkflowPath, this.serviceRegistry, this.scope); } /** @@ -159,15 +185,20 @@ public class WorkflowDefinition * * @return the active workflow instances spawned from this workflow definition */ - public synchronized List getActiveInstances() + public synchronized Scriptable getActiveInstances() { - List cmrWorkflowInstances = workflowService.getActiveWorkflows(this.id); - List activeInstances = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowInstance cmrWorkflowInstance : cmrWorkflowInstances) + WorkflowService workflowService = this.serviceRegistry.getWorkflowService(); + + List cmrWorkflowInstances = workflowService.getActiveWorkflows(this.id); + ArrayList activeInstances = new ArrayList(); + for (WorkflowInstance cmrWorkflowInstance : cmrWorkflowInstances) { - activeInstances.add(new WorkflowInstance(cmrWorkflowInstance, workflowService)); + activeInstances.add(new JscriptWorkflowInstance(cmrWorkflowInstance, this.serviceRegistry, this.scope)); } - return activeInstances; + Scriptable activeInstancesScriptable = + (Scriptable)new ValueConverter().convertValueForScript(this.serviceRegistry, this.scope, null, activeInstances); + + return activeInstancesScriptable; } } diff --git a/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowInstance.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowInstance.java new file mode 100644 index 0000000000..e63a0edfc9 --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowInstance.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.workflow.jscript; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.workflow.WorkflowInstance; +import org.alfresco.service.cmr.workflow.WorkflowPath; +import org.alfresco.service.cmr.workflow.WorkflowService; +import org.mozilla.javascript.Scriptable; + +/** + * Class representing an active or in-flight workflow + * + * @author glenj + * + */ +public class JscriptWorkflowInstance implements Serializable +{ + static final long serialVersionUID = 1015996328908978487L; + + /** Service Registry object */ + private final ServiceRegistry serviceRegistry; + + /** Root scripting scope for this object */ + private final Scriptable scope; + + /** Workflow instance id */ + private final String id; + + /** Workflow instance description */ + private final String description; + + /** Workflow instance start date */ + private final Date startDate; + + /** Workflow instance end date */ + private final Date endDate; + + /** Flag this Workflow instance as active-'true' or complete-'false' */ + private final boolean active; + + /** + * Creates a new instance of WorkflowInstance from + * scratch + * + * @param id ID of new workflow instance object + * @param description Description of new workflow instance object + * @param startDate Start Date of new workflow instance object + * @param serviceRegistry Service Registry instance + * @param scope the root scripting scope for this object + */ + JscriptWorkflowInstance(final String id, final String description, final Date startDate, + final ServiceRegistry serviceRegistry, final Scriptable scope) + { + this.id = id; + this.description = description; + this.active = true; + this.startDate = startDate; + this.endDate = null; + this.serviceRegistry = serviceRegistry; + this.scope = scope; + } + + /** + * Create a new instance of WorkflowInstance from a + * WorkflowInstance object from the CMR workflow object model + * + * @param cmrWorkflowInstance CMR workflow instance + * @param serviceRegistry Service Registry instance + * @param scope the root scripting scope for this object + */ + JscriptWorkflowInstance(final WorkflowInstance + cmrWorkflowInstance, final ServiceRegistry serviceRegistry, final Scriptable scope) + { + this.id = cmrWorkflowInstance.id; + this.description = cmrWorkflowInstance.description; + this.active = cmrWorkflowInstance.active; + this.startDate = cmrWorkflowInstance.startDate; + this.endDate = cmrWorkflowInstance.endDate; + this.serviceRegistry = serviceRegistry; + this.scope = scope; + } + + /** + * Get all paths for the specified workflow instance + */ + public Scriptable getPaths() + { + WorkflowService workflowService = serviceRegistry.getWorkflowService(); + + List cmrPaths = workflowService.getWorkflowPaths(this.id); + ArrayList paths = new ArrayList(); + for (WorkflowPath cmrPath : cmrPaths) + { + paths.add(new JscriptWorkflowPath(cmrPath, this.serviceRegistry, this.scope)); + } + + Scriptable pathsScriptable = + (Scriptable)new ValueConverter().convertValueForScript(this.serviceRegistry, this.scope, null, paths); + + return pathsScriptable; + } + + /** + * Getter for id property + * + * @return the id + */ + public String getId() + { + return id; + } + + /** + * Getter for description property + * + * @return the description + */ + public String getDescription() + { + return description; + } + + /** + * Get state for active property + * + * @return the active + */ + public boolean isActive() + { + return active; + } + + /** + * Getter for startDate property + * + * @return the startDate + */ + public Scriptable getStartDate() + { + return (Scriptable)new ValueConverter().convertValueForScript( + this.serviceRegistry, this.scope, null, this.startDate); + } + + /** + * Getter for endDate property + * + * @return the endDate + */ + public Scriptable getEndDate() + { + return (Scriptable)new ValueConverter().convertValueForScript( + this.serviceRegistry, this.scope, null, this.endDate); + } + + /** + * Cancel workflow instance + */ + public void cancel() + { + serviceRegistry.getWorkflowService().cancelWorkflow(this.id); + } + + /** + * Delete workflow instance + */ + public void delete() + { + serviceRegistry.getWorkflowService().deleteWorkflow(this.id); + } +} diff --git a/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowNode.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowNode.java new file mode 100644 index 0000000000..b6e98468e4 --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowNode.java @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ + +package org.alfresco.repo.workflow.jscript; + +import java.io.Serializable; +import java.util.ArrayList; + +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.workflow.WorkflowNode; +import org.alfresco.service.cmr.workflow.WorkflowTransition; +import org.mozilla.javascript.Scriptable; + +/** + * Represents a Workflow Node within the Workflow Definition. + */ +public class JscriptWorkflowNode implements Serializable +{ + static final long serialVersionUID = 6785972019256246499L; + + /** Name of workflow node */ + private final String name; + + /** Localised title of workflow node */ + private final String title; + + /** Localised description of workflow node */ + private final String description; + + /** true if this workflow node is associated with a workflow task */ + private final boolean isTaskNode; + + /** The transitions leaving this node (or null, if none) */ + private final ArrayList transitions; + + /** Root scripting scope for this object */ + private final Scriptable scope; + + /** Service Registry */ + private final ServiceRegistry serviceRegistry; + + /** + * Constructor to create a new instance of this class + * + * @param name Name of workflow node + * @param title Title of workflow node + * @param description Description of workflow node + * @param isTaskNode true if this node is a task node + * @param transitions transitions leaving this node (null if none) + * @param scope root scripting scope for this object + * @param serviceRegistry service registry object + */ + JscriptWorkflowNode(String name, String title, String description, + boolean isTaskNode, ArrayList transitions, + Scriptable scope, ServiceRegistry serviceRegistry) + { + this.name = name; + this.title = title; + this.description = description; + this.isTaskNode = isTaskNode; + this.transitions = transitions; + this.scope = scope; + this.serviceRegistry = serviceRegistry; + } + + /** + * Constructor to create a new instance of this class from an + * existing instance of WorkflowNode from the CMR workflow + * object model + * + * @param workflowNode CMR workflow node object to create + * new JscriptWorkflowNode from + * @param scope root scripting scope for this newly instantiated object + * @param serviceRegistry service registry object + */ + JscriptWorkflowNode(WorkflowNode workflowNode, Scriptable scope, ServiceRegistry serviceRegistry) + { + this.name = workflowNode.name; + this.title = workflowNode.title; + this.description = workflowNode.description; + this.isTaskNode = workflowNode.isTaskNode; + this.transitions = new ArrayList(); + WorkflowTransition[] cmrWorkflowTransitions = workflowNode.transitions; + for (WorkflowTransition cmrWorkflowTransition : cmrWorkflowTransitions) + { + transitions.add(new JscriptWorkflowTransition(cmrWorkflowTransition)); + } + this.scope = scope; + this.serviceRegistry = serviceRegistry; + } + + /** + * Gets the value of the name property + * + * @return the name + */ + public String getName() + { + return name; + } + + /** + * Gets the value of the title property + * + * @return the title + */ + public String getTitle() + { + return title; + } + + /** + * Gets the value of the description property + * + * @return the description + */ + public String getDescription() + { + return description; + } + + /** + * Gets the value of the isTaskNode property + * + * @return the isTaskNode + */ + public boolean isTaskNode() + { + return isTaskNode; + } + + /** + * Gets the value of the transitions property + * + * @return the transitions + */ + public Scriptable getTransitions() + { + Scriptable transitionsScriptable = (Scriptable)new ValueConverter().convertValueForScript( + this.serviceRegistry, this.scope, null, this.transitions); + + return transitionsScriptable; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() + { + String transitionsList = "{"; + for (JscriptWorkflowTransition transition : this.transitions) + { + transitionsList += ", '" + transition + "'"; + } + transitionsList += "}"; + return "WorkflowNode[title=" + title + ",transitions=" + transitionsList + "]"; + } +} diff --git a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowPath.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowPath.java similarity index 56% rename from source/java/org/alfresco/repo/workflow/jsapi/WorkflowPath.java rename to source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowPath.java index 1f3cd0277d..3b7730f0e9 100644 --- a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowPath.java +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowPath.java @@ -22,13 +22,19 @@ * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ -package org.alfresco.repo.workflow.jsapi; +package org.alfresco.repo.workflow.jscript; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.workflow.WorkflowNode; +import org.alfresco.service.cmr.workflow.WorkflowPath; import org.alfresco.service.cmr.workflow.WorkflowService; +import org.alfresco.service.cmr.workflow.WorkflowTask; +import org.mozilla.javascript.Scriptable; /** * Class that represents a path of execution through a workflow. @@ -40,8 +46,10 @@ import org.alfresco.service.cmr.workflow.WorkflowService; * @author glenj * */ -public class WorkflowPath +public class JscriptWorkflowPath implements Serializable { + static final long serialVersionUID = 8271566861210368614L; + /** Unique ID for workflow path */ private final String id; @@ -52,27 +60,32 @@ public class WorkflowPath private WorkflowNode node; /** Workflow instance path is part of */ - private WorkflowInstance instance; + private JscriptWorkflowInstance instance; - /** Workflow Service reference */ - private WorkflowService workflowService; + /** Service Registry object */ + private ServiceRegistry serviceRegistry; + /** Root scripting scope for this object */ + private final Scriptable scope; + /** * Creates a new instance of a workflow path * * @param id workflow path ID * @param node workflow node the path has reached * @param instance instance to which the workflow path belongs - * @param workflowService reference to the Workflow Service + * @param serviceRegistry Service Registry object + * @param scope the root scripting scope for this object */ - public WorkflowPath(final String id, final WorkflowNode node, final WorkflowInstance instance, - final WorkflowService workflowService) + JscriptWorkflowPath(final String id, final WorkflowNode node, final JscriptWorkflowInstance instance, + final ServiceRegistry serviceRegistry, final Scriptable scope) { this.id = id; this.node = node; this.instance = instance; this.active = false; - this.workflowService = workflowService; + this.serviceRegistry = serviceRegistry; + this.scope = scope; } /** @@ -81,16 +94,18 @@ public class WorkflowPath * * @param cmrWorkflowPath an instance of WorkflowPath from the CMR * workflow object model - * @param workflowService reference to the Workflow Service + * @param serviceRegistry Service Registry object + * @param scope the root scripting scope for this object */ - public WorkflowPath(final org.alfresco.service.cmr.workflow.WorkflowPath cmrWorkflowPath, - final WorkflowService workflowService) + JscriptWorkflowPath(final WorkflowPath cmrWorkflowPath, + final ServiceRegistry serviceRegistry, Scriptable scope) { this.id = cmrWorkflowPath.id; this.node = cmrWorkflowPath.node; - this.instance = new WorkflowInstance(cmrWorkflowPath.instance, workflowService); + this.instance = new JscriptWorkflowInstance(cmrWorkflowPath.instance, serviceRegistry, scope); this.active = cmrWorkflowPath.active; - this.workflowService = workflowService; + this.serviceRegistry = serviceRegistry; + this.scope = scope; } /** @@ -132,7 +147,7 @@ public class WorkflowPath * * @return the instance */ - public WorkflowInstance getInstance() + public JscriptWorkflowInstance getInstance() { return instance; } @@ -142,15 +157,32 @@ public class WorkflowPath * * @return all the tasks associated with this workflow path instance */ - public List getTasks() + public Scriptable getTasks() { - List cmrTasks = workflowService.getTasksForWorkflowPath(id); - List tasks = new ArrayList(); - for (org.alfresco.service.cmr.workflow.WorkflowTask cmrTask : cmrTasks) + WorkflowService workflowService = serviceRegistry.getWorkflowService(); + + List cmrTasks = workflowService.getTasksForWorkflowPath(id); + ArrayList tasks = new ArrayList(); + for (WorkflowTask cmrTask : cmrTasks) { - tasks.add(new WorkflowTask(cmrTask, workflowService)); + tasks.add(new JscriptWorkflowTask(cmrTask, this.serviceRegistry)); } - return tasks; + Scriptable tasksScriptable = + (Scriptable)new ValueConverter().convertValueForScript(this.serviceRegistry, scope, null, tasks); + + return tasksScriptable; + } + + /** + * Signal a transition to another node in the workflow + * + * @param transitionId ID of the transition to follow (or null, for the default transition) + * @return the updated workflow path + */ + public JscriptWorkflowPath signal(String transitionId) + { + WorkflowPath path = serviceRegistry.getWorkflowService().signal(this.id, transitionId); + return new JscriptWorkflowPath(path, this.serviceRegistry, this.scope); } } diff --git a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowTask.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTask.java similarity index 67% rename from source/java/org/alfresco/repo/workflow/jsapi/WorkflowTask.java rename to source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTask.java index 1a386520a6..23ba51d8f2 100644 --- a/source/java/org/alfresco/repo/workflow/jsapi/WorkflowTask.java +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTask.java @@ -22,13 +22,16 @@ * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ -package org.alfresco.repo.workflow.jsapi; +package org.alfresco.repo.workflow.jscript; import java.io.Serializable; -import java.util.Map; +import java.util.Set; -import org.alfresco.service.cmr.workflow.WorkflowService; +import org.alfresco.repo.jscript.ScriptableQNameMap; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.workflow.WorkflowTask; import org.alfresco.service.namespace.QName; +import org.mozilla.javascript.Scriptable; /** * This class represents a workflow task (an instance of a workflow @@ -37,8 +40,10 @@ import org.alfresco.service.namespace.QName; * @author glenj * */ -public class WorkflowTask +public class JscriptWorkflowTask implements Serializable { + static final long serialVersionUID = -8285971359421912313L; + /** Unique ID for workflow task */ private final String id; @@ -51,8 +56,8 @@ public class WorkflowTask /** Description of workflow task */ private final String description; - /** Properties (key/value pairs) */ - private Map properties; + /** Properties (key/value pairs) for this Workflow Task */ + private ScriptableQNameMap properties; /** Whether task is complete or not - 'true':complete, 'false':in-progress */ private boolean complete = false; @@ -60,8 +65,8 @@ public class WorkflowTask /** Whether task is pooled or not */ private boolean pooled = false; - /** Workflow Service reference */ - private WorkflowService workflowService; + /** Service Registry object */ + private ServiceRegistry serviceRegistry; /** * Creates a new instance of a workflow task (instance of a workflow task definition) @@ -70,16 +75,18 @@ public class WorkflowTask * @param name workflow task name * @param title workflow task title * @param description workflow task description - * @param workflowService reference to the Workflow Service + * @param serviceRegistry Service Registry object */ - public WorkflowTask(final String id, final String name, final String title, final String description, - final WorkflowService workflowService) + JscriptWorkflowTask(final String id, final String name, final String title, + final String description, final ServiceRegistry serviceRegistry, + final ScriptableQNameMap properties) { this.id = id; this.name = name; this.title = title; this.description = description; - this.workflowService = workflowService; + this.serviceRegistry = serviceRegistry; + this.properties = properties; } /** @@ -87,16 +94,28 @@ public class WorkflowTask * workflow object model * * @param cmrWorkflowTask an instance of WorkflowTask from CMR workflow object model - * @param workflowService reference to the Workflow Service + * @param serviceRegistry Service Registry object */ - public WorkflowTask(final org.alfresco.service.cmr.workflow.WorkflowTask cmrWorkflowTask, - WorkflowService workflowService) + JscriptWorkflowTask(final WorkflowTask cmrWorkflowTask, + final ServiceRegistry serviceRegistry) { this.id = cmrWorkflowTask.id; this.name = cmrWorkflowTask.name; this.title = cmrWorkflowTask.title; this.description = cmrWorkflowTask.description; - this.workflowService = workflowService; + this.serviceRegistry = serviceRegistry; + + // instantiate ScriptableQNameMap properties + // from WorkflowTasks's Map properties + this.properties = new ScriptableQNameMap( + serviceRegistry.getNamespaceService()); + + Set keys = cmrWorkflowTask.properties.keySet(); + for (QName key : keys) + { + Serializable value = cmrWorkflowTask.properties.get(key); + this.properties.put(key.toString(), value); + } } /** @@ -144,7 +163,7 @@ public class WorkflowTask * * @return the properties */ - public Map getProperties() + public Scriptable getProperties() { return properties; } @@ -154,7 +173,7 @@ public class WorkflowTask * * @param properties the properties to set */ - public void setProperties(Map properties) + public void setProperties(ScriptableQNameMap properties) { this.properties = properties; } @@ -170,17 +189,6 @@ public class WorkflowTask return complete; } - /** - * Sets whether the task is complete or in-progress - * 'true':complete, 'false':in-progress - * - * @param complete the complete to set - */ - public void setComplete(boolean complete) - { - this.complete = complete; - } - /** * Returns whether this task is pooled or not * @@ -208,6 +216,6 @@ public class WorkflowTask */ public void endTask(String transitionId) { - workflowService.endTask(this.id, transitionId); + serviceRegistry.getWorkflowService().endTask(this.id, transitionId); } } diff --git a/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTransition.java b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTransition.java new file mode 100644 index 0000000000..470e33c9ec --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jscript/JscriptWorkflowTransition.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.workflow.jscript; + +import java.io.Serializable; + +import org.alfresco.service.cmr.workflow.WorkflowTransition; + +public class JscriptWorkflowTransition implements Serializable +{ + static final long serialVersionUID = 8370298400161156357L; + + /** Workflow transition id */ + private String id; + + /** Localised workflow transition title */ + private String title; + + /** Localised workflow transition description */ + private String description; + + /** + * Constructor to create a new instance of this class + * from scratch + * + * @param id Workflow transition ID + * @param title Workflow transition title + * @param description Workflow transition description + */ + JscriptWorkflowTransition(String id, String title, String description) + { + this.id = id; + this.title = title; + this.description = description; + } + + /** + * Constructor to create a new instance of this class from an existing + * instance of WorkflowTransition from the CMR workflow object model + * + * @param transition CMR WorkflowTransition object from which + * to create a new instance of this class + */ + JscriptWorkflowTransition(WorkflowTransition transition) + { + this.id = transition.id; + this.title = transition.title; + this.description = transition.description; + } + + /** + * Gets the value of the id property + * + * @return the id + */ + public String getId() + { + return id; + } + + /** + * Gets the value of the title property + * + * @return the title + */ + public String getTitle() + { + return title; + } + + /** + * Gets the value of the description property + * + * @return the description + */ + public String getDescription() + { + return description; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() + { + return "JscriptWorkflowTransition[id=" + id + ",title=" + title + ",description=" + description + "]"; + } +} diff --git a/source/java/org/alfresco/repo/workflow/jscript/WorkflowManager.java b/source/java/org/alfresco/repo/workflow/jscript/WorkflowManager.java new file mode 100644 index 0000000000..613788fe33 --- /dev/null +++ b/source/java/org/alfresco/repo/workflow/jscript/WorkflowManager.java @@ -0,0 +1,228 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.repo.workflow.jscript; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.alfresco.repo.jscript.BaseScopableProcessorExtension; +import org.alfresco.repo.jscript.ScriptNode; +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.workflow.WorkflowDefinition; +import org.alfresco.service.cmr.workflow.WorkflowInstance; +import org.alfresco.service.cmr.workflow.WorkflowTask; +import org.alfresco.service.cmr.workflow.WorkflowTaskState; +import org.mozilla.javascript.Scriptable; + +/** + * The Workflow Manager serves as the main entry point for scripts + * to create and interact with workflows. + * It is made available in the root scripting scope + * + * @author glenj + * + */ +public class WorkflowManager extends BaseScopableProcessorExtension +{ + /** Registry Service property */ + private ServiceRegistry services; + + /** + * Sets the Service Registry property + * + * @param services the service registry + */ + public void setServiceRegistry(ServiceRegistry services) + { + this.services = services; + } + + /** + * Get deployed workflow definition by ID + * + * @param id the workflow definition ID + * @return the workflow definition matching the given ID + */ + public JscriptWorkflowDefinition getDefinition(String id) + { + WorkflowDefinition cmrWorkflowDefinition = + this.services.getWorkflowService().getDefinitionById(id); + return new JscriptWorkflowDefinition(cmrWorkflowDefinition, this.services, getScope()); + } + + /** + * Get tasks assigned to the current user. Note that this will only return in-progress + * tasks. + * + * @return the list of assigned (in-progress) tasks + */ + public Scriptable getAssignedTasks() + { + return getAssignedTasksByState(WorkflowTaskState.IN_PROGRESS); + } + + /** + * Get completed tasks assigned to the current user. + * + * @return the list of completed tasks + */ + public Scriptable getCompletedTasks() + { + return getAssignedTasksByState(WorkflowTaskState.COMPLETED); + } + + /** + * Get Workflow Instance by ID + * + * @param workflowInstanceID ID of the workflow instance to retrieve + * @return the workflow instance for the given ID + */ + public JscriptWorkflowInstance getInstance(String workflowInstanceID) + { + WorkflowInstance cmrWorkflowInstance = this.services.getWorkflowService().getWorkflowById( + workflowInstanceID); + return new JscriptWorkflowInstance(cmrWorkflowInstance, this.services, getScope()); + } + + /** + * Get pooled tasks + * + * @param authority the authority + * @return the list of assigned tasks + */ + public Scriptable getPooledTasks(final String authority) + { + List cmrPooledTasks = this.services.getWorkflowService().getPooledTasks( + authority); + ArrayList pooledTasks = new ArrayList(); + for (WorkflowTask cmrPooledTask : cmrPooledTasks) + { + pooledTasks.add(new JscriptWorkflowTask(cmrPooledTask, this.services)); + } + + Scriptable pooledTasksScriptable = (Scriptable)new ValueConverter().convertValueForScript( + this.services, getScope(), null, pooledTasks); + return pooledTasksScriptable; + } + + /** + * Get task by id + * + * @param id task id + * @return the task (null if not found) + */ + public JscriptWorkflowTask getTask(String id) + { + WorkflowTask cmrWorkflowTask = this.services.getWorkflowService().getTaskById(id); + return new JscriptWorkflowTask(cmrWorkflowTask, this.services); + } + + /** + * Get task by id. Alternative method signature to getTask(String id) for + * those used to the Template API + * + * @param id task id + * @return the task (null if not found) + */ + public JscriptWorkflowTask getTaskById(String id) + { + return getTask(id); + } + + /** + * Gets the latest versions of the deployed, workflow definitions + * + * @return the latest versions of the deployed workflow definitions + */ + public Scriptable getLatestDefinitions() + { + List cmrDefinitions = this.services.getWorkflowService().getDefinitions(); + ArrayList workflowDefs = new ArrayList(); + for (WorkflowDefinition cmrDefinition : cmrDefinitions) + { + workflowDefs.add(new JscriptWorkflowDefinition(cmrDefinition, this.services, getScope())); + } + + Scriptable workflowDefsScriptable = (Scriptable)new ValueConverter().convertValueForScript( + this.services, this.getScope(), null, workflowDefs); + return workflowDefsScriptable; + } + + /** + * Gets all versions of the deployed workflow definitions + * + * @return all versions of the deployed workflow definitions + */ + public Scriptable getAllDefinitions() + { + List cmrDefinitions = this.services.getWorkflowService().getAllDefinitions(); + ArrayList workflowDefs = new ArrayList(); + for (WorkflowDefinition cmrDefinition : cmrDefinitions) + { + workflowDefs.add(new JscriptWorkflowDefinition(cmrDefinition, this.services, getScope())); + } + + Scriptable workflowDefsScriptable = (Scriptable)new ValueConverter().convertValueForScript( + this.services, this.getScope(), null, workflowDefs); + return workflowDefsScriptable; + } + + /** + * Create a workflow package (a container of content to route through a workflow) + * + * @return the created workflow package + */ + public ScriptNode createPackage() + { + NodeRef node = this.services.getWorkflowService().createPackage(null); + return new ScriptNode(node, services); + } + + /** + * Get tasks assigned to the current user, filtered by workflow task state. + * Only tasks having the specified state will be returned. + * + * @param state workflow task state to filter assigned tasks by + * @return the list of assigned tasks, filtered by state + */ + private Scriptable getAssignedTasksByState(WorkflowTaskState state) + { + List cmrAssignedTasks = this.services.getWorkflowService().getAssignedTasks( + services.getAuthenticationService().getCurrentUserName(), state); + ArrayList assignedTasks = new ArrayList(); + for (WorkflowTask cmrTask : cmrAssignedTasks) + { + assignedTasks.add(new JscriptWorkflowTask(cmrTask, this.services)); + } + + Scriptable assignedTasksScriptable = + (Scriptable)new ValueConverter().convertValueForScript(this.services, getScope(), null, assignedTasks); + + return assignedTasksScriptable; + } +}