REST GET Connector fails with BDM

1
0
-1

a REST GET is invoked that returns
[{"id":1,"name":"Item1","isComplete":false},{"id":2,"name":"Item2","isComplete":true},{"id":3,"name":"Item3","isComplete":true}]

The BDM setup to consume this is defined as
ToDoItem
Id: Integer
Name: String
IsComplete: Boolean

A pool variable (varToDoItems) is created based on this, and is set to be a List (ie checkbox on Is Multiple)

I then mapped varToDoItems (left hand operand) to "takes value of" bodyAsObject (right hand operand).

The engine log then shows this error -

java.lang.ClassCastException : "java.util.LinkedHashMap cannot be cast to org.bonitasoft.engine.bdm.Entity"
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bonitasoft.engine.bdm.Entity
at org.bonitasoft.engine.operation.MergeEntityAction.execute(MergeEntityAction.java:50)
at org.bonitasoft.engine.operation.EntitiesActionsExecutor.executeAction(EntitiesActionsExecutor.java:45)
at org.bonitasoft.engine.operation.BusinessDataAssignmentStrategy.computeNewValueForLeftOperand(BusinessDataAssignmentStrategy.java:45)
at org.bonitasoft.engine.core.operation.impl.OperationServiceImpl.calculateRightOperandValue(OperationServiceImpl.java:124)

Would appreciate help~

3 answers

1
0
-1

There isn't a lot of info that I see with regards to consuming a json string - so I'm including this version using bodyAsString as well

import com.company.model.ToDoItem
import groovy.json.JsonSlurper

File outputFile = new File("C:/LocalData/Temp/BonitaTestOnRestGet.txt")
outputFile.write("Begin output\n\n")

JsonSlurper slurper = new JsonSlurper()
List result = new ArrayList()

if (bodyAsString)
{
outputFile.append(bodyAsString + "\n\n")
result = slurper.parseText(bodyAsString)
outputFile.append("result as string : " + result.toString() + "\n\n")
for (keyValuePair in result)
{
ToDoItem obj = new ToDoItem()
obj.Id = keyValuePair['id']
obj.Name = keyValuePair['name']
obj.IsComplete = keyValuePair['isComplete']
outputFile.append('obj\n'
+ obj.getId().toString()
+ obj.getName()
+ obj.isIsComplete().toString() + "\n\n")
}
}
else
{
outputFile.write("empty bodyAsString\n\n")
}
return result

1
0
-1

Didn't want to change the previous line because it contained a couple of other errors (ie key names are camelCase so I was getting nulls)

Here's the updated one using bodyAsObject

import com.company.model.ToDoItem

File outputFile = new File("C:/LocalData/Temp/BonitaTestOnRestGet.txt")
outputFile.write("Begin output\n\n")

// List result = new ArrayList()
List result = new ArrayList()

if (bodyAsObject)
{
outputFile.append(bodyAsObject.toString() + "\n\n")
for (keyValuePair in bodyAsObject)
{
ToDoItem obj = new ToDoItem()
obj.Id = keyValuePair['id']
obj.Name = keyValuePair['name']
obj.IsComplete = keyValuePair['isComplete']
outputFile.append('obj\n'
+ obj.getId().toString()
+ obj.getName()
+ obj.isIsComplete().toString() + "\n\n")
result.add(obj)
}
}
else
{
outputFile.write("empty bodyAsString\n\n")
}
return result

1
0
-1

Pierre Yves Monnet kindly provided the answers -

  1. change the declaration from def to the correct data type
  2. fix the bug of result.add(keyValuePair) to result.add(ob)

the complete script ->
import com.company.model.ToDoItem
List result = new ArrayList()

if (bodyAsObject)
{
for (keyValuePair in bodyAsObject)
{
ToDoItem obj = new ToDoItem()
obj.Id = keyValuePair['Id']
obj.Name = keyValuePair['Name']
obj.IsComplete = keyValuePair['IsComplete']
result.add(obj)
}
}

result

Notifications