Skip to content

Commit

Permalink
#451 prelim
Browse files Browse the repository at this point in the history
  • Loading branch information
Donald Oakes committed Jun 10, 2020
1 parent 36efb9f commit 3b26dbe
Show file tree
Hide file tree
Showing 36 changed files with 436 additions and 429 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public JSONObject toJson(Object obj) throws TranslationException {
return (JSONObject) obj;
}

public Object fromJson(JSONObject json) throws TranslationException {
public Object fromJson(JSONObject json, String type) throws TranslationException {
return json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,37 @@

public class JsonableTranslator extends DocumentReferenceTranslator implements JsonTranslator {

/**
* TODO honor runtime type
*/
@Override
public Object toObject(String str, String type) throws TranslationException {
try {
JSONObject json = new JsonObject(str);
return createJsonable(json);
return createJsonable(json, type);
}
catch (Exception ex) {
throw new TranslationException(ex.getMessage(), ex);
}
}

/**
* TODO prop-driven JSONABLE_TYPE population
*/
@Override
public String toString(Object obj, String variableType) throws TranslationException {
Jsonable jsonable = (Jsonable) obj;
JSONObject json = new JsonObject();
try {
json.put(JSONABLE_TYPE, jsonable.getClass().getName());
String name = jsonable.getJsonName();
if (name == null)
name = jsonable.getClass().getSimpleName().substring(0, 1).toLowerCase() + jsonable.getClass().getSimpleName().substring(1);

json.put(name, jsonable.getJson());
return json.toString(2);
return toJson(obj).toString(2);
}
catch (JSONException ex) {
throw new TranslationException(ex.getMessage(), ex);
}
}

public JSONObject toJson(Object obj) throws TranslationException {
try {
if (obj instanceof JSONObject)
return (JSONObject)obj;
else
return ((Jsonable)obj).getJson();
}
catch (JSONException ex) {
throw new TranslationException(ex.getMessage(), ex);
}
}

public Object fromJson(JSONObject json) throws TranslationException {
public Object fromJson(JSONObject json, String type) throws TranslationException {
try {
return createJsonable(json);
return createJsonable(json, type);
}
catch (Exception ex) {
throw new TranslationException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JSONObject toJson(Object obj) throws TranslationException {
}

@Override
public Object fromJson(JSONObject json) throws TranslationException {
public Object fromJson(JSONObject json, String type) throws TranslationException {
return json.toString(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package com.centurylink.mdw.common.translator.impl;

import java.beans.IntrospectionException;
import java.util.Set;

import com.centurylink.mdw.bpm.ProcessExecutionPlanDocument;
import com.centurylink.mdw.model.ExecutionPlan;
import com.centurylink.mdw.model.Jsonable;
import com.centurylink.mdw.translator.DocumentReferenceTranslator;
import com.centurylink.mdw.translator.JsonTranslator;
import com.centurylink.mdw.translator.TranslationException;
import com.centurylink.mdw.translator.XmlDocumentTranslator;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand All @@ -28,13 +32,8 @@
import org.yaml.snakeyaml.introspector.Property;
import org.yaml.snakeyaml.representer.Representer;

import com.centurylink.mdw.bpm.ProcessExecutionPlanDocument;
import com.centurylink.mdw.model.ExecutionPlan;
import com.centurylink.mdw.model.Jsonable;
import com.centurylink.mdw.translator.DocumentReferenceTranslator;
import com.centurylink.mdw.translator.JsonTranslator;
import com.centurylink.mdw.translator.TranslationException;
import com.centurylink.mdw.translator.XmlDocumentTranslator;
import java.beans.IntrospectionException;
import java.util.Set;

/**
* By convention, the first line of serialized yaml is a comment that
Expand Down Expand Up @@ -125,14 +124,9 @@ public JSONObject toJson(Object obj) throws TranslationException {
}

@Override
public Object fromJson(JSONObject json) throws TranslationException {
public Object fromJson(JSONObject json, String type) throws TranslationException {
try {
if (json.has(JSONABLE_TYPE)) {
return createJsonable(json);
}
else {
throw new UnsupportedOperationException("No Json > Yaml deserialization for non-Jsonables");
}
return createJsonable(json, type);
}
catch (Exception ex) {
throw new TranslationException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,13 @@ public JSONObject toJson(String variableName, Object objectValue) throws Transla
}
}

/**
* For Jsonable, _type property is required.
*/
public Object fromJson(String variableName, JSONObject json) throws TranslationException {
public Object fromJson(String variableName, JSONObject json, String type) throws TranslationException {
if (json == null)
return null;
Variable variable = context.getProcess().getVariable(variableName);
VariableTranslator translator = context.getPackage().getTranslator(variable.getType());
if (translator instanceof JsonTranslator) {
return ((JsonTranslator)translator).fromJson(json);
return ((JsonTranslator)translator).fromJson(json, type);
}
else {
throw new TranslationException("Cannot convert from JSON using " + translator.getClass());
Expand Down
58 changes: 40 additions & 18 deletions mdw-common/src/com/centurylink/mdw/translator/JsonTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.Constructor;
import java.util.Iterator;

import com.centurylink.mdw.model.JsonObject;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -30,32 +31,53 @@
*/
public interface JsonTranslator {

String JSONABLE_TYPE = "_type";

JSONObject toJson(Object obj) throws TranslationException;

Object fromJson(JSONObject json) throws TranslationException;
Object fromJson(JSONObject json, String type) throws TranslationException;

Package getPackage();

String JSONABLE_TYPE = "_type";
default Object createJsonable(JSONObject json) throws Exception {
String type = json.getString(JSONABLE_TYPE);
/**
* For compatibility
* @param json
* @param type
* @return
* @throws Exception
*/
default Object createJsonable(JSONObject json, String type) throws Exception {
JSONObject jsonObject = json;
if (json.has(JSONABLE_TYPE)) {
// serialized the old way, with embedded _type property
type = json.getString(JSONABLE_TYPE);
Iterator<?> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next().toString();
if (!JSONABLE_TYPE.equals(key)) {
jsonObject = json.optJSONObject(key);
if (jsonObject != null)
break;
}
}
if (jsonObject == null)
throw new JSONException("Object not found for " + JSONABLE_TYPE + ": " + type);
}
Class<? extends Jsonable> clazz;
Package pkg = getPackage();
if (pkg == null)
if (pkg == null) {
pkg = PackageCache.getMdwBasePackage();
clazz = pkg.getClassLoader().loadClass(type).asSubclass(Jsonable.class);
Constructor<? extends Jsonable> ctor = clazz.getConstructor(JSONObject.class);
Iterator<?> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next().toString();
if (!JSONABLE_TYPE.equals(key)) {
JSONObject objectJson = json.optJSONObject(key);
if (objectJson != null) {
com.centurylink.mdw.model.Jsonable jsonable = ctor.newInstance(objectJson);
return jsonable;
}
}
}
throw new JSONException("Object not found for " + JSONABLE_TYPE + ": " + clazz);

if (JSONObject.class.getName().equals(type) || JsonObject.class.getName().equals(type)) {
return json;
}
else {
// dynamically typed Jsonable implementation
clazz = pkg.getClassLoader().loadClass(type).asSubclass(Jsonable.class);
Constructor<? extends Jsonable> ctor = clazz.getConstructor(JSONObject.class);
Jsonable jsonable = ctor.newInstance(jsonObject);
return jsonable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ default JSONObject toJson(Object obj) throws TranslationException {
}

@Override
default Object fromJson(JSONObject json) throws TranslationException {
default Object fromJson(JSONObject json, String type) throws TranslationException {
try {
if (json.has(JSONABLE_TYPE)) {
return createJsonable(json);
}
else {
String xml = org.json.XML.toString(json);
return fromDomNode(DomHelper.toDomNode(xml));
}
return createJsonable(json, type);
}
catch (Exception ex) {
throw new TranslationException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import javax.xml.bind.JAXBElement;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,69 @@ void actionActivity(Long activityInstanceId, String action, String completionCod
ActivityInstance getActivity(Long instanceId) throws ServiceException;
ActivityLog getActivityLog(Long instanceId) throws ServiceException;

/**
* Launch a process asynchronously
*/
Long startProcess(String name, String masterRequestId, String ownerType,
Long ownerId, Map<String,Object> values) throws ServiceException;
Long startProcess(Process process, String masterRequestId, String ownerType,
Long ownerId, Map<String,Object> values) throws ServiceException;

/**
* @deprecated use {@link #startProcess(String, String, String, Long, Map)}
*/
@Deprecated
Long launchProcess(String name, String masterRequestId, String ownerType,
Long ownerId, Map<String, Object> params) throws ServiceException;
Long ownerId, Map<String,Object> values) throws ServiceException;

/**
* @deprecated use {@link #startProcess(String, String, String, Long, Map)}
*/
@Deprecated
Long launchProcess(Process process, String masterRequestId, String ownerType,
Long ownerId, Map<String, String> params) throws ServiceException;
Long ownerId, Map<String,String> values) throws ServiceException;

Response invokeServiceProcess(String processName, Object masterRequest, String masterRequestId,
Map<String, Object> parameters, Map<String, String> headers) throws ServiceException;

Response invokeServiceProcess(Process process, String masterRequestId, String ownerType,
Long ownerId, Map<String, String> params, Map<String, String> headers) throws ServiceException;
/**
* Invoke a service process synchronously.
*/
Response invokeProcess(String name, Object masterRequest, String masterRequestId,
Map<String,Object> values, Map<String,String> headers) throws ServiceException;

/**
* responseHeaders will be populated from process variable, if any
* Invoke a service process synchronously.
* responseHeaders will be populated from process value, if any
*/
Response invokeProcess(String name, Object masterRequest, String masterRequestId,
Map<String,Object> values, Map<String,String> headers, Map<String,String> responseHeaders) throws ServiceException;

/**
* Invoke a service process synchronously.
*/
Response invokeProcess(String name, String masterRequestId, String ownerType,
Long ownerId, Map<String,Object> values, Map<String,String> headers) throws ServiceException;
Response invokeProcess(Process process, String masterRequestId, String ownerType,
Long ownerId, Map<String,Object> values, Map<String,String> headers) throws ServiceException;

/**
* @deprecated use {@link #invokeProcess(String, Object, String, Map, Map, Map)}
*/
@Deprecated
Response invokeServiceProcess(String processName, Object masterRequest, String masterRequestId,
Map<String, Object> parameters, Map<String,String> headers, Map<String,String> responseHeaders) throws ServiceException;
Map<String,Object> values, Map<String,String> headers) throws ServiceException;
/**
* @deprecated use {@link #invokeProcess(String, Object, String, Map, Map, Map)}
*/
@Deprecated
Response invokeServiceProcess(String processName, Object masterRequest, String masterRequestId,
Map<String,Object> values, Map<String,String> headers, Map<String,String> responseHeaders) throws ServiceException;

/**
* @deprecated user {@link #invokeProcess(Process, String, String, Long, Map, Map)}
*/
@Deprecated
Response invokeServiceProcess(Process process, String masterRequestId, String ownerType,
Long ownerId, Map<String,String> params) throws ServiceException;
Long ownerId, Map<String,String> values, Map<String,String> headers) throws ServiceException;

Integer notify(String event, String message, int delay) throws ServiceException;

Expand Down

0 comments on commit 3b26dbe

Please sign in to comment.