How can i override BpmnFactory's _ensureId method?

In this post is mentioned, that we can override the BpmnFactory._ensureId.

How exactly do i override it?

The reason I ask for this is that we would like to implement GUIDs for all elements that need an id, instead of the normal id.

Edit: i copied the wrong reply-url

You can follow eduk8u these steps one by one to override:
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.Process;
import org.camunda.bpm.model.xml.ModelBuilder;
import org.camunda.bpm.model.xml.impl.ModelInstanceImpl;
import org.camunda.bpm.model.xml.impl.util.ModelUtil;
import org.camunda.bpm.model.xml.instance.ModelElementInstance;
import org.camunda.bpm.model.xml.type.ModelElementType;
import org.camunda.bpm.model.xml.type.ModelElementTypeBuilder;
import org.camunda.bpm.model.xml.type.child.SequenceBuilder;

public class CustomBpmnFactory extends BpmnFactory {

@Override
protected String _ensureId(ModelElementInstance element) {
    // Custom logic to generate or manipulate the ID
    String originalId = super._ensureId(element);
    String modifiedId = "custom_" + originalId;
    return modifiedId;
}

public static void main(String[] args) {
    // Example usage
    ModelBuilder modelBuilder = ModelUtil.createModelBuilder();
    ModelElementTypeBuilder typeBuilder = modelBuilder.defineType(Process.class, "bpmn:Process")
            .namespaceUri(BpmnModelInstance.CAMUNDA_NS)
            .extendsType(org.camunda.bpm.model.bpmn.impl.instance.ProcessImpl.class)
            .instanceProvider(new BpmnModelInstanceImpl.InstanceProvider<Process>() {
                public Process newInstance(ModelTypeInstanceContext instanceContext) {
                    return new ProcessImpl(instanceContext);
                }
            });

    SequenceBuilder sequenceBuilder = typeBuilder.sequence();

    // ... Define other BPMN elements as needed

    BpmnModelInstance modelInstance = new BpmnModelInstanceImpl(modelBuilder, null);
    Process process = modelInstance.newInstance(Process.class);
    process.setId("myProcess");
    modelInstance.setDefinitions(process);
    
    CustomBpmnFactory customFactory = new CustomBpmnFactory();
    customFactory.setBpmnModelInstance(modelInstance);

    // ... Use the CustomBpmnFactory for further operations
}

}

1 Like

Not what i have asked for - link also spam?

@s.chung Extending our tools, in particular any component such as bpmnFactory you’d proceed as following:

  • Create your custom extension class MyCustomBpmnFactory extends BpmnFactory. You have to adhere to the existing service contract. I.e. it is fundamental that IDs are actually unique etc.
  • Provide MyCustomBpmnFactory as a service to bpmn-js by exposing it as an extension module :arrow_down:
import BpmnFactory from './MyCustomBpmnFactory';
import BpmnModeler from 'bpmn-js/lib/Modeler';

// re-declare `bpmnFactory` to be provided through
// your custom service
const extensionModule = {
  'bpmnFactory': [ 'type', MyCustomBpmnFactory ]
};

// bootstrap your BPMN editor using the custom extension module:
const bpmnModeler = new BpmnModeler({
  additionalModules: [ 
    extensionModule
  ]
});

const bpmnFactory = bpmnModeler.get('bpmnFactory'); 
// instance of MyCustomBpmnFactory

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.