Add convertObjectToJava to code executors - for converting language objects to java objects

This commit is contained in:
2023-06-20 09:06:57 -05:00
parent 0f799339d6
commit 3791c069c7
6 changed files with 277 additions and 1 deletions

View File

@ -32,19 +32,24 @@ import com.kingsrook.qqq.api.actions.QRecordApiAdapter;
import com.kingsrook.qqq.api.model.APIVersion;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaData;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaDataContainer;
import com.kingsrook.qqq.backend.core.actions.scripts.QCodeExecutor;
import com.kingsrook.qqq.backend.core.actions.scripts.QCodeExecutorAware;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
/*******************************************************************************
** Object injected into script context, for interfacing with a QQQ API.
*******************************************************************************/
public class ApiScriptUtils implements Serializable
public class ApiScriptUtils implements QCodeExecutorAware, Serializable
{
private String apiName;
private String apiVersion;
private QCodeExecutor qCodeExecutor;
/*******************************************************************************
@ -165,6 +170,7 @@ public class ApiScriptUtils implements Serializable
public Map<String, Serializable> insert(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("insert(" + tableApiName + ")");
body = processBodyToJsonString(body);
return (ApiImplementation.insert(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
@ -176,6 +182,7 @@ public class ApiScriptUtils implements Serializable
public List<Map<String, Serializable>> bulkInsert(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkInsert(" + tableApiName + ")");
body = processBodyToJsonString(body);
return (ApiImplementation.bulkInsert(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
@ -187,17 +194,61 @@ public class ApiScriptUtils implements Serializable
public void update(String tableApiName, Object primaryKey, Object body) throws QException
{
validateApiNameAndVersion("update(" + tableApiName + "," + primaryKey + ")");
body = processBodyToJsonString(body);
ApiImplementation.update(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(primaryKey), String.valueOf(body));
}
/*******************************************************************************
** Take a "body" object, which maybe defined in the script's language/run-time,
** and try to process it into a JSON String (which is what the API Implementation wants)
*******************************************************************************/
private Object processBodyToJsonString(Object body)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if the caller already supplied the object as a string, then return that string. //
// and in case it can't be parsed as json, well, let that error come out of the api implementation, and go back to the caller. //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(body instanceof String)
{
return (body);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if the input body wasn't a json string, try to convert it from a language-type object (e.g., javscript) to a java-object, //
// then make JSON out of that for the APIImplementation //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object bodyJavaObject = processInputObjectViaCodeExecutor(body);
return JsonUtils.toJson(bodyJavaObject);
}
/*******************************************************************************
** Use the QCodeExecutor (if we have one) to process an input object from the
** script's language into a (more) native java object.
** e.g., a Nashorn ScriptObjectMirror will end up as a "primitive", or a List or Map of such
*******************************************************************************/
private Object processInputObjectViaCodeExecutor(Object body)
{
if(qCodeExecutor == null || body == null)
{
return (body);
}
return (qCodeExecutor.convertObjectToJava(body));
}
/*******************************************************************************
**
*******************************************************************************/
public List<Map<String, Serializable>> bulkUpdate(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkUpdate(" + tableApiName + ")");
body = processBodyToJsonString(body);
return (ApiImplementation.bulkUpdate(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
@ -220,6 +271,7 @@ public class ApiScriptUtils implements Serializable
public List<Map<String, Serializable>> bulkDelete(String tableApiName, Object body) throws QException
{
validateApiNameAndVersion("bulkDelete(" + tableApiName + ")");
body = processBodyToJsonString(body);
return (ApiImplementation.bulkDelete(getApiInstanceMetaData(), apiVersion, tableApiName, String.valueOf(body)));
}
@ -257,4 +309,15 @@ public class ApiScriptUtils implements Serializable
}
return paramMap;
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void setQCodeExecutor(QCodeExecutor qCodeExecutor)
{
this.qCodeExecutor = qCodeExecutor;
}
}