import org.slf4j.Logger import org.slf4j.LoggerFactory import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.ArrayNode import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.ObjectNode Logger log = LoggerFactory.getLogger(getClass()) // Get current milestoneDay def milestoneDay = execution.getVariable("milestoneDay") def dayValue = milestoneDay.getData() def currentMilestoneDay = dayValue.day.asInt() println "currentMilestoneDay====================== $currentMilestoneDay" println "currentMilestoneDay Class====================== ${currentMilestoneDay.getClass()}" // Get the response ArrayList from the execution variable def restMilestoneObj = execution.getVariable("milestoneObj") def restNotificationObj = execution.getVariable("notificationObj") // Convert the ArrayList to a Groovy object using ObjectMapper def mapper = new ObjectMapper() ArrayNode milestoneArrayNode = mapper.convertValue(restMilestoneObj, ArrayNode) ArrayNode notificationArrayNode = mapper.convertValue(restNotificationObj, ArrayNode) // Access the "milestones" array from the first element of the milestoneArrayNode JsonNode milestonesNode = milestoneArrayNode.get(0).get("data").get("milestones") JsonNode notificationsNode = notificationArrayNode.get(0).get("data").get("notifications") // Now you have milestoneArrayNode and notificationArrayNode as ArrayNode, and milestonesNode and notificationsNode as JsonNode. log.info("MILESTONE ArrayNode: {}", milestoneArrayNode) log.info("NOTIFICATION ArrayNode: {}", notificationArrayNode) log.info("MILESTONES JsonNode: {}", milestonesNode) log.info("NOTIFICATIONS JsonNode: {}", notificationsNode) // Create a List<ObjectNode> to store the milestones with the "relativeDays" field List<ObjectNode> milestoneList = new ArrayList<>(); // Iterate through each milestone in the milestonesNode milestonesNode.forEach { milestone -> // Check if the milestone has STATUS=1 and TYPE="ABSOLUTE" if (milestone.get("STATUS").asInt() == 1 && milestone.get("TYPE").asText() == "ABSOLUTE") { // Clone the milestone as ObjectNode to add the "relativeDays" field ObjectNode milestoneWithRelativeDays = milestone.deepCopy() // Get the "STARTED_ON" value from the milestone and calculate the "relativeDays" def duration = milestone.get("STARTED_ON").asInt() def startedOn = milestone.get("DURATION").asInt() def daysExtension = milestone.get("DAYS_EXTENSION").asInt() def relativeDays = duration+ daysExtension + startedOn log.info("relativeDay** : {}", relativeDays) if (relativeDays > currentMilestoneDay) { // Get the "ID" value from the milestone def milestoneId = milestone.get("ID").asInt() // Iterate over the notificationsNode to find notifications with matching MILESTONE_ID and NOTIFY_BEFORE_DAYS List<JsonNode> matchingNotifications = new ArrayList<>(); notificationsNode.forEach { notification -> if (notification.get("MILESTONE_ID").asInt() == milestoneId) { def notifyBeforeDays = notification.get("NOTIFY_BEFORE_DAYS").asInt() def notiDay = relativeDays - currentMilestoneDay log.info("notiDay : {}", relativeDays - currentMilestoneDay) log.info("notifyBeforeDays : {}",notifyBeforeDays) if (relativeDays - currentMilestoneDay == notifyBeforeDays) { matchingNotifications.add(notification) } } } // Print the notifications for the milestone that matches the condition log.info("Notifications for Milestone ID {}: {}", milestoneId, matchingNotifications) } // Add the "relativeDays" field to the milestone object milestoneWithRelativeDays.put("relativeDays", relativeDays) // Add the milestone with "relativeDays" to the milestoneList milestoneList.add(milestoneWithRelativeDays) } } else if (milestone.get("STATUS").asText() == "ACTIVE" && milestone.get("TYPE").asText() == "RELATIVE") { // Clone the milestone as ObjectNode to add the "relativeDays" field ObjectNode milestoneWithRelativeDays = milestone.deepCopy() // Get the "DURATION" and "DAYS_EXTENSION" values from the milestone def duration = milestone.get("DURATION").asInt() def daysExtension = milestone.get("DAYS_EXTENSION").asInt() // Calculate the "relativeDays" using duration and daysExtension of the current milestone def relativeDays = duration + daysExtension // Get the "PREDECESSOR" value from the milestone def predecessor = milestone.get("PREDECESSOR").asText() if (predecessor != null && !predecessor.isEmpty()) { // Split the predecessor names and iterate through them String[] predecessorNames = predecessor.split(",") for (String pred : predecessorNames) { String predecessorName = pred.trim() // Find the predecessor milestone in the milestonesNode JsonNode predecessorMilestone = milestonesNode.find { node -> node.get("MILESTONE_NAME").asText() == predecessorName && node.get("STATUS").asText() == "INACTIVE" } if (predecessorMilestone != null) { // Get the "DURATION" and "DAYS_EXTENSION" values from the predecessor milestone int prevDuration = predecessorMilestone.get("DURATION").asInt() int prevDaysExtension = predecessorMilestone.get("DAYS_EXTENSION").asInt() // Add predecessor's duration and daysExtension to the "relativeDays" relativeDays += prevDuration + prevDaysExtension } } } // Add the "relativeDays" field to the milestone object milestoneWithRelativeDays.put("relativeDays", relativeDays) // Add the milestone with "relativeDays" to the milestoneList milestoneList.add(milestoneWithRelativeDays) } } } // Print the milestoneList with "relativeDays" field log.info("MILESTONE List with relativeDays: {}", milestoneList)
Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.
OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.
def name = System.in.newReader().readLine()
println "Hello " + name
Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.
Data type | Description | Range |
---|---|---|
String | To represent text literals | NA |
char | To represent single character literal | NA |
int | To represent whole numbers | -2,147,483,648 to 2,147,483,647 |
short | To represent short numbers | -32,768 to 32,767 |
long | To represent long numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
double | To represent 64 bit floating point numbers | 4.94065645841246544e-324d to 1.79769313486231570e+308d |
float | To represent 32 bit floating point numbers | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
byte | To represent byte value | -128 to 127 |
boolean | To represent boolean values either true or false | True or False |
You can define variables in two ways
data-type variable-name;
[or]
def variable-name;
0.upto(n) {println "$it"}
or
n.times{println "$it"}
where n is the number of loops and 0 specifies the starting index
When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
List allows you to store ordered collection of data values.
def mylist = [1,2,3,4,5];
List Methods | Description |
---|---|
size() | To find size of elements |
sort() | To sort the elements |
add() | To append new value at the end |
contains() | Returns true if this List contains requested value. |
get() | Returns the element of the list at the definite position |
pop() | To remove the last item from the List |
isEmpty() | Returns true if List contains no elements |
minus() | This allows you to exclude few specified elements from the elements of the original |
plus() | This allows you to add few specified elements to the elements of the original |
remove() | To remove the element present at the specific position |
reverse() | To reverse the elements of the original List and creates new list |