Cross posting for future findability and reference:
The following is a example helper methods using Groovy and the Camunda Model Fluent API, which allow you to add Camunda Extension Properties to elementIds.
It takes a Map of Properties and a Array of Element IDs and will cycle through them to modify the Model.
import org.camunda.bpm.model.bpmn.Bpmn
import org.camunda.bpm.model.bpmn.BpmnModelInstance
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaProperties
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaProperty
import org.camunda.bpm.model.xml.instance.ModelElementInstance
private CamundaProperties createCamundaProperties(BpmnModelInstance bpmnModelInstance, Map<String,String> properties) {
CamundaProperties camundaProperties = bpmnModelInstance.newInstance(CamundaProperties.class)
properties.each {
camundaProperties.addChildElement(addCamundaProperty(camundaProperties, it.getKey(), it.getValue()))
}
return camundaProperties
}
private CamundaProperty addCamundaProperty(CamundaProperties properties, String key, String value){
CamundaProperty property = properties.getModelInstance().newInstance(CamundaProperty.class)
property.setCamundaName(key)
property.setCamundaValue(value)
return property
}
BpmnModelInstance addCamundaProperties(BpmnModelInstance bpmnModelInstance, List<String> elementIds, Map<String, String> properties ){
elementIds.each { elementId ->
ModelElementInstance instance = bpmnModelInstance.getModelElementById(elementId)
try {
CamundaProperties camundaProperties = createCamundaProperties(bpmnModelInstance,properties)
instance.builder().addExtensionElement(camundaProperties)
} catch (all){
throw new Exception("Cant add extension to Element: ${elementId}. Error: ${all}")
}
}
return bpmnModelInstance
}
BpmnModelInstance model3() {
BpmnModelInstance model = Bpmn.createExecutableProcess('model')
.name("Reminder Demo")
.startEvent('someStartEvent')
.userTask('readEmail')
.boundaryEvent('killusertask')
.timerWithDuration("PT1H")
.cancelActivity(true)
.moveToActivity('readEmail')
.boundaryEvent()
.timerWithCycle("R3/PT10M")
.cancelActivity(false)
.serviceTask()
.name('reminderSent')
.implementation('expression')
.camundaExpression('${1+1}')
.endEvent()
.moveToActivity('readEmail')
.manualTask('manual1').name('do something')
.moveToNode('killusertask').connectTo('manual1')
//.moveToActivity('killusertask').connectTo('manual1') This does not work. Must use the moveToNode()
.manualTask('manual2').name('do something else')
.endEvent()
.done()
model = addCamundaProperties(model,
['model','someStartEvent','killusertask', 'readEmail'],
['prop1':'value1', 'prop2':'value2'])
return model
}