From 84ed627e3e9044fee665eddefe9b7357518be0c7 Mon Sep 17 00:00:00 2001 From: Jonathan Lukas Date: Fri, 25 Oct 2024 13:59:06 +0200 Subject: [PATCH] added another example about converter extension (#1037) * added another example about converter extension * example uses a stable version * disabled non-working test * moved to java 17 --- .../migration/converter/BpmnConverter.java | 18 +++++++- camunda-7-adapter/pom.xml | 4 ++ .../extendedConverter/TaskTopicVisitor.java | 33 ++++++++++++++ ...ration.converter.visitor.DomElementVisitor | 3 +- .../ExtendedConverterTest.java | 41 ++++++++++++++++- .../resources/ExternalTaskWorker_Example.bpmn | 45 +++++++++++++++++++ pom.xml | 5 +++ process-instance-migration/pom.xml | 4 ++ 8 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 example/extended-converter/src/main/java/org/camunda/community/migration/example/extendedConverter/TaskTopicVisitor.java create mode 100644 example/extended-converter/src/test/resources/ExternalTaskWorker_Example.bpmn diff --git a/backend-diagram-converter/core/src/main/java/org/camunda/community/migration/converter/BpmnConverter.java b/backend-diagram-converter/core/src/main/java/org/camunda/community/migration/converter/BpmnConverter.java index 727ced77d..2b521380d 100644 --- a/backend-diagram-converter/core/src/main/java/org/camunda/community/migration/converter/BpmnConverter.java +++ b/backend-diagram-converter/core/src/main/java/org/camunda/community/migration/converter/BpmnConverter.java @@ -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 results, Writer writer) { try (ICSVWriter csvWriter = new CSVWriterBuilder(writer).withSeparator(';').build()) { csvWriter.writeNext(createHeaders()); diff --git a/camunda-7-adapter/pom.xml b/camunda-7-adapter/pom.xml index ec7ab5513..3f97f83b2 100644 --- a/camunda-7-adapter/pom.xml +++ b/camunda-7-adapter/pom.xml @@ -19,6 +19,10 @@ io.camunda.spring spring-boot-starter-camunda + + io.camunda.spring + java-common + io.camunda.spring spring-boot-starter-camunda-test-testcontainer diff --git a/example/extended-converter/src/main/java/org/camunda/community/migration/example/extendedConverter/TaskTopicVisitor.java b/example/extended-converter/src/main/java/org/camunda/community/migration/example/extendedConverter/TaskTopicVisitor.java new file mode 100644 index 000000000..2e09532eb --- /dev/null +++ b/example/extended-converter/src/main/java/org/camunda/community/migration/example/extendedConverter/TaskTopicVisitor.java @@ -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; + } +} \ No newline at end of file diff --git a/example/extended-converter/src/main/resources/META-INF/services/org.camunda.community.migration.converter.visitor.DomElementVisitor b/example/extended-converter/src/main/resources/META-INF/services/org.camunda.community.migration.converter.visitor.DomElementVisitor index db7e829fa..1735fc371 100644 --- a/example/extended-converter/src/main/resources/META-INF/services/org.camunda.community.migration.converter.visitor.DomElementVisitor +++ b/example/extended-converter/src/main/resources/META-INF/services/org.camunda.community.migration.converter.visitor.DomElementVisitor @@ -1 +1,2 @@ -org.camunda.community.migration.example.extendedConverter.CustomDomElementVisitor \ No newline at end of file +org.camunda.community.migration.example.extendedConverter.CustomDomElementVisitor +org.camunda.community.migration.example.extendedConverter.TaskTopicVisitor \ No newline at end of file diff --git a/example/extended-converter/src/test/java/org/camunda/community/migration/example/extendedConverter/ExtendedConverterTest.java b/example/extended-converter/src/test/java/org/camunda/community/migration/example/extendedConverter/ExtendedConverterTest.java index d772a23c2..be9b461a2 100644 --- a/example/extended-converter/src/test/java/org/camunda/community/migration/example/extendedConverter/ExtendedConverterTest.java +++ b/example/extended-converter/src/test/java/org/camunda/community/migration/example/extendedConverter/ExtendedConverterTest.java @@ -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) { @@ -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); } } diff --git a/example/extended-converter/src/test/resources/ExternalTaskWorker_Example.bpmn b/example/extended-converter/src/test/resources/ExternalTaskWorker_Example.bpmn new file mode 100644 index 000000000..95d67f4f3 --- /dev/null +++ b/example/extended-converter/src/test/resources/ExternalTaskWorker_Example.bpmn @@ -0,0 +1,45 @@ + + + + + Flow_0zz19uu + + + + Flow_06q11dh + + + + Flow_0zz19uu + Flow_06q11dh + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index ad0ec020d..554a22d79 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,11 @@ spring-boot-starter-camunda ${version.spring-boot-starter-camunda} + + io.camunda.spring + java-common + ${version.spring-boot-starter-camunda} + io.camunda camunda-operate-client-java diff --git a/process-instance-migration/pom.xml b/process-instance-migration/pom.xml index 985394c23..37d5c970b 100644 --- a/process-instance-migration/pom.xml +++ b/process-instance-migration/pom.xml @@ -24,6 +24,10 @@ + + io.camunda.spring + java-common + io.camunda zeebe-protocol