Workflow JavaScript API

-----------------------

Overview
--------
The WorkflowManager bean is available in the root scripting scope.
It provides a simplified object oriented API giving JavaScript scripts access to Workflow Service operations. Through the WorkflowManager object, scripts can do things like
- get latest versions of all deployed workflow definitions
- get assigned tasks, pooled tasks etc.
- get workflow paths
- get workflow instances
- start a new workflow instance
- delete a workflow instance, cancel a workflow instance
- get all tasks belonging to a workflow path
- end a task
- etc. etc.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6479 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Glen Johnson
2007-08-10 07:34:12 +00:00
parent 5279ab5a22
commit 4c2aba1bd9
6 changed files with 889 additions and 0 deletions

View File

@@ -140,4 +140,12 @@
</property>
</bean>
<bean id="workflowScript" parent="baseJavaScriptExtension" class="org.alfresco.repo.workflow.jsapi.WorkflowManager">
<property name="extensionName">
<value>workflow</value>
</property>
<property name="workflowService">
<ref bean="WorkflowService"/>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,173 @@
/*
* 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.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.workflow.WorkflowService;
import org.alfresco.service.namespace.QName;
public class WorkflowDefinition
{
/** Workflow Service reference */
private WorkflowService workflowService;
/** Workflow definition id */
private final String id;
/** Workflow definition name */
private final String name;
/** Workflow definition version */
private final String version;
/** Workflow definition title */
private final String title;
/** Workflow definition description */
private final String description;
/**
* Create a new instance of <code>WorkflowDefinition</code> 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
*/
public WorkflowDefinition(final org.alfresco.service.cmr.workflow.WorkflowDefinition cmrWorkflowDefinition,
final WorkflowService workflowService)
{
this.id = cmrWorkflowDefinition.id;
this.name = cmrWorkflowDefinition.name;
this.version = cmrWorkflowDefinition.version;
this.title = cmrWorkflowDefinition.title;
this.description = cmrWorkflowDefinition.description;
this.workflowService = workflowService;
}
/**
* Creates a new instance of WorkflowDefinition
*
* @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
*/
public WorkflowDefinition(final String id, final String name, final String version,
final String title, final String description, WorkflowService workflowService)
{
this.id = id;
this.name = name;
this.version = version;
this.title = title;
this.description = description;
}
/**
* Get value of 'id' property
*
* @return the id
*/
public String getId()
{
return id;
}
/**
* Get value of 'name' property
*
* @return the name
*/
public String getName()
{
return name;
}
/**
* Get value of 'version' property
*
* @return the version
*/
public String getVersion()
{
return version;
}
/**
* Get value of 'title' property
*
* @return the title
*/
public String getTitle()
{
return title;
}
/**
* Get value of 'description' property
*
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* Start workflow instance from workflow definition
*
* @param properties properties (map of key-value pairs used to populate the
* start task properties
* @return the initial workflow path
*/
public WorkflowPath startWorkflow(Map<QName, Serializable> properties)
{
org.alfresco.service.cmr.workflow.WorkflowPath cmrWorkflowPath =
workflowService.startWorkflow(id, properties);
return new WorkflowPath(cmrWorkflowPath, workflowService);
}
/**
* Get active workflow instances of this workflow definition
*
* @return the active workflow instances spawned from this workflow definition
*/
public synchronized List<WorkflowInstance> getActiveInstances()
{
List<org.alfresco.service.cmr.workflow.WorkflowInstance> cmrWorkflowInstances = workflowService.getActiveWorkflows(this.id);
List<WorkflowInstance> activeInstances = new ArrayList<WorkflowInstance>();
for (org.alfresco.service.cmr.workflow.WorkflowInstance cmrWorkflowInstance : cmrWorkflowInstances)
{
activeInstances.add(new WorkflowInstance(cmrWorkflowInstance, workflowService));
}
return activeInstances;
}
}

View File

@@ -0,0 +1,174 @@
/*
* 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 <code>WorkflowInstance</code>
*
* @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 <code>WorkflowInstance</code> 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<WorkflowPath> getPaths(final String instanceId)
{
List<org.alfresco.service.cmr.workflow.WorkflowPath> cmrPaths = workflowService.getWorkflowPaths(instanceId);
List<WorkflowPath> paths = new ArrayList<WorkflowPath>();
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);
}
}

View File

@@ -0,0 +1,165 @@
/*
* 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<WorkflowTask> getAssignedTasks(final String authority, final WorkflowTaskState state)
{
List<org.alfresco.service.cmr.workflow.WorkflowTask> cmrAssignedTasks = workflowService.getAssignedTasks(authority, state);
List<WorkflowTask> assignedTasks = new ArrayList<WorkflowTask>();
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<WorkflowTask> getPooledTasks(final String authority, final WorkflowTaskState state)
{
List<org.alfresco.service.cmr.workflow.WorkflowTask> cmrPooledTasks = workflowService.getPooledTasks(authority);
List<WorkflowTask> pooledTasks = new ArrayList<WorkflowTask>();
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<WorkflowDefinition> getLatestDefinitions()
{
List<org.alfresco.service.cmr.workflow.WorkflowDefinition> cmrDefinitions = workflowService.getDefinitions();
List<WorkflowDefinition> workflowDefs = new ArrayList<WorkflowDefinition>();
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<WorkflowDefinition> getAllDefinitions()
{
List<org.alfresco.service.cmr.workflow.WorkflowDefinition> cmrDefinitions = workflowService.getAllDefinitions();
List<WorkflowDefinition> workflowDefs = new ArrayList<WorkflowDefinition>();
for (org.alfresco.service.cmr.workflow.WorkflowDefinition cmrDefinition : cmrDefinitions)
{
workflowDefs.add(new WorkflowDefinition(cmrDefinition, workflowService));
}
return workflowDefs;
}
}

View File

@@ -0,0 +1,156 @@
/*
* 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.service.cmr.workflow.WorkflowNode;
import org.alfresco.service.cmr.workflow.WorkflowService;
/**
* Class that represents a path of execution through a workflow.
*
* A simple workflow consists of only one root path of execution.
* That path may branch at some subsequent transition, so that execution
* follows multiple paths through the workflow.
*
* @author glenj
*
*/
public class WorkflowPath
{
/** Unique ID for workflow path */
private final String id;
/** State of workflow path 'true':active 'false':complete */
private boolean active;
/** Workflow node that the path has reached */
private WorkflowNode node;
/** Workflow instance path is part of */
private WorkflowInstance instance;
/** Workflow Service reference */
private WorkflowService workflowService;
/**
* 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
*/
public WorkflowPath(final String id, final WorkflowNode node, final WorkflowInstance instance,
final WorkflowService workflowService)
{
this.id = id;
this.node = node;
this.instance = instance;
this.active = false;
this.workflowService = workflowService;
}
/**
* Creates a new instance of WorkflowPath from an instance of the WorkflowPath
* class provided by the CMR workflow model
*
* @param cmrWorkflowPath an instance of WorkflowPath from the CMR
* workflow object model
* @param workflowService reference to the Workflow Service
*/
public WorkflowPath(final org.alfresco.service.cmr.workflow.WorkflowPath cmrWorkflowPath,
final WorkflowService workflowService)
{
this.id = cmrWorkflowPath.id;
this.node = cmrWorkflowPath.node;
this.instance = new WorkflowInstance(cmrWorkflowPath.instance, workflowService);
this.active = cmrWorkflowPath.active;
this.workflowService = workflowService;
}
/**
* Creates a new instance of a workflow path from
*/
/**
* Gets the value of the <code>id</code> property
*
* @return the id
*/
public String getId()
{
return id;
}
/**
* Gets the value of the <code>active</code> property
*
* @return the active
*/
public boolean isActive()
{
return active;
}
/**
* Gets the value of the <code>node</code> property
*
* @return the node
*/
public WorkflowNode getNode()
{
return node;
}
/**
* Gets the value of the <code>instance</code> property
*
* @return the instance
*/
public WorkflowInstance getInstance()
{
return instance;
}
/**
* Get all tasks associated with this workflow path
*
* @return all the tasks associated with this workflow path instance
*/
public List<WorkflowTask> getTasks()
{
List<org.alfresco.service.cmr.workflow.WorkflowTask> cmrTasks = workflowService.getTasksForWorkflowPath(id);
List<WorkflowTask> tasks = new ArrayList<WorkflowTask>();
for (org.alfresco.service.cmr.workflow.WorkflowTask cmrTask : cmrTasks)
{
tasks.add(new WorkflowTask(cmrTask, workflowService));
}
return tasks;
}
}

View File

@@ -0,0 +1,213 @@
/*
* 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.io.Serializable;
import java.util.Map;
import org.alfresco.service.cmr.workflow.WorkflowService;
import org.alfresco.service.namespace.QName;
/**
* This class represents a workflow task (an instance of a workflow
* task definition)
*
* @author glenj
*
*/
public class WorkflowTask
{
/** Unique ID for workflow task */
private final String id;
/** Name for workflow task */
private final String name;
/** Title for workflow task */
private final String title;
/** Description of workflow task */
private final String description;
/** Properties (key/value pairs) */
private Map<QName, Serializable> properties;
/** Whether task is complete or not - 'true':complete, 'false':in-progress */
private boolean complete = false;
/** Whether task is pooled or not */
private boolean pooled = false;
/** Workflow Service reference */
private WorkflowService workflowService;
/**
* Creates a new instance of a workflow task (instance of a workflow task definition)
*
* @param id workflow task ID
* @param name workflow task name
* @param title workflow task title
* @param description workflow task description
* @param workflowService reference to the Workflow Service
*/
public WorkflowTask(final String id, final String name, final String title, final String description,
final WorkflowService workflowService)
{
this.id = id;
this.name = name;
this.title = title;
this.description = description;
this.workflowService = workflowService;
}
/**
* Creates a new instance of a workflow task from a WorkflowTask from the CMR
* workflow object model
*
* @param cmrWorkflowTask an instance of WorkflowTask from CMR workflow object model
* @param workflowService reference to the Workflow Service
*/
public WorkflowTask(final org.alfresco.service.cmr.workflow.WorkflowTask cmrWorkflowTask,
WorkflowService workflowService)
{
this.id = cmrWorkflowTask.id;
this.name = cmrWorkflowTask.name;
this.title = cmrWorkflowTask.title;
this.description = cmrWorkflowTask.description;
this.workflowService = workflowService;
}
/**
* Gets the value of the <code>id</code> property
*
* @return the id
*/
public String getId()
{
return id;
}
/**
* Gets the value of the <code>name</code> property
*
* @return the name
*/
public String getName()
{
return name;
}
/**
* Gets the value of the <code>title</code> property
*
* @return the title
*/
public String getTitle()
{
return title;
}
/**
* Gets the value of the <code>description</code> property
*
* @return the description
*/
public String getDescription()
{
return description;
}
/**
* Gets the value of the <code>properties</code> property
*
* @return the properties
*/
public Map<QName, Serializable> getProperties()
{
return properties;
}
/**
* Sets the value of the <code>properties</code> property
*
* @param properties the properties to set
*/
public void setProperties(Map<QName, Serializable> properties)
{
this.properties = properties;
}
/**
* Returns whether the task is complete
* 'true':complete, 'false':in-progress
*
* @return the complete
*/
public boolean isComplete()
{
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
*
* @return 'true': task is pooled, 'false': task is not pooled
*/
public boolean isPooled()
{
return pooled;
}
/**
* Sets whether task is pooled('true') or not('false')
*
* @param pooled the pooled to set
*/
public void setPooled(boolean pooled)
{
this.pooled = pooled;
}
/**
* End the task
*
* @param transition transition to end the task for
*/
public void endTask(String transitionId)
{
workflowService.endTask(this.id, transitionId);
}
}