Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
BZ-1007971 - Decision logic design should be more intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedvede committed Nov 15, 2013
1 parent 63177f0 commit f972842
Show file tree
Hide file tree
Showing 24 changed files with 2,365 additions and 78 deletions.
@@ -0,0 +1,102 @@
/**
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.designer.expressioneditor.marshalling;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.jbpm.designer.expressioneditor.model.Condition;
import org.jbpm.designer.expressioneditor.model.ExpressionEditorMessage;
import org.jbpm.designer.expressioneditor.model.ConditionExpression;

import java.io.ByteArrayOutputStream;

public class ExpressionEditorMessageJSONMarshaller {

public ExpressionEditorMessageJSONMarshaller() {

}

public String marshall(ExpressionEditorMessage message) throws Exception {

if (message == null) return null;

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator generator = jsonFactory.createJsonGenerator(outputStream, JsonEncoding.UTF8);

generator.writeStartObject();

if (message.getExpression() == null) message.setExpression(new ConditionExpression());

if (message.getExpression() != null) {

generator.writeFieldName(ExpressionEditorMessageTokens.OPERATOR_TOKEN);
if (message.getExpression().getOperator() != null) {
generator.writeString(message.getExpression().getOperator());
} else {
generator.writeNull();
}

generator.writeArrayFieldStart(ExpressionEditorMessageTokens.CONDITIONS_TOKEN);
if (message.getExpression().getConditions().size() > 0) {
for (Condition condition : message.getExpression().getConditions()) {
generator.writeStartObject();
generator.writeFieldName(ExpressionEditorMessageTokens.CONDITION_TOKEN);
generator.writeString(condition.getFunction());
if (condition.getParameters().size() > 0) {
generator.writeArrayFieldStart(ExpressionEditorMessageTokens.PARAMETERS_TOKEN);
for (String param : condition.getParameters()) {
generator.writeString(param);
}
generator.writeEndArray();
}
generator.writeEndObject();
}
}
generator.writeEndArray();
}

generator.writeFieldName(ExpressionEditorMessageTokens.SCRIPT_TOKEN);
if (message.getScript() != null) {
generator.writeString(message.getScript());
} else {
generator.writeNull();
}

generator.writeFieldName(ExpressionEditorMessageTokens.ERROR_CODE_TOKEN);
if (message.getErrorCode() != null) {
generator.writeString(message.getErrorCode());
} else {
generator.writeNull();
}

generator.writeFieldName(ExpressionEditorMessageTokens.ERROR_MESSAGE_TOKEN);
if (message.getErrorMessage() != null) {
generator.writeString(message.getErrorMessage());
} else {
generator.writeNull();
}

generator.writeEndObject();

generator.close();
return outputStream.toString();

}

}
@@ -0,0 +1,144 @@
/**
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.designer.expressioneditor.marshalling;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonToken;
import org.jbpm.designer.expressioneditor.model.Condition;
import org.jbpm.designer.expressioneditor.model.ExpressionEditorMessage;
import org.jbpm.designer.expressioneditor.model.ConditionExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class ExpressionEditorMessageJSONUnmarshaller {

private static final Logger logger = LoggerFactory.getLogger(ExpressionEditorMessageJSONUnmarshaller.class);

public ExpressionEditorMessageJSONUnmarshaller() {
}

public ExpressionEditorMessage unmarshall(String jsonMessage) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
JsonParser parser = null;
try {
parser = jsonFactory.createJsonParser(jsonMessage);
} catch (Exception e) {
logger.error("It was not possible to create a json parser for the jsonMessage: " + jsonMessage, e);
throw e;
}
return unmarshall(parser);
}

public ExpressionEditorMessage unmarshall(InputStream inputStream) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
JsonParser parser = null;
try {
parser = jsonFactory.createJsonParser(inputStream);
} catch (Exception e) {
logger.error("It was not possible to create the a json parser for the inputStream: " + inputStream, e);
throw e;
}
return unmarshall(parser);
}


private ExpressionEditorMessage unmarshall(JsonParser parser) throws Exception {
ExpressionEditorMessage message = new ExpressionEditorMessage();
ConditionExpression expression = new ConditionExpression();

try {
String currentField;

while (parser.nextToken() != JsonToken.END_OBJECT) {

currentField = parser.getCurrentName();

if (ExpressionEditorMessageTokens.OPERATOR_TOKEN.equals(currentField)) {
parser.nextToken();
expression.setOperator(parseText(parser));
message.setExpression(expression);
} else if (ExpressionEditorMessageTokens.CONDITIONS_TOKEN.equals(currentField)) {
List<Condition> conditions = parseConditions(parser);
if (conditions.size() > 0) {
expression.setConditions(conditions);
message.setExpression(expression);
}
} else if (ExpressionEditorMessageTokens.SCRIPT_TOKEN.equals(currentField)) {
parser.nextToken();
message.setScript(parseText(parser));
} else if (ExpressionEditorMessageTokens.ERROR_CODE_TOKEN.equals(currentField)) {
parser.nextToken();
message.setErrorCode(parseText(parser));
} else if (ExpressionEditorMessageTokens.ERROR_MESSAGE_TOKEN.equals(currentField)) {
parser.nextToken();
message.setErrorMessage(parseText(parser));
}
}
} catch (IOException e) {
logger.error("An error was produced during json message parsing. " + e);
throw e;
}
return message;
}

private String parseText(JsonParser parser) throws IOException, JsonParseException {
return parser.getText();
}

private List<Condition> parseConditions(JsonParser parser) throws IOException, JsonParseException {
List<Condition> result = new ArrayList<Condition>();
Condition condition;

if (parser.nextToken() == JsonToken.START_ARRAY) {
while (parser.nextToken() != JsonToken.END_ARRAY) {
if ((condition = parseCondition(parser)) != null) {
result.add(condition);
}
}
}
return result;
}

private Condition parseCondition(JsonParser parser) throws IOException, JsonParseException {
Condition condition = null;
if (parser.getCurrentToken() == JsonToken.START_OBJECT && parser.nextToken() != JsonToken.END_OBJECT) {
condition = new Condition();
if (ExpressionEditorMessageTokens.CONDITION_TOKEN.equals(parser.getCurrentName())) {
parser.nextToken();
condition.setFunction(parseText(parser));
}
if (parser.nextToken() != JsonToken.END_OBJECT && ExpressionEditorMessageTokens.PARAMETERS_TOKEN.equals(parser.getCurrentName())) {
//parser the parameters
if (parser.nextToken() == JsonToken.START_ARRAY) {
while (parser.nextToken() != JsonToken.END_ARRAY) {
condition.addParam(parseText(parser));
}
}
}
}
return condition;
}

}
@@ -0,0 +1,13 @@
package org.jbpm.designer.expressioneditor.marshalling;

public interface ExpressionEditorMessageTokens {

static final String OPERATOR_TOKEN = "operator";
static final String CONDITION_TOKEN = "condition";
static final String CONDITIONS_TOKEN = "conditions";
static final String PARAMETERS_TOKEN = "parameters";
static final String SCRIPT_TOKEN = "script";
static final String ERROR_CODE_TOKEN = "errorCode";
static final String ERROR_MESSAGE_TOKEN = "errorMessage";

}
@@ -0,0 +1,55 @@
/**
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.designer.expressioneditor.model;

import java.util.ArrayList;
import java.util.List;

public class Condition {

private String function;

private List<String> parameters = new ArrayList<String>();

public Condition() {
}

public Condition(String function) {
this.function = function;
}

public String getFunction() {
return function;
}

public void setFunction(String function) {
this.function = function;
}

public List<String> getParameters() {
return parameters;
}

public void setParameters(List<String> parameters) {
this.parameters = parameters;
}

public void addParam(String param) {
parameters.add(param);
}

}
@@ -0,0 +1,54 @@
/**
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jbpm.designer.expressioneditor.model;

import java.util.ArrayList;
import java.util.List;

public class ConditionExpression {

public static final String AND_OPERATOR = "AND";

public static final String OR_OPERATOR = "OR";

private String operator;

private List<Condition> conditions = new ArrayList<Condition>();

public ConditionExpression() {
}

public ConditionExpression(String operator) {
this.operator = operator;
}

public String getOperator() {
return operator;
}

public void setOperator(String operator) {
this.operator = operator;
}

public List<Condition> getConditions() {
return conditions;
}

public void setConditions(List<Condition> conditions) {
this.conditions = conditions;
}
}

0 comments on commit f972842

Please sign in to comment.