Merged V1.3 to V1.4 (Workflow changes required)

svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4152 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4153 .
   svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4163 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4164 .
   svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4178 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4179 .
   svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4328 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4329 .
   svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4331 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4332 .



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4529 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-12-06 16:03:37 +00:00
parent a67f2d0cc9
commit 93c54b5127
10 changed files with 518 additions and 23 deletions

View File

@@ -0,0 +1,192 @@
/*
* 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.workflow.jbpm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.alfresco.service.cmr.workflow.WorkflowException;
import org.dom4j.Element;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.Transition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.Token;
import org.jbpm.instantiation.FieldInstantiator;
import org.jbpm.jpdl.el.impl.JbpmExpressionEvaluator;
/**
* For each "item in collection", create a fork.
*/
public class ForEachFork implements ActionHandler
{
private static final long serialVersionUID = 4643103713602441652L;
private Element foreach;
private String var;
/**
* Create a new child token for each item in list.
*
* @param executionContext
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void execute(final ExecutionContext executionContext)
throws Exception
{
//
// process action handler arguments
//
if (foreach == null)
{
throw new WorkflowException("forEach has not been provided");
}
// build "for each" collection
List forEachColl = null;
String forEachCollStr = foreach.getTextTrim();
if (forEachCollStr != null)
{
if (forEachCollStr.startsWith("#{"))
{
Object eval = JbpmExpressionEvaluator.evaluate(forEachCollStr, executionContext);
if (eval == null)
{
throw new WorkflowException("forEach expression '" + forEachCollStr + "' evaluates to null");
}
// expression evaluates to string
if (eval instanceof String)
{
String[] forEachStrs = ((String)eval).trim().split(",");
forEachColl = new ArrayList(forEachStrs.length);
for (String forEachStr : forEachStrs)
{
forEachColl.add(forEachStr);
}
}
// expression evaluates to collection
else if (eval instanceof Collection)
{
forEachColl = (List)eval;
}
}
}
else
{
forEachColl = (List)FieldInstantiator.getValue(List.class, foreach);
}
if (var == null || var.length() == 0)
{
throw new WorkflowException("forEach variable name has not been provided");
}
//
// create forked paths
//
Token rootToken = executionContext.getToken();
Node node = executionContext.getNode();
List<ForkedTransition> forkTransitions = new ArrayList<ForkedTransition>();
// first, create a new token and execution context for each item in list
for (int i = 0; i < node.getLeavingTransitions().size(); i++)
{
Transition transition = (Transition) node.getLeavingTransitions().get(i);
for (int iVar = 0; iVar < forEachColl.size(); iVar++)
{
// create child token to represent new path
String tokenName = getTokenName(rootToken, transition.getName(), iVar);
Token loopToken = new Token(rootToken, tokenName);
loopToken.setTerminationImplicit(true);
executionContext.getJbpmContext().getSession().save(loopToken);
// assign variable within path
final ExecutionContext newExecutionContext = new ExecutionContext(loopToken);
newExecutionContext.getContextInstance().createVariable(var, forEachColl.get(iVar), loopToken);
// record path & transition
ForkedTransition forkTransition = new ForkedTransition();
forkTransition.executionContext = newExecutionContext;
forkTransition.transition = transition;
forkTransitions.add(forkTransition);
}
}
//
// let each new token leave the node.
//
for (ForkedTransition forkTransition : forkTransitions)
{
node.leave(forkTransition.executionContext, forkTransition.transition);
}
}
/**
* Create a token name
*
* @param parent
* @param transitionName
* @return
*/
protected String getTokenName(Token parent, String transitionName, int loopIndex)
{
String tokenName = null;
if (transitionName != null)
{
if (!parent.hasChild(transitionName))
{
tokenName = transitionName;
}
else
{
int i = 2;
tokenName = transitionName + Integer.toString(i);
while (parent.hasChild(tokenName))
{
i++;
tokenName = transitionName + Integer.toString(i);
}
}
}
else
{
// no transition name
int size = ( parent.getChildren()!=null ? parent.getChildren().size()+1 : 1 );
tokenName = Integer.toString(size);
}
return tokenName + "." + loopIndex;
}
/**
* Fork Transition
*/
private class ForkedTransition
{
private ExecutionContext executionContext;
private Transition transition;
}
}

View File

@@ -68,6 +68,7 @@ import org.hibernate.proxy.HibernateProxy;
import org.jbpm.JbpmContext;
import org.jbpm.JbpmException;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.context.exe.TokenVariableMap;
import org.jbpm.db.GraphSession;
import org.jbpm.db.TaskMgmtSession;
import org.jbpm.graph.def.Node;
@@ -909,6 +910,18 @@ public class JBPMEngine extends BPMEngine
TaskMgmtSession taskSession = context.getTaskMgmtSession();
TaskInstance taskInstance = getTaskInstance(taskSession, taskId);
// ensure all mandatory properties have been provided
QName[] missingProps = getMissingMandatoryTaskProperties(taskInstance);
if (missingProps != null && missingProps.length > 0)
{
String props = "";
for (int i = 0; i < missingProps.length; i++)
{
props += missingProps[i].toString() + ((i < missingProps.length -1) ? "," : "");
}
throw new WorkflowException("Mandatory task properties have not been provided: " + props);
}
// signal the transition on the task
if (transition == null)
{
@@ -1190,10 +1203,32 @@ public class JBPMEngine extends BPMEngine
Map<QName, PropertyDefinition> taskProperties = taskDef.getProperties();
Map<QName, AssociationDefinition> taskAssocs = taskDef.getAssociations();
// build properties from jBPM context (visit all tokens to the root)
Map<String, Object> vars = instance.getVariablesLocally();
if (!localProperties)
{
ContextInstance context = instance.getContextInstance();
Token token = instance.getToken();
while (token != null)
{
TokenVariableMap varMap = context.getTokenVariableMap(token);
if (varMap != null)
{
Map<String, Object> tokenVars = varMap.getVariablesLocally();
for (Map.Entry<String, Object> entry : tokenVars.entrySet())
{
if (!vars.containsKey(entry.getKey()))
{
vars.put(entry.getKey(), entry.getValue());
}
}
}
token = token.getParent();
}
}
// map arbitrary task variables
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(10);
Map<String, Object> vars = (localProperties ? instance.getVariablesLocally() : instance.getVariables());
for (Entry<String, Object> entry : vars.entrySet())
{
String key = entry.getKey();
@@ -1544,6 +1579,69 @@ public class JBPMEngine extends BPMEngine
}
}
/**
* Get missing mandatory properties on Task
*
* @param instance task instance
* @return array of missing property names (or null, if none)
*/
protected QName[] getMissingMandatoryTaskProperties(TaskInstance instance)
{
List<QName> missingProps = null;
// retrieve properties of task
Map<QName, Serializable> existingValues = getTaskProperties(instance, false);
// retrieve definition of task
ClassDefinition classDef = getAnonymousTaskDefinition(getTaskDefinition(instance.getTask()));
Map<QName, PropertyDefinition> propertyDefs = classDef.getProperties();
Map<QName, AssociationDefinition> assocDefs = classDef.getAssociations();
// for each property, determine if it is mandatory
for (Map.Entry<QName, PropertyDefinition> entry : propertyDefs.entrySet())
{
QName name = entry.getKey();
if (!(name.getNamespaceURI().equals(NamespaceService.CONTENT_MODEL_1_0_URI) || (name.getNamespaceURI().equals(NamespaceService.SYSTEM_MODEL_1_0_URI))))
{
boolean isMandatory = entry.getValue().isMandatory();
if (isMandatory)
{
Object value = existingValues.get(entry.getKey());
if (value == null || (value instanceof String && ((String)value).length() == 0))
{
if (missingProps == null)
{
missingProps = new ArrayList<QName>();
}
missingProps.add(entry.getKey());
}
}
}
}
for (Map.Entry<QName, AssociationDefinition> entry : assocDefs.entrySet())
{
QName name = entry.getKey();
if (!(name.getNamespaceURI().equals(NamespaceService.CONTENT_MODEL_1_0_URI) || (name.getNamespaceURI().equals(NamespaceService.SYSTEM_MODEL_1_0_URI))))
{
boolean isMandatory = entry.getValue().isTargetMandatory();
if (isMandatory)
{
Object value = existingValues.get(entry.getKey());
if (value == null || (value instanceof List && ((List)value).size() == 0))
{
if (missingProps == null)
{
missingProps = new ArrayList<QName>();
}
missingProps.add(entry.getKey());
}
}
}
}
return (missingProps == null) ? null : missingProps.toArray(new QName[missingProps.size()]);
}
/**
* Convert a Repository association to JBPMNodeList or JBPMNode
*

View File

@@ -32,6 +32,7 @@ import org.alfresco.repo.workflow.BPMEngineRegistry;
import org.alfresco.repo.workflow.TaskComponent;
import org.alfresco.repo.workflow.WorkflowComponent;
import org.alfresco.repo.workflow.WorkflowModel;
import org.alfresco.repo.workflow.WorkflowPackageComponent;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
@@ -60,6 +61,7 @@ public class JBPMEngineTest extends BaseSpringTest
NodeService nodeService;
WorkflowComponent workflowComponent;
TaskComponent taskComponent;
WorkflowPackageComponent packageComponent;
WorkflowDefinition testWorkflowDef;
NodeRef testNodeRef;
@@ -74,6 +76,7 @@ public class JBPMEngineTest extends BaseSpringTest
BPMEngineRegistry registry = (BPMEngineRegistry)applicationContext.getBean("bpm_engineRegistry");
workflowComponent = registry.getWorkflowComponent("jbpm");
taskComponent = registry.getTaskComponent("jbpm");
packageComponent = (WorkflowPackageComponent)applicationContext.getBean("workflowPackageImpl");
// deploy test process messages
I18NUtil.registerResourceBundle("org/alfresco/repo/workflow/jbpm/test-messages");
@@ -357,6 +360,7 @@ public class JBPMEngineTest extends BaseSpringTest
Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "reviewer"), "admin");
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "testNode"), testNodeRef);
parameters.put(QName.createQName(NamespaceService.BPM_MODEL_1_0_URI, "package"), packageComponent.createPackage(null));
WorkflowPath path = workflowComponent.startWorkflow(workflowDef.id, parameters);
assertNotNull(path);
List<WorkflowTask> tasks = workflowComponent.getTasksForWorkflowPath(path.id);
@@ -402,6 +406,7 @@ public class JBPMEngineTest extends BaseSpringTest
Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "reviewer"), "admin");
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "testNode"), testNodeRef);
parameters.put(QName.createQName(NamespaceService.BPM_MODEL_1_0_URI, "package"), packageComponent.createPackage(null));
WorkflowPath path = workflowComponent.startWorkflow(workflowDef.id, parameters);
assertNotNull(path);
List<WorkflowTask> tasks1 = workflowComponent.getTasksForWorkflowPath(path.id);
@@ -443,6 +448,7 @@ public class JBPMEngineTest extends BaseSpringTest
Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "reviewer"), "admin");
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "testNode"), testNodeRef);
parameters.put(QName.createQName(NamespaceService.BPM_MODEL_1_0_URI, "package"), packageComponent.createPackage(null));
WorkflowPath path = workflowComponent.startWorkflow(workflowDef.id, parameters);
assertNotNull(path);
List<WorkflowTask> tasks1 = workflowComponent.getTasksForWorkflowPath(path.id);
@@ -466,6 +472,7 @@ public class JBPMEngineTest extends BaseSpringTest
WorkflowDefinition workflowDef = deployment.definition;
Map<QName, Serializable> parameters = new HashMap<QName, Serializable>();
parameters.put(QName.createQName(NamespaceService.DEFAULT_URI, "testNode"), testNodeRef);
parameters.put(QName.createQName(NamespaceService.BPM_MODEL_1_0_URI, "package"), packageComponent.createPackage(null));
WorkflowPath path = workflowComponent.startWorkflow(workflowDef.id, parameters);
assertNotNull(path);
List<WorkflowTask> tasks1 = workflowComponent.getTasksForWorkflowPath(path.id);