Custom Groovy Field Mapping is similar to Custom Java Field Mapping in that they both can handle more complex logics and allow multiple fields to participate at the same time. Custom Groovy Field Mapping is easier to use because the Groovy script is embedded in the customization description XML file, and therefore will not require a separate jar file.
The syntax of Custom Groovy Field Mapping in the customization description XML file is also similar to Custom Java Field Mapping. The following is an example from Customization.xml in SampleCustomization project.
<GroovyFieldMappingTemplates>
<App1Name>Sample</App1Name>
<App2Name>P6</App2Name>
<GroovyFieldMappingTemplate>
<Description>Sample Groovy resource field mapping</Description>
<App1BusinessObjectName>Resource</App1BusinessObjectName>
<App2BusinessObjectName>Resource</App2BusinessObjectName>
<Name>SampleGroovyResourceFieldMap</Name>
<PDIBusinessObjectName>Resource</PDIBusinessObjectName>
<GroovyFieldMapping>
<Direction>GuestToPDI</Direction>
<SourceFields>EmployeeName,SampleDate</SourceFields>
<TargetFields>Name,PDISampleDate</TargetFields>
<RequireAllFields>true</RequireAllFields>
<Script>
<![CDATA[
Name = EmployeeName.toUpperCase();
if (containsField("EmployeeName")) {
{Name} = [EmployeeName].toUpperCase();
}
if (containsField("SampleDate")) {
def cal = new GregorianCalendar();
cal.setTime([SampleDate]);
cal.add(Calendar.DATE, -1);
cal.add(Calendar.HOUR, 2);
{PDISampleDate} = cal.getTime();
}
}
]]>
</Script>
</GroovyFieldMapping>
</GroovyFieldMappingTemplate>
</GroovyFieldMappingTemplates>
Within the Groovy script, use brackets to surround a source field, and curly brackets for a target field as in the following example:
{Name} = [EmployeeName].toUpperCase();
Where EmployeeName
is a field from the source object, and Name
is a field from the target object.
You can also use the containsField
method to test whether a field exists in the source object. In the above example, the script uses containsField
to test whether EmployeeName
or SampleDate
fields are there, before it executes the logic. This is important to know so as to avoid null pointer exceptions.
When the RequireAllFields
tag is set to true, the script will only be called when all source fields are present in the source object; no possibility for null pointer exception there. But when the RequireAllFields tag is set to false, the script will be executed even when some source fields are not present in the source object. In the case when a source field is not present in the source object, for a primitive type field, such as integer, long, double types, the value will be set to default value 0; for a string type field, it will be set to default value ""; for a date type field, it will be set to null.
Limitations
For security reasons, the following limitations have been enforced on Groovy capability:
Legal Notices
Copyright © 2013, 2015,
Oracle and/or its affiliates. All rights reserved.
Last Published Wednesday, March 25, 2015