Skip to content

Instantly share code, notes, and snippets.

@molleafauss
Created December 18, 2014 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molleafauss/5ea6ff35acee7538d2d8 to your computer and use it in GitHub Desktop.
Save molleafauss/5ea6ff35acee7538d2d8 to your computer and use it in GitHub Desktop.
Activiti bug(?) with Extension Attributes
package activiti;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicInteger;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.Activity;
import org.activiti.bpmn.model.BoundaryEvent;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExtensionAttribute;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.TerminateEventDefinition;
import org.activiti.bpmn.model.TimerEventDefinition;
import org.activiti.bpmn.model.UserTask;
public class CustomAttributeBug {
private static final String NAMESPACE = "http://www.acme.com/bpmn";
private static final String PFX = "pfx";
public static void main(String[] args) throws UnsupportedEncodingException {
CustomAttributeBug bug = new CustomAttributeBug();
bug.generate();
BpmnXMLConverter conv = new BpmnXMLConverter();
byte[] bstr = conv.convertToXML(bug.model);
System.out.println(new String(bstr, "UTF-8"));
}
private Process process;
private BpmnModel model;
private AtomicInteger idx;
public CustomAttributeBug() {
process = new Process();
process.setId("TEST");
model = new BpmnModel();
model.addNamespace(PFX, NAMESPACE);
model.addProcess(process);
idx = new AtomicInteger();
}
public void generate() {
String start = startEvent();
String task1 = userTask("Task 1", "TestValue");
flow(start, task1, null);
String task2 = userTask("Task with Timeout", null);
String timer = timerBoundary("PT10M", task2, "CustomValue");
flow(task1, task2, null);
String end1 = endEvent(false);
flow(task2, end1, null);
String task3 = userTask("Task on Timeout", "FooBar");
flow(timer, task3, null);
String end2 = endEvent(false);
flow(task3, end2, null);
}
public void flow(String source, String target, String condition) {
SequenceFlow flow = new SequenceFlow();
String fid = "flow" + idx.incrementAndGet();
flow.setId(fid);
flow.setSourceRef(source);
flow.setTargetRef(target);
if(condition != null) {
flow.setConditionExpression(condition);
}
process.addFlowElement(flow);
}
public String startEvent() {
StartEvent start = new StartEvent();
String id = "startevent" + idx.incrementAndGet();
start.setId(id);
start.setName("Start Event");
process.addFlowElement(start);
return id;
}
public String userTask(String name, String attribute) {
UserTask task = new UserTask();
String id = "usertask" + idx.incrementAndGet();
task.setId(id);
task.setName(name);
if(attribute != null) {
setAttribute(task, attribute);
}
process.addFlowElement(task);
return id;
}
public String endEvent(boolean terminate) {
EndEvent event = new EndEvent();
String id = "endevent" + idx.incrementAndGet();
event.setId(id);
if(terminate) {
event.addEventDefinition(new TerminateEventDefinition());
}
process.addFlowElement(event);
return id;
}
public String timerBoundary(String expression, String attached, String customAttribute) {
String id = "boundaryevent" + idx.incrementAndGet();
BoundaryEvent be = new BoundaryEvent();
be.setId(id);
be.setName("Timer");
be.setAttachedToRef((Activity) process.getFlowElement(attached));
be.setCancelActivity(false);
TimerEventDefinition event = new TimerEventDefinition();
event.setTimeDate(expression);
be.addEventDefinition(event);
process.addFlowElement(be);
setAttribute(be, customAttribute);
return id;
}
public void setAttribute(FlowElement element, String value) {
ExtensionAttribute attr = new ExtensionAttribute("customAttribute");
attr.setNamespace(NAMESPACE);
attr.setValue(value);
element.addAttribute(attr);
}
}
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:pfx="http://www.acme.com/bpmn"
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.activiti.org/test">
<process id="TEST" isExecutable="true">
<startEvent id="startevent1" name="Start Event"/>
<userTask id="usertask2" name="Task 1" pfx:customAttribute="TestValue"/>
<sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask2"/>
<userTask id="usertask4" name="Task with Timeout"/>
<boundaryEvent id="boundaryevent5" name="Timer" attachedToRef="usertask4" cancelActivity="false">
<timerEventDefinition>
<timeDate>PT10M</timeDate>
</timerEventDefinition>
</boundaryEvent>
<sequenceFlow id="flow6" sourceRef="usertask2" targetRef="usertask4"/>
<endEvent id="endevent7"/>
<sequenceFlow id="flow8" sourceRef="usertask4" targetRef="endevent7"/>
<userTask id="usertask9" name="Task on Timeout" pfx:customAttribute="FooBar"/>
<sequenceFlow id="flow10" sourceRef="boundaryevent5" targetRef="usertask9"/>
<endEvent id="endevent11"/>
<sequenceFlow id="flow12" sourceRef="usertask9" targetRef="endevent11"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_TEST">
<bpmndi:BPMNPlane bpmnElement="TEST" id="BPMNPlane_TEST"/>
</bpmndi:BPMNDiagram>
</definitions>
@molleafauss
Copy link
Author

API visible comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment