How to add modeler workspace extensions properties through java api

Hello,
I’m trying to create a model through java api where i’m able to create the basic model as bellow:

val modelInstance: BpmnModelInstance = Bpmn.createProcess(fileName.toLowerCase)
.name(fileName)
.executable()
.startEvent.id(“start”).name(“Start Event”)
.userTask.id(“ut”).name(“User Task”)
.endEvent.id(“end”).name(“End event”)
.done()

Here,i wish to add a property in extensions of modeler workspace.Can any one help me with this!!!

@Sagari_E here is some code i have used for this type of thing.

I just tested it out and it works for setting the mode. I did not know this, but apparently the Definition ID of the BPMN model is considered a ModelElement, so can reuse the same code to add Extension Elements to Activities!


    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
    }

So at the bottom you see a fancy BPMN Fluent API model.

Once the core model is built, you need to add your extension elements to your activities (or in your case the Model).

So you can call addCamundaProperties(model, elements[], properties[:])

Note that you need to add id values to each of the elements you want to attach to.

In your specific case you wanted to add it to the Definition, so you use the Definition Id as your elementId

@thorben @camunda as a note for Fluent API, The issue with the fluent api and adding extensionElements, is to create a extension element using the core classes, you need the model instance that is part of the builder. Would be nice if there was a helper method accessible inside of the builder that let you pass the current mode into another method to generate the extension element.

1 Like