Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
Issue #186
  • Loading branch information
rsoika committed Feb 19, 2023
1 parent 1e11029 commit 83e3862
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
import javax.json.JsonObjectBuilder;

/**
* The SchemaBuilder can be used to define a JSONFOrms Schema used by JSONForms
* The DataBuilder can be used to define a data structure for JSONFOrms
* <p>
* The schema defines the data type for each data element. If no schema
* definition is available, JSONForms will treat the property as a String.
* The schema of the data is defined by the SchemaBuilder class.
* <p>
* The mandatory scope property, which expects a JSON schema reference value,
* defines to which property of the data the control should be bound to.
* The DataBuilder can add simple key/value pairs as also complex data arrays
*
* @author rsoika
*
Expand All @@ -41,37 +39,39 @@ public class DataBuilder {

private static Logger logger = Logger.getLogger(DataBuilder.class.getName());

JsonObjectBuilder jsonObjectBuilder;
JsonObjectBuilder rootBuilder;
// arrays
JsonArrayBuilder objectArrayBuilder;
JsonObjectBuilder objectArrayItemBuilder;
String objectName = null;
JsonArrayBuilder arrayBuilder;
JsonObjectBuilder arrayObjectBuilder;
String arrayName = null;

/**
* Initialize the root json object builder
*/
public DataBuilder() {

jsonObjectBuilder = Json.createObjectBuilder();
rootBuilder = Json.createObjectBuilder();
}

/**
* Adds a new property
* Adds a new property to the root object.
* <p>
* In case an arrayBuilder was created before, the key/value pair is added into
* the arrayObjectBuilder
*
* @param name - name of the data property
* @param value - value of the data property
*
*/
public DataBuilder addData(final String name, final String value) {

if (objectArrayBuilder != null) {
if (objectArrayItemBuilder == null) {
objectArrayItemBuilder = Json.createObjectBuilder();
if (arrayBuilder != null) {
if (arrayObjectBuilder == null) {
arrayObjectBuilder = Json.createObjectBuilder();
}
objectArrayItemBuilder.add(name, value);

arrayObjectBuilder.add(name, value);
} else {
jsonObjectBuilder.add(name, value);
rootBuilder.add(name, value);
}
return this;

}

/**
Expand All @@ -81,30 +81,26 @@ public DataBuilder addData(final String name, final String value) {
* @return this
*/
public DataBuilder addArray(final String name) {

// close old array builder if exits
closeArrayBuilder();

// create new array builder
objectArrayBuilder = Json.createArrayBuilder();
objectName = name;

arrayBuilder = Json.createArrayBuilder();
arrayName = name;
return this;
}

/**
* Adds a new object to an existing objectArrayBuilder
* Adds a new object to the arrayObjectBuilder
*
* @return this
*/
public DataBuilder addObject() {

if (objectArrayBuilder != null) {
if (objectArrayItemBuilder != null) {
objectArrayBuilder.add(objectArrayItemBuilder);
if (arrayBuilder != null) {
// add an existing arrayItem into the arrayObject
if (arrayObjectBuilder != null) {
arrayBuilder.add(arrayObjectBuilder);
}

objectArrayItemBuilder = Json.createObjectBuilder();
arrayObjectBuilder = Json.createObjectBuilder();
} else {
logger.severe("You can not add an object to an array if no array was defined before!");
}
Expand All @@ -113,19 +109,18 @@ public DataBuilder addObject() {
}

/**
* Helper Method to close an open array builder
* Helper Method to close an open array builder. The method adds an existing
* arrayObjectBuilder.
*/
private void closeArrayBuilder() {
if (objectArrayItemBuilder != null) {
if (arrayObjectBuilder != null) {
if (arrayObjectBuilder != null) {
arrayBuilder.add(arrayObjectBuilder.build());
arrayObjectBuilder = null;

if (objectArrayItemBuilder != null) {
objectArrayBuilder.add(objectArrayItemBuilder);
}

jsonObjectBuilder.add(objectName, objectArrayBuilder);

rootBuilder.add(arrayName, arrayBuilder);
}

}

/**
Expand All @@ -134,7 +129,7 @@ private void closeArrayBuilder() {
public String build() {
closeArrayBuilder();
// write result
JsonObject jsonObject = jsonObjectBuilder.build();
JsonObject jsonObject = rootBuilder.build();
String result = null;
try (Writer writer = new StringWriter()) {
Json.createWriter(writer).write(jsonObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
********************************************************************************/
package org.openbpmn.glsp.jsonforms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.StringReader;
import java.util.logging.Logger;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.eclipse.glsp.server.model.DefaultGModelState;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -82,4 +89,53 @@ public void testArray() {

}

/**
* Verifies the creation of multiple arrays.
*
* See Issue #186
*/
@Test
public void testMultiArray() {

// ========================================
// Build a data object with 2 arrays
DataBuilder builder = new DataBuilder(). //
addData("name", "John"). //
addData("description", "Hello World");

// first array with one object
builder.addArray("scripts");
builder.addObject();
builder.addData("language", "javascript");
builder.addData("script", "1+1");
builder.addObject();
builder.addData("language", "java");
builder.addData("script", "1+1");

// second array with one object
builder.addArray("links");
builder.addObject();
builder.addData("source", "a-connector");
builder.addData("target", "b-connector");

String json = builder.build();
assertNotNull(json);
logger.info(json);

// ========================================
// validate the json structure
JsonReader jsonReader = Json.createReader(new StringReader(json));
JsonObject dataObject = jsonReader.readObject();
jsonReader.close();
// each array should contain exactly one object
assertNotNull(dataObject);
JsonArray array1 = dataObject.getJsonArray("scripts");
assertNotNull(array1);
assertEquals(2, array1.size());

JsonArray array2 = dataObject.getJsonArray("links");
assertNotNull(array2);
assertEquals(1, array2.size());
}

}

0 comments on commit 83e3862

Please sign in to comment.