Adding Extensions Properties to Model with Fluent API (Helper Example)

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
}

@StephenOTT Is there any such example in Java? Aslo at instance.builder().addExtensionElement giving compilation error.

@PrashantD the example is Groovy. So its close or 100% java compatible. You just need to add the semi colons and replace the .each to foreach structures.

Thanks for prompt response.

I did the same but int addCamundaProperties method instance.builder() giving compliation error.

@PrashantD you need to provide more information than that. Need to see error logs or something that can be debugged.

resolved that by adding cast as ((BpmnModelElementInstance) instance).builder().addExtensionElement(camundaProperties);

Thanks.

Okay. Glad you figured it out