Spin: add Jackson JSON array to task variables and evaluate it in Expressions

@miwoe

Wanted to get back to this question - I needed to begin using the SPIN type for a web-UI requirement (embedded forms).

I hope this snippet helps explain - problem was that Camunda needs to know that you’re creating a process variable of type (from a Java API perspective) SpinJsonNode. There are various tools to help with this. But, I leaned towards the simple approach in the following code otherwise… if I simply appended a spin object cloned from Jackson JsonNode, the spin library created very complex, though nicely detailed, JSON objects.

Sample JAX-RS POST JSON payload:

{
    "caseID": "case_simple_01_cid",
    "claim": "admin",
    "processVariables": [
        {
            "name": "hello",
            "value": "greetings BPM"
        }, {
            "name": "myName",
            "value": "Joe Smith"
        }, {
            "name": "spinCustomer",
            "value": {
                "firstname": "Bob",
                "lastname": "Smith",
                "customerrank" : "highvalue"
            }
        }
    ]
}

Java Example:

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Path("casebasicstartspin")
  public JsonNode caseBasicStartSpin(JsonNode postpayload)  throws Exception {
    
    // Testing out a new approach to receiving and returning
    // Jackson JsonNode.
    
    LOGGER.info("********************************");
    LOGGER.info("*** caseBasicStartSpin - invoked");    
    LOGGER.info("********************************");   
    
    // get case ID
    // NOTE: Using the "General -> Case Id" field value from the CMMN case model
    String caseID = postpayload.findValue("caseID").asText();
    
    LOGGER.info("*** caseBasicStart - caseID: " + caseID);  
    
    // get the case/process variables
    final JsonNode arrNode = postpayload.get("processVariables");
    Map<String, Object> variables = new HashMap<String, Object>();

    ObjectMapper mapper = new ObjectMapper();
    for (final JsonNode jsonNode : arrNode) {
      // check for variable with children - which we'll assume are of type JSON
      LOGGER.info("*** jsonNode - name: " + jsonNode.get("name").asText() + " , size: " + jsonNode.get("value").size());
      if (jsonNode.get("value").size() > 0) {
        variables.put(jsonNode.get("name").asText(), Spin.JSON(mapper.writer().writeValueAsString(jsonNode.get("value"))));
      } else {
        variables.put(jsonNode.get("name").asText(), jsonNode.get("value").asText());
      }
    }

Log Output:
spinCustomer was created in the above for loop, via testing the JsonNode for children (implies this is a Json type)
spinJsonCustomerManualAdd was my baseline test - verifying manual process variable creation

07:37:40,778 INFO  [com.talkdata.service.Case] (default task-5) *** caseBasicStart - caseID: case_simple_01_cid
07:37:40,778 INFO  [com.talkdata.service.Case] (default task-5) *** jsonNode - name: hello , size: 0
07:37:40,778 INFO  [com.talkdata.service.Case] (default task-5) *** jsonNode - name: myName , size: 0
07:37:40,778 INFO  [com.talkdata.service.Case] (default task-5) *** jsonNode - name: spinCustomer , size: 3
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *** caseTest
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *******************
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *** CaseDefinitionId ID: case_simple_01_cid:1:0cc51e8a-1d0b-11e7-b634-000c296e2b3e
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *** CaseInstanceId ID: 522d34b6-1d21-11e7-90fd-000c296e2b3e
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *** ActivityId ID: case_simple_01_id
07:37:40,807 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *** Process Variables: 
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) myName : Joe Smith
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) hello : greetings BPM
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) spinJsonCustomerManualAdd : {"customer":"Kermit"}
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) spinCustomer : {"firstname":"Bob","lastname":"Smith","customerrank":"highvalue"}
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) >> Date Stamp: Sun Apr 09 07:37:40 CDT 2017
07:37:40,808 INFO  [com.talkdata.task.PrintProcessVars] (default task-5) *******************


1 Like