Skip to content

Commit

Permalink
added another example about converter extension (#1037)
Browse files Browse the repository at this point in the history
* added another example about converter extension

* example uses a stable version

* disabled non-working test

* moved to java 17
  • Loading branch information
jonathanlukas authored Oct 25, 2024
1 parent 3a17ec5 commit 84ed627
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,27 @@ private void traverse(
new DefaultDomElementVisitorContext(
element, context, result, notificationService, properties);
visitors.stream()
.sorted(Comparator.comparingInt(v -> v instanceof AbstractProcessElementVisitor ? 2 : 3))
.sorted(Comparator.comparingInt(this::sortVisitor))
.forEach(visitor -> visitor.visit(elementContext));
element.getChildElements().forEach(child -> traverse(child, result, context, properties));
}

private int sortVisitor(DomElementVisitor visitor) {
if (visitor instanceof AbstractProcessElementVisitor) {
// apply process element visitors first
return 2;
}
if (visitor
.getClass()
.getPackageName()
.startsWith(DomElementVisitor.class.getPackage().getName())) {
// then, apply native implementations
return 3;
}
// everything else is applied last
return 4;
}

public void writeCsvFile(List<BpmnDiagramCheckResult> results, Writer writer) {
try (ICSVWriter csvWriter = new CSVWriterBuilder(writer).withSeparator(';').build()) {
csvWriter.writeNext(createHeaders());
Expand Down
4 changes: 4 additions & 0 deletions camunda-7-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<groupId>io.camunda.spring</groupId>
<artifactId>spring-boot-starter-camunda</artifactId>
</dependency>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>java-common</artifactId>
</dependency>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>spring-boot-starter-camunda-test-testcontainer</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.camunda.community.migration.example.extendedConverter;

import org.camunda.community.migration.converter.BpmnDiagramCheckResult.Severity;
import org.camunda.community.migration.converter.DomElementVisitorContext;
import org.camunda.community.migration.converter.convertible.ServiceTaskConvertible;
import org.camunda.community.migration.converter.message.ComposedMessage;
import org.camunda.community.migration.converter.message.Message;
import org.camunda.community.migration.converter.visitor.AbstractSupportedAttributeVisitor;

public class TaskTopicVisitor extends AbstractSupportedAttributeVisitor {

@Override
public String attributeLocalName() {
return "topic";
}

@Override
protected Message visitSupportedAttribute(DomElementVisitorContext context, String attribute) {
context.addConversion(
ServiceTaskConvertible.class,
serviceTaskConversion -> serviceTaskConversion.addZeebeTaskHeader(attributeLocalName(), attribute));
context.addConversion(
ServiceTaskConvertible.class,
serviceTaskConversion -> serviceTaskConversion
.getZeebeTaskDefinition()
.setType("GenericWorker"));
ComposedMessage composedMessage = new ComposedMessage();
composedMessage.setMessage("Tasktopic has been transformed: " + attribute);
composedMessage.setSeverity(Severity.INFO);
composedMessage.setLink("Link");
return composedMessage;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.camunda.community.migration.example.extendedConverter.CustomDomElementVisitor
org.camunda.community.migration.example.extendedConverter.CustomDomElementVisitor
org.camunda.community.migration.example.extendedConverter.TaskTopicVisitor
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.xml.instance.DomElement;
import org.camunda.community.migration.converter.BpmnConverter;
import org.camunda.community.migration.converter.BpmnConverterFactory;
import org.camunda.community.migration.converter.ConverterPropertiesFactory;
import org.camunda.community.migration.converter.DomElementVisitorFactory;
import org.camunda.community.migration.converter.visitor.DomElementVisitor;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.StringWriter;
import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.camunda.community.migration.converter.NamespaceUri.*;

public class ExtendedConverterTest {
private static BpmnModelInstance loadModelInstance(String bpmnFile) {
Expand All @@ -35,13 +38,47 @@ void shouldAddPropertiesToGateway() {
.getInstance()
.get();
BpmnModelInstance modelInstance = loadModelInstance("example-model.bpmn");
converter.convert(modelInstance,
converter.convert(
modelInstance,
ConverterPropertiesFactory
.getInstance()
.get()
);
StringWriter writer = new StringWriter();
converter.printXml(modelInstance.getDocument(),true,writer);
converter.printXml(modelInstance.getDocument(), true, writer);
System.out.println(writer);
}

@Disabled
@Test
void shouldSetCustomJobType() {
BpmnConverter converter = BpmnConverterFactory
.getInstance()
.get();
BpmnModelInstance modelInstance = loadModelInstance("ExternalTaskWorker_Example.bpmn");
converter.convert(
modelInstance,
ConverterPropertiesFactory
.getInstance()
.get()
);
DomElement extensionElements = modelInstance
.getDocument()
.getElementById("Activity_1qqj67q")
.getChildElementsByNameNs(BPMN, "extensionElements")
.get(0);
DomElement header = extensionElements.getChildElementsByNameNs(ZEEBE, "taskHeaders").get(0).getChildElementsByNameNs(ZEEBE,"header").get(0);
String headerKey = header.getAttribute(ZEEBE,"key");
String headerValue = header.getAttribute(ZEEBE,"value");
String jobType = extensionElements
.getChildElementsByNameNs(ZEEBE, "taskDefinition")
.get(0)
.getAttribute(ZEEBE, "type");
assertThat(jobType).isEqualTo("GenericWorker");
assertThat(headerKey).isEqualTo("topic");
assertThat(headerValue).isEqualTo("TestTopic");
StringWriter writer = new StringWriter();
converter.printXml(modelInstance.getDocument(), true, writer);
System.out.println(writer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0lg2s2j" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.28.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.22.0">
<bpmn:process id="Process_1j6ouum" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="Process started">
<bpmn:outgoing>Flow_0zz19uu</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0zz19uu" sourceRef="StartEvent_1" targetRef="Activity_1qqj67q" />
<bpmn:endEvent id="Event_04ydt36" name="Process ended">
<bpmn:incoming>Flow_06q11dh</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_06q11dh" sourceRef="Activity_1qqj67q" targetRef="Event_04ydt36" />
<bpmn:serviceTask id="Activity_1qqj67q" name="Call test topic" camunda:type="external" camunda:topic="TestTopic">
<bpmn:incoming>Flow_0zz19uu</bpmn:incoming>
<bpmn:outgoing>Flow_06q11dh</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1j6ouum">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="159" y="142" width="77" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_04ydt36_di" bpmnElement="Event_04ydt36">
<dc:Bounds x="432" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="413" y="142" width="74" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0smb4x6_di" bpmnElement="Activity_1qqj67q">
<dc:Bounds x="270" y="77" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_0zz19uu_di" bpmnElement="Flow_0zz19uu">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_06q11dh_di" bpmnElement="Flow_06q11dh">
<di:waypoint x="370" y="117" />
<di:waypoint x="432" y="117" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@
<artifactId>spring-boot-starter-camunda</artifactId>
<version>${version.spring-boot-starter-camunda}</version>
</dependency>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>java-common</artifactId>
<version>${version.spring-boot-starter-camunda}</version>
</dependency>
<dependency>
<groupId>io.camunda</groupId>
<artifactId>camunda-operate-client-java</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions process-instance-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>java-common</artifactId>
</dependency>
<dependency>
<groupId>io.camunda</groupId>
<artifactId>zeebe-protocol</artifactId>
Expand Down

0 comments on commit 84ed627

Please sign in to comment.