import java.util.List; import java.util.LinkedList; import java.util.Date; import java.lang.Long; import java.text.SimpleDateFormat; import java.util.*; import java.text.*; import groovy.json.JsonSlurper dest = [:]; // Safely evaluates a Gpath expression. If the expression cannot be evaluated it returns null Object evalGpathOrNull(String gPathExpression) { try { return Eval.me("payload", payload, gPathExpression); } catch (Exception e) { println ('error: evalGpathOrNull:' + gPathExpression + ' - ' + e.getMessage()); return null; } } // Safely evaluates a Gpath expression. // If the first expression is null or throws an error, it evaluates the second one // If neither expression can be evaluated it returns null Object evalGpathsOrNull(String gPathExpression, String gPathAlternative) { Object result = ""; try { result = Eval.me("payload", payload, gPathExpression); } catch (Exception e) { result = null; } if (result != null) return result; try { result = Eval.me("payload", payload, gPathAlternative); } catch (Exception e) { result = null; } return result; } String evalNumberToDecimal(String stringNumber) { NumberFormat format = NumberFormat.getIntegerInstance(Locale.US); String parsed = 0; try { parsed = format.parse(stringNumber); } catch (Exception ex) { System.out.println (ex.Message) } //println ("Converted ${stringNumber} to ${parsed}"); return parsed; } // Removes all null values from a Map Map removeNulls(Map<String, Object> map) { myMap = map.findAll { it.value != null } } // Map a value into a field in the destination map if it isn't null // Also handle specific field transform logic void map(destination, fieldName, value, replace) { //println ("Pre Map - ${fieldName} : ${value}"); if (value != null && value != '') { switch (fieldName.toLowerCase()) { case 'hiredate'.toLowerCase(): fromFormat = new SimpleDateFormat("MM/dd/yyyy"); fromFormat.setLenient(false); toFormat = new SimpleDateFormat("yyyy-MM-dd"); toFormat.setLenient(false); Date date = fromFormat.parse(value); value = toFormat.format(date); break; case 'frequency'.toLowerCase(): if (value.startsWith('Annual')) value = 'Annual'; break case 'payPlanName'.toLowerCase(): if (value.startsWith('Annual')) value = 'Salary_Plan'; break case 'region'.toLowerCase(): // If Invalid Country_Region_ID replace with default println ("region: ${value} ${value.length()}/ replace: ${replace}"); if (value.length() < 5) value = replace; break; case 'salary.type': if (value.startsWith('Annual')) value = 'Salary_Plan'; if (value.startsWith('Hourly')) value = 'Hourly_Plan'; break; case 'salary.amt': case 'allowance.car.amt': case 'sales.plan.amt': case 'ote.amt': //Convert bad number string to a decimal value = evalNumberToDecimal(value); break; case 'startdate': long startDate = Long.valueOf(value); Date date = new Date(); date.setTime(dateString); println "startDate:" + date; break; default: //println ("${fieldName} : ${value}"); break; } } else { //When value is null or empty string / """ switch (fieldName.toLowerCase()) { case 'country'.toLowerCase(): if (value.length() == 0) value = replace; break; case 'hiredate'.toLowerCase(): format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 7); value = format.format(cal.getTime()); break; default: //if value is null or empty string and not one of the above fields, then replace with the default. value = replace; //println (fieldName + ':' + value + ' (default)'); break; } } destination[fieldName] = value; } // Example Response Data def restResponse = '[{"uid":10512213, "name":"Bob"},{"uid":7208201, "name":"John"},{"uid":10570, "name":"Jim"},{"uid":1799657, "name":"Sally"}]' // Parse the response dest = new JsonSlurper().parseText( restResponse ) // ---- Fields mapping //map (dest, 'address', evalGpathOrNull("payload.address"),"Address"); dest = dest.sort { it.key.toLowerCase() }; println ('PreWorkday JSON---------------'); println (new groovy.json.JsonBuilder(dest).toPrettyString()); println ('jobvite2workday-newhire-mapping.groovy completed');
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 |