Skip to content

Commit

Permalink
optimized behavior and delete signal situation
Browse files Browse the repository at this point in the history
Issue imixs#222
  • Loading branch information
rsoika committed Mar 22, 2023
1 parent a7ed809 commit 43ccfca
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
********************************************************************************/
package org.openbpmn.extension;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonArray;
Expand All @@ -32,13 +36,16 @@
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.Signal;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.glsp.bpmn.BPMNGNode;
import org.openbpmn.glsp.jsonforms.DataBuilder;
import org.openbpmn.glsp.jsonforms.SchemaBuilder;
import org.openbpmn.glsp.jsonforms.UISchemaBuilder;
import org.openbpmn.glsp.jsonforms.UISchemaBuilder.Layout;
import org.openbpmn.glsp.model.BPMNGModelFactory;
import org.openbpmn.glsp.model.BPMNGModelState;
import org.openbpmn.glsp.operations.BPMNPropertyPanelUpdateAction;
import org.w3c.dom.Element;
Expand All @@ -63,6 +70,11 @@ public class DefaultBPMNDefinitionsExtension extends AbstractBPMNElementExtensio
@Inject
protected BPMNGModelState modelState;

@Inject
protected BPMNGModelFactory bpmnGModelFactory;

private boolean refreshProperties = false;

public DefaultBPMNDefinitionsExtension() {
super();
}
Expand Down Expand Up @@ -122,7 +134,64 @@ public void buildPropertiesForm(final BPMNElement bpmnElement, final DataBuilder
addElements("targetNamespace", "exporter", "exporterVersion"); //

// Signal List
addSignals(modelState.getBpmnModel(), dataBuilder, schemaBuilder, uiSchemaBuilder);
buildSignalProperties(modelState.getBpmnModel(), dataBuilder, schemaBuilder, uiSchemaBuilder);

}

/**
* Adds the Signal set schema properties
*
* @param eventDefinitions
* @param dataBuilder
* @param schemaBuilder
* @param uiSchemaBuilder
*/
private void buildSignalProperties(final BPMNModel model, final DataBuilder dataBuilder,
final SchemaBuilder schemaBuilder,
final UISchemaBuilder uiSchemaBuilder) {

Map<String, String> multilineOption = new HashMap<>();
multilineOption.put("multi", "true");

if (model.getSignals().size() > 0) {
Map<String, String> arrayDetailOption = new HashMap<>();
// GENERATED | DEFAULT | HorizontalLayout
arrayDetailOption.put("detail", "VerticalLayout");

uiSchemaBuilder. //
addCategory("Signals"). //
addLayout(Layout.VERTICAL);

JsonObjectBuilder layoutBuilder = Json.createObjectBuilder().add("type", "VerticalLayout");
JsonArrayBuilder controlsArrayBuilder = Json.createArrayBuilder();
controlsArrayBuilder //
.add(Json.createObjectBuilder() //
.add("type", "Control") //
.add("scope", "#/properties/name")//
);
layoutBuilder.add("elements", controlsArrayBuilder);
JsonObjectBuilder detailBuilder = Json.createObjectBuilder(). //
add("detail", layoutBuilder.build());

uiSchemaBuilder.addDetailLayout("signals", "Signals", detailBuilder.build());

// Schema builder
schemaBuilder.addArray("signals");
schemaBuilder.addProperty("name", "string", null, null);

/*
* create the data structure for the signals - each signal is represented as a
* separate object
*/
dataBuilder.addArray("signals");
for (Signal bpmnSignal : model.getSignals()) {
dataBuilder.addObject();
dataBuilder.addData("name", bpmnSignal.getName());
// the id is used to find the signal in the bpmn model later
dataBuilder.addData("id", bpmnSignal.getId());
}
dataBuilder.closeArray();
}

}

Expand All @@ -132,6 +201,7 @@ public void buildPropertiesForm(final BPMNElement bpmnElement, final DataBuilder
@Override
public void updatePropertiesData(final JsonObject json, final String category, final BPMNElement bpmnElement,
final GModelElement gNodeElement) {

Element definitions = modelState.getBpmnModel().getDefinitions();

if ("General".equals(category)) {
Expand All @@ -149,26 +219,28 @@ public void updatePropertiesData(final JsonObject json, final String category, f
boolean update = false;
logger.debug("...update signals.. ");
JsonArray signalSetValues = json.getJsonArray("signals");

List<String> signalIdList = new ArrayList<String>();
if (signalSetValues != null) {
for (JsonValue signalValue : signalSetValues) {
JsonObject signalData = (JsonObject) signalValue;
String id = signalData.getString("id", null);
Signal signal = (Signal) modelState.getBpmnModel().findElementById(id);
if (signal != null) {
signalIdList.add(id);
// did the name change?
String newName = signalData.getString("name", "");
if (!newName.equals(signal.getName())) {
signal.setName(newName);
// update = true; // Causes loop
// modelState.reset();
// Update the properties for all signal events!
updateSignalEvents();
}

} else {
// signal did not yet exist in definition list - so we create a new one
int i = modelState.getBpmnModel().getSignals().size() + 1;
String newSignalID = "signal_" + i;
String newSignalName = "Signal " + i;
signalIdList.add(newSignalID);
try {
modelState.getBpmnModel().addSignal(newSignalID, newSignalName);
} catch (BPMNModelException e) {
Expand All @@ -177,11 +249,19 @@ public void updatePropertiesData(final JsonObject json, final String category, f
update = true; // reset signal state
}
}

// remove signals from definitions which are not longer in the list
// collect all affected ids...
if (removeDeprecatedSignals(signalIdList)) {
update = true;
}

} else {
update = true; // reset signal state
}
if (update) {
modelState.reset();
bpmnGModelFactory.applyBPMNExtensions(gNodeElement, bpmnElement);
// send an update for the property panel to the client...
actionDispatcher
.dispatchAfterNextUpdate(new BPMNPropertyPanelUpdateAction());
Expand All @@ -191,59 +271,51 @@ public void updatePropertiesData(final JsonObject json, final String category, f
}

/**
* Adds the Signal set schema properties
*
* @param eventDefinitions
* @param dataBuilder
* @param schemaBuilder
* @param uiSchemaBuilder
* Remove signals from definitions which are not longer in a given list of
* signal Ids
* <p>
* The method returns true if the list was updated
*
*/
private void addSignals(final BPMNModel model, final DataBuilder dataBuilder, final SchemaBuilder schemaBuilder,
final UISchemaBuilder uiSchemaBuilder) {

Map<String, String> multilineOption = new HashMap<>();
multilineOption.put("multi", "true");

if (model.getSignals().size() > 0) {
Map<String, String> arrayDetailOption = new HashMap<>();
// GENERATED | DEFAULT | HorizontalLayout
arrayDetailOption.put("detail", "VerticalLayout");

uiSchemaBuilder. //
addCategory("Signals"). //
addLayout(Layout.VERTICAL);

JsonObjectBuilder layoutBuilder = Json.createObjectBuilder().add("type", "VerticalLayout");
JsonArrayBuilder controlsArrayBuilder = Json.createArrayBuilder();
controlsArrayBuilder //
.add(Json.createObjectBuilder() //
.add("type", "Control") //
.add("scope", "#/properties/name")//
);
layoutBuilder.add("elements", controlsArrayBuilder);
JsonObjectBuilder detailBuilder = Json.createObjectBuilder(). //
add("detail", layoutBuilder.build());

uiSchemaBuilder.addDetailLayout("signals", "Signals", detailBuilder.build());

// Schema builder
schemaBuilder.addArray("signals");
schemaBuilder.addProperty("name", "string", null, null);

/*
* create the data structure for the signals - each signal is represented as a
* separate object
*/
dataBuilder.addArray("signals");
for (Signal bpmnSignal : model.getSignals()) {
dataBuilder.addObject();
dataBuilder.addData("name", bpmnSignal.getName());
// the id is used to find the signal in the bpmn model later
dataBuilder.addData("id", bpmnSignal.getId());
private boolean removeDeprecatedSignals(List<String> signalIdList) {
boolean update = false;
// remove signals from definitions which are not longer in the list
// collect all affected ids...
Iterator<Signal> _signalsIter = modelState.getBpmnModel().getSignals().iterator();
List<String> _toRemove = new ArrayList<String>();
while (_signalsIter.hasNext()) {
Signal _signal = _signalsIter.next();
if (!signalIdList.contains(_signal.getId())) {
_toRemove.add(_signal.getId());
}
dataBuilder.closeArray();
}
// now remove all signals no longer listed in the properties
for (String idToRemove : _toRemove) {
modelState.getBpmnModel().deleteSignal(idToRemove);
update = true;
}
// return final update state
return update;
}

/**
* This helper method updates the property data for all signal events. This
* method is needed in case the name of an existing event changed.
*/
private void updateSignalEvents() {
Set<Event> events = modelState.getBpmnModel().findAllEvents();
for (Event event : events) {
// test if this event is an signal event
Set<Element> signalEventDefinitions = event.getEventDefinitionsByType("signalEventDefinition");
if (signalEventDefinitions.size() > 0) {
// find the gmodel
BPMNGNode _baseElement = modelState.getIndex().findElementByClass(event.getId(),
BPMNGNode.class).orElse(null);
if (_baseElement != null) {
bpmnGModelFactory.applyBPMNExtensions(_baseElement, event);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public GGraph buildGGraph(final BPMNModel model) {
* @param elementNode
* @param bpmnElement
*/
void applyBPMNExtensions(final GModelElement elementNode, final BPMNElement bpmnElement) {
public void applyBPMNExtensions(final GModelElement elementNode, final BPMNElement bpmnElement) {
// finally we define the JSONForms schemata
DataBuilder dataBuilder = new DataBuilder();
UISchemaBuilder uiSchemaBuilder = new UISchemaBuilder(Layout.CATEGORIZATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ public BPMNElement findElementById(String id) {
* <p>
* If no event exists, the method returns an empty list.
*
* @return
* @return the list of events or an empty is if no events exist.
*/
public Set<Event> findAllEvents() {
LinkedHashSet<Event> result = new LinkedHashSet<Event>();
Expand Down

0 comments on commit 43ccfca

Please sign in to comment.