Skip to content

Commit

Permalink
Added customization of the reference to the message and the signal
Browse files Browse the repository at this point in the history
  • Loading branch information
amotov committed Dec 25, 2024
1 parent 3078cbc commit d5ea842
Showing 1 changed file with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.openbpmn.bpmn.BPMNModel;
Expand Down Expand Up @@ -46,39 +47,47 @@ public double getDefaultHeight() {
* @throws BPMNMissingElementException
*/
public void addEventDefinition(String type) throws BPMNModelException {

if (this.getElementNode() == null) {
throw new BPMNMissingElementException("Missing ElementNode!");
}
Element eventDefinition = model.createElement(BPMNNS.BPMN2, type);
eventDefinition.setAttribute("id", BPMNModel.generateShortID(type));

// in case of a Signal Definition we need to add a reference to the first
// existing Signal.
if (BPMNTypes.EVENT_DEFINITION_SIGNAL.equals(type)) {
// do we have at least one signal ?
if (model.getSignals().size() == 0) {
if (model.getSignals().isEmpty()) {
// create a dummy signal
model.addSignal("signal_1", "Signal 1");
}
// take the first one
Signal signal = model.getSignals().iterator().next();
eventDefinition.setAttribute("signalRef", signal.getId());

addEventDefinition(signal.getId(), type);
return;
}

// in case of a Message Definition we need to add a reference to the first
// existing Message.
if (BPMNTypes.EVENT_DEFINITION_MESSAGE.equals(type)) {
// do we have at least one message ?
if (model.getMessages().size() == 0) {
if (model.getMessages().isEmpty()) {
// create a dummy message
model.addMessage("message_1", "Message 1");
}
// take the first one
Message message = model.getMessages().iterator().next();
eventDefinition.setAttribute("messageRef", message.getId());
addEventDefinition(message.getId(), type);
}
}

public void addEventDefinition(String id, String type) throws BPMNModelException {
if (this.getElementNode() == null) {
throw new BPMNMissingElementException("Missing ElementNode!");
}
Element eventDefinition = model.createElement(BPMNNS.BPMN2, type);
eventDefinition.setAttribute("id", BPMNModel.generateShortID(type));

if (BPMNTypes.EVENT_DEFINITION_SIGNAL.equals(type)) {
eventDefinition.setAttribute("signalRef", id);
}

if (BPMNTypes.EVENT_DEFINITION_MESSAGE.equals(type)) {
eventDefinition.setAttribute("messageRef", id);
}
this.getElementNode().appendChild(eventDefinition);

Expand Down Expand Up @@ -210,6 +219,8 @@ public List<BPMNValidationMarker> validate() {
"A Catch Event must have at least one outgoing Sequence Flow!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}

Message message = model.getMessages().iterator().next();
}

// Throw Event?
Expand All @@ -222,6 +233,38 @@ public List<BPMNValidationMarker> validate() {

}

getEventDefinitionsByType(BPMNTypes.EVENT_DEFINITION_MESSAGE)
.forEach(ed -> {
boolean hasMessage =
model.getMessages().stream()
.anyMatch(m ->
Objects.equals(
ed.getAttribute("messageRef"),
m.getId()));
if (!hasMessage) {
this.addValidationMarker(
new BPMNValidationMarker("Event",
"A Event must have a corresponding Message!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}
});

getEventDefinitionsByType(BPMNTypes.EVENT_DEFINITION_SIGNAL)
.forEach(ed -> {
boolean hasMessage =
model.getSignals().stream()
.anyMatch(m ->
Objects.equals(
ed.getAttribute("signalRef"),
m.getId()));
if (!hasMessage) {
this.addValidationMarker(
new BPMNValidationMarker("Event",
"A Event must have a corresponding Signal!", this.getId(),
BPMNValidationMarker.ErrorType.ERROR));
}
});

return this.getValidationMarkers();
}

Expand Down

0 comments on commit d5ea842

Please sign in to comment.