After receiving lots of positive feedback for my previous blog, “Groovy – Manage Approvals”, it is time to get on with a bit more about Groovy combined with RestAPI resources to manage approvals.

In certain cases in the approval process, you may need to programmatically capture the current location out of the overall promotion path that is setup in your application. Suppose you have got a simple 2 steps promotional path defined as follows. From the web interface you can see nicely that approval unit West is the Current Location in this flow.

promotionpath RestAPI can help us identify the current location from Groovy. You simply need to run a Get Method on RestAPI URL. See example as follows.

HttpResponse<String> jsonResponse = operation.application.getConnection("MyConnection").get('/planningunits/”Scenario”::”Version”::"memberName"/promotionpath').asString();
println(jsonResponse.body)

This piece of code should return a JSON response body where you will have all possible information you would need to proceed.

How do you read this JSON body ? Before I answer this question, a quick reminder; it is a good idea to check jsonResponse.status in an actual implementation and make sure it returns an OK status before you proceed processing what body response is returning. Real life examples may not always return status code like 200 which is http OK message. For instance, if there was an infrastructure issue or user had an expired password or similar situations would return an http error status that you will have to handle. Here is an example to check the status and throw an error in cases where http request is not successful.

if(!(200..299).contains(jsonResponse.status)){
   throwvetoException("Error processing RestAPI: $jsonResponse.statusText")
}

Are you familiar with JsonSlurper ? Formal definition is that JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as maps, lists and primitive types like Integer, Double, Boolean and String. Good news is that you have access to JsonSlurper in groovy and you can use this helper class to read JSON body returning from our RestAPI call.

def jsonMap = (Map) new JsonSlurper().parseText(jsonResponse.body)
if(jsonMap.get("items").findAll{ it['currentLoc']==true }['name'][0]=="West"){
   println("Current Location is the West Entity.")
}

First line of the code above returns a Map object by parsing response body. Second line of the code finds out current location of the approval process and checks if it is West. Please see following example as well.

def ctx = JsonPath.parse(jsonResponse.body)
if((ctx.read('$.items[?(@.currentLoc == true)]["name"]') as List)[0] == "West") {
   println("Current Location is West Entity.")
}

First line of the code above returns a JsonContext object by parsing response body. Second line of the code finds out current location of the approval process and checks if it is West. Both of those examples above can be used to read JSON response body. However parsing a deep hierarchy with JsonSlurper will be cumbersome, it will be handy to use JsonPath that provides simple and powerful way to extract parts of a given JSON document.

Before I wrap up this blog, I came to know from my contacts that planning unit apis will need to be enhanced to work with non PBCS/EPBCS based applications such as FCCS. I hope my FCCS contacts will have this functionality very soon.

LEAVE A REPLY

Please enter your comment!
Please enter your name here