Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue imixs#183
  • Loading branch information
rsoika committed Feb 18, 2023
1 parent c41acd3 commit 4190ad1
Show file tree
Hide file tree
Showing 18 changed files with 753 additions and 754 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@
********************************************************************************/
package org.openbpmn.extension;

import java.util.Optional;

import javax.json.JsonObject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.glsp.graph.GModelElement;
import org.eclipse.glsp.graph.GNode;
import org.openbpmn.bpmn.BPMNNS;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.glsp.bpmn.LabelGNode;
import org.openbpmn.glsp.model.BPMNGModelState;
import org.openbpmn.glsp.utils.BPMNGraphUtil;

import com.google.inject.Inject;

/**
* This is Abstract implementation provides some core funtionallity like update
Expand All @@ -28,6 +41,15 @@
*/
abstract class AbstractBPMNElementExtension implements BPMNExtension {

private static Logger logger = LogManager.getLogger(AbstractBPMNElementExtension.class);

@Inject
protected BPMNGModelState modelState;

public BPMNGModelState getModelState() {
return modelState;
}

/**
* Returns the Extension label to be used in the Tool Palette. The default name
* is the namespace. Implementations should overwrite this method.
Expand Down Expand Up @@ -61,7 +83,33 @@ public String getNamespaceURI() {

@Override
public int getPriority() {
return 999999;
return 1;
}

/**
* This method updates the name attribute of a BPMNElement and also the
* corresponding GNode Element in the diagram plane.
*
* @param json
* @param bpmnElement
* @param gNodeElement
*/
public void updateNameProperty(final JsonObject json, BPMNElement bpmnElement, final GModelElement gNodeElement) {
// Update the name feature
String name = json.getString("name", "");
if (!name.equals(bpmnElement.getName())) {
bpmnElement.setName(name);
// Update Label...
Optional<GModelElement> label = modelState.getIndex().get(gNodeElement.getId() + "_bpmnlabel");
if (!label.isEmpty()) {
LabelGNode lgn = (LabelGNode) label.get();
// update the bpmn-text-node of the GNodeElement
GNode gnode = BPMNGraphUtil.findMultiLineTextNode(lgn);
if (gnode != null) {
gnode.getArgs().put("text", name);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/********************************************************************************
* Copyright (c) 2022 Imixs Software Solutions GmbH and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
package org.openbpmn.extension;

import java.util.Iterator;
import java.util.Set;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.glsp.graph.GModelElement;
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.core.BPMNElement;
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.w3c.dom.Element;

/**
* The TimerEventDefinitionExtension is responsible to read and update optional
* TimerEventDefinitions from the BPMN model. The Extension builds a custom
* property section named 'Timers' shown a list of all TimerEventDefinitions
* define in a Event.
*
* @author rsoika
*/
public class ConditionalEventDefinitionExtension extends DefaultBPMNEventExtension {

private static Logger logger = LogManager.getLogger(DefaultBPMNSequenceFlowExtension.class);

@Override
public int getPriority() {
return 101;
}

/**
* Returns if this Extension can be applied to the given elementTypeID
*/
@Override
public boolean handlesElementTypeId(final String elementTypeId) {
return BPMNTypes.BPMN_EVENTS.contains(elementTypeId);
}

/**
* This Helper Method generates a JSON Object with the BPMNElement properties.
* <p>
* This json object is used on the GLSP Client to generate the EMF JsonForms
*/
@Override
public void buildPropertiesForm(final BPMNElement bpmnElement, final DataBuilder dataBuilder,
final SchemaBuilder schemaBuilder, final UISchemaBuilder uiSchemaBuilder) {

Event event = (Event) bpmnElement;

// Conditional

Set<Element> conditionalEventDefinition = event.getEventDefinitionsByType("conditionalEventDefinition");

if (conditionalEventDefinition.size() > 0) {

JsonObject multilineOption = Json.createObjectBuilder() //
.add("multi", true).build();

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

// create a detail control Layout....
JsonArrayBuilder controlsArrayBuilder = Json.createArrayBuilder();
controlsArrayBuilder //
.add(Json.createObjectBuilder() //
.add("type", "Control") //
.add("scope", "#/properties/language"))//
.add(Json.createObjectBuilder() //
.add("type", "Control") //
.add("scope", "#/properties/expression") //
.add("label", "Expression") //
.add("options", multilineOption) //
);

JsonObjectBuilder detailLayoutBuilder = Json.createObjectBuilder(). //
add("type", "VerticalLayout"). ///
add("elements", controlsArrayBuilder);

JsonObjectBuilder detailBuilder = Json.createObjectBuilder(). //
add("detail", detailLayoutBuilder.build());
uiSchemaBuilder.addDetailLayout("conditions", "Conditions", detailBuilder.build());

/*
* Add the Schema ....
*/
schemaBuilder.addArray("conditions");
schemaBuilder.addProperty("language", "string", null, null);
schemaBuilder.addProperty("expression", "string", null, null);

/*
* Now we can create the data structure - each conditionalEventDefinition is
* represented as a separate object
*/
dataBuilder.addArray("conditions");
for (Element definition : conditionalEventDefinition) {
dataBuilder.addObject();
dataBuilder.addData("language", definition.getAttribute("language"));
dataBuilder.addData("expression", definition.getAttribute("expression"));
}

}
}

/**
* Update the timers definitions
*
* This method updates all timerEventDefinitions. The method expects a
* dataList containing all timer definitions with its values.
* The method simply overwrites all timerEventDefinitions.
* <p>
* Example:
*
* <pre>
* {@code
* <bpmn2:startEvent id="event_n8bj0g" name="Event-1">
* <bpmn2:documentation id="documentation_4XhEKA"/>
* <bpmn2:timerEventDefinition id="timerEventDefinition_hElKhw">
* <bpmn2:timeDuration id="FormalExpression_0" xsi:type=
"bpmn2:tFormalExpression">3cc</bpmn2:timeDuration>
* </bpmn2:timerEventDefinition>
* </bpmn2:startEvent>
* }
* </pre>
*/
@Override
public void updatePropertiesData(final JsonObject json, final BPMNElement bpmnElement,
final GModelElement gNodeElement) {
Event bpmnEvent = (Event) bpmnElement;
JsonArray dataList = json.getJsonArray("conditions");

// synchronize the definition list of the event element
Set<Element> conditionalEventDefinitions = synchronizeEventDefinitions("conditionalEventDefinition", bpmnEvent,
dataList);

// now we can update the values one by one
// NOTE: the id can change within the definitionList if an element was deleted
// or moved!
// but we do not care about this issue.
Iterator<Element> iter = conditionalEventDefinitions.iterator();
int i = 0;
while (iter.hasNext()) {
Element eventDefinitionElement = iter.next();
JsonObject jsonData = dataList.getJsonObject(i); // .get(i);
if (jsonData != null) {
eventDefinitionElement.setAttribute("language", jsonData.getString("language", ""));
eventDefinitionElement.setAttribute("expression", jsonData.getString("expression", ""));
}
i++;
// update completed
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,18 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.json.JsonObject;

import org.eclipse.glsp.graph.GModelElement;
import org.eclipse.glsp.graph.GNode;
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.DataObject;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.glsp.bpmn.LabelGNode;
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.BPMNGModelState;
import org.openbpmn.glsp.utils.BPMNGraphUtil;

import com.google.inject.Inject;

Expand Down Expand Up @@ -94,37 +88,14 @@ public void buildPropertiesForm(final BPMNElement bpmnElement, final DataBuilder

}

/**
* Update the DataObject properties
*/
@Override
public void updatePropertiesData(final JsonObject json, final BPMNElement bpmnElement,
final GModelElement gNodeElement) {

// default update of name and documentation
Set<String> features = json.keySet();
for (String feature : features) {

if ("name".equals(feature)) {
String text = json.getString(feature);
bpmnElement.setName(text);
// Update GModelElement Label...
Optional<GModelElement> label = modelState.getIndex().get(gNodeElement.getId() + "_bpmnlabel");
if (!label.isEmpty()) {
LabelGNode lgn = (LabelGNode) label.get();
// update the bpmn-text-node of the GNodeElement
GNode gnode = BPMNGraphUtil.findMultiLineTextNode(lgn);
if (gnode != null) {
gnode.getArgs().put("text", text);
}
continue;
}
continue;
}
if ("documentation".equals(feature)) {
((BPMNElementNode) bpmnElement).setDocumentation(json.getString(feature));
continue;
}

}

updateNameProperty(json, bpmnElement, gNodeElement);
// update attributes and tags
bpmnElement.setDocumentation(json.getString("documentation"));
}

}
Loading

0 comments on commit 4190ad1

Please sign in to comment.