Search code examples
javagroovymuleanypoint-studiomule-esb

Mule ESB (Groovy Script): How would I check a value and add a new key value mapping to a java.util.LinkedList


I am calling a service that returns data from a DB in the form of a LinkedList. I need to update the LinkedList with a new field called "status" which is determined off of endDate.

  • endDate > current date => status=deactivated
  • endDate <= current date => status=active

Mule Payload Class: java.util.LinkedList

Mule Payload: [{serialNumber=, maintenanceId=12345, customerID=09890, startDate=2017-10-10 23:34:17, endDate=2018-10-10 23:34:17},{serialNumber=, maintenanceId=09091, customerID=74743, startDate=2014-8-16 23:34:17, endDate=2019-8-16 23:34:17}]

The issue I am having in mule is that I am unable to navigate into the linked list to retrieve the value as well as add a new value to the list. Hoping someone could give me some advice on the best way to move forward. I am trying to use a groovy transformer to update the payload, but it's not going so well, so I don't have any code to show.

Thanks taking the time!


Solution

  • I had a similar requirement (the payload was a JSON but it should work as well) and this is what I did using Dataweave (I added your data so it can be easier to understand).

    %dw 1.0
    %output application/java
    ---
    flowVars.input2 map {
        serialNumber : $.serialNumber,
        maintenanceId: $.maintenanceId,
        customerID: $.customerID,
        startDate: $.startDate,
        endDate: $.endDate,
        status: "deactivated" when $.endDate as :date {format:"yyyy-M-dd HH:mm:ss"} > (now  as :date {format:"yyyy-M-dd HH:mm:ss"}) otherwise "activated"
    }
    

    With this transformation you iterate the list and add the status value based on your requirement.

    Input example:

     [{"serialNumber":"test1", "maintenanceId":"12345", "customerID":"09890", "startDate":"2017-10-10 23:34:17", "endDate":"2018-10-10 23:34:17"},{"serialNumber":"test2", "maintenanceId":"09091", "customerID":"74743", "startDate":"2014-8-15 23:34:17", "endDate":"2018-8-15 23:34:17"}]
    

    Output example

    [{"serialNumber":"test1","maintenanceId":"12345","customerID":"09890","startDate":"2017-10-10 23:34:17","endDate":"2018-10-10 23:34:17","status":"deactivated"},{"serialNumber":"test2","maintenanceId":"09091","customerID":"74743","startDate":"2014-8-15 23:34:17","endDate":"2018-8-15 23:34:17","status":"activated"}]
    

    Hope this helps you