Groovy in ADF

Groovy is an agile, dynamic language for the Java platform, defined as JSR 241. It has many features that were inspired by languages such as Python, Ruby, and Smalltalk, making them available to Java developers with a Java-like syntax. It interoperates seamlessly with any Java class, and can be compiled and interpreted without disturbing normal operations.
The latest release of JDeveloper provides integrated support for Groovy. You can use Groovy expressions for all sorts of declarative values, such as bind variables and attribute default values. You can use a Groovy script that returns true or false for declarative validation. You can also use Groovy expressions in error messages.
Groovy can simplify expressions and make them more concise in that it supports object access via dot-separated notation, so it supports syntax such as Empno instead of getAttribute(EMPNO).
You can find out more about Groovy at http://groovy.codehaus.org/ and http://radio.weblogs.com/0118231/2007/05/22.html#a829.

Note:Groovy is still a fairly new technology and is missing features such as code completion and debugging capabilities. This makes it difficult to use for large segments of code.

Using Groovy Syntax in ADF
The current object is passed into the script as the “this” object. So, to reference any attributes inside the current object, simply use the attribute name. For example, in an attribute-level or entity-level Script Expression validator, to refer toan attribute named “Salary,” the script may say simply referenceSalary.
There is one top-level reserved name, adf, to get to objects that the framework makes available to the Groovy script.

These objects include:
adf.context: To reference the ADFContext object
adf.object: To reference the object on which the expression is being applied
adf.error: In validation rules, to access the error handler that allows the validation expression to generate exceptions (adf.error.raise) or warnings (adf.error.warn)
adf.currentDate: To reference the current date with time truncated
adf.currentDateTime: To reference the current date and time

All the other accessible member names come from the context in which the script is applied:
• Bind Variable: The context is the variable object itself. You can reference the structureDefproperty to access other information as well as the viewObject property to access the viewobject in which the bind variables participate.

• Transient Attribute: The context is the current entity or view row. You can reference all attributes by name in the entity or view row in which it appears, as well as any public method on that entity or view row.
To access methods on the current object, you must use the adf.objectkeyword to reference the current object like this:
adf.object.yourMethodName().

The adf.objectkeyword is equivalent to the this keyword in Java. Without it, intransient expressions, the method is assumed to exist on the dynamically compiled Groovy script object itself.

• Expression Validation Rule: The context is the validator object (JboValidatorContext) merged with the entity on which the validator is applied.
You can reference keywords such as:
- newValue, in an attribute-level validator, toaccess the attribute value being set
- oldValue, in an attribute-level validator, to access the current value of the attribute being set
- source, to access the entity on which the validator is applied in order to invoke methods on it (accessing simply by using attribute values in the entity does not
require using the source keyword)

All Java methods, language constructs, and Groovy language constructs are available in the script. Some additional tips to keep in mind:
• You can use built-in aggregate functions on ADF RowSetobjects by referencing the functions sum(), count(), avg(), min(), and max(). They accept a string argument, which is interpreted as a Groovy expression that getsevaluated in the context of each row in the set as the aggregate is being computed:
rowSetAttr.sum("GroovyExpr")
such as employeesInDept.sum("Sal") or employeesInDept.sum("Sal!=0?Sal:0 + Comm!=0?Comm:0")
rowSetAttr.count("GroovyExpr")
• Use the return keyword just like in Java to return a value, unless it is a one-line expression in which case the return is assumed to be the result of the expression itself (such as Sal + Comm or Sal > 0).
• Use the ternary operator to implement functionality that is similar to SQL’s NVL() function—for example, Sal + (Comm != null ? Comm : 0).
• Do not use { }to surround the entire script. Groovy treats {as a beginning of a Closure object. (See Groovy documentation for more information about Closures.)
• Any object that implements oracle.jbo.Row, oracle.jbo.RowSet, or oracle.jbo.ExprValueSupplieris automatically wrapped at run time into a Groovy “Expando” object to extend the properties available for those objects beyond the bean properties. This enables easy reference to ADF row properties (even if no Java class
is generated) and avoids introspection for most used names.


Comments:

I think its great that you are spreading the Groovy goodness, but I take exception with your statement that "Groovy is still a fairly new technology and is missing features such as code completion and debugging capabilities. This makes it difficult to use for large segments of code". There certainly are IDEs that support the creation of enterprise Groovy projects. What would have been more accurate is stating that: "JDeveloper is missing features such as..."

Posted by David Schleis on April 16, 2014 at 07:27 PM EEST #

Hi David,
You are exactly true, JDeveloper is missing some features about Groovy. But you have no chance while you are developing ADF.

Posted by Arda Eralp on April 16, 2014 at 08:59 PM EEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About


I am a member of the Fusion Middleware Applications Consultancy team.

I focus on tips and tricks FMW applications consultancy but also have a strong interest in ADF Development & Achitecture, Oracle BPM, Oracle Identity Manager and Oracle SOA Suite.

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.


Search

Archives
« March 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today