Home

Environment
Problem
When creating a workflow a package may need to be created with multiple documents, in javascript there’s a limitation when creating packages with multiple documents

//Get the activiti Review workflow defintion
var wfdef = workflow.getDefinitionByName("activiti$activitiReview");
//Create the workflow package object
var wfpackage = workflow.createPackage();
//Create the parameters object to pass to the workflow
var wfparams = new Array();
wfparams["bpm:workflowDescription"]="Test WF Package";
wfparams["bpm:assignee"]=people.getPerson("admin"); 

//Search the documents to be added to the package, for simplicity we are adding all documents named mytest.txt
var nodes = search.luceneSearch("workspace://SpacesStore",query,"@cm:name","mytest.txt");

for (var i=0; i<nodes.length; i++)
{
  var child = nodes[i];
  //Add the document to the package object
  wfpackage.addNode(child);
}

//Start the workflow with the constructed package
wfdef.startWorkflow(wfpackage,wfparams);

If there are multiple documents in the package with the same name an exception is thrown

Duplicate child name not allowed
Cause
The reason why the error is thrown is because when executing wfpackage.addNode() it creates a child association of type cm:contains, when dealing with workflow packages we need to create child associations of type bpm:packageContains.
Solution
To create a workflow package in java with multiple documents with the same name we need to add the documents with the child association of type bpm:packageContains as demonstrated in the snippet below:

//Get the workflow definition
WorkflowDefinition wfDefinition = workflowService.getDefinitionByName("activiti$activitiReview");

//Create a package object
NodeRef wfPackage = workflowService.createPackage(null);

//Create the workflow properties
Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
properties.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "WF TEST");
properties.put(WorkflowModel.ASSOC_PACKAGE, wfPackage);     
    
//Get a list of documents to add to the workflow package    
List<NodeRef> itemsToAdd = (ArrayList<NodeRef>) getListOfNodeRefsToAdd();

Iterator<NodeRef> itemsToAddIterator = itemsToAdd.iterator(); 
while(itemsToAddIterator.hasNext())
{
  NodeRef itemNodeRef = itemsToAddIterator.next();
  
//Add document
this.nodeService.addChild(wfPackage,itemNodeRef,WorkflowModel.ASSOC_PACKAGE_CONTAINS,QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,QName .createValidLocalName((String) this.nodeService.getProperty(itemNodeRef, ContentModel.PROP_NAME))));           
}
        
// start the workflow

WorkflowPath wfPath = workflowService.startWorkflow(wfDefinition.getId(), properties);
Advertisements

Laisser un commentaire

Choisissez une méthode de connexion pour poster votre commentaire:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s