In this post lets have Groovy samples.
Groovy is a scripting language for the Java platform with Java-like syntax. The Groovy language simplifies the authoring of code by employing dot-separated notation yet still support syntax to manipulate collections, Strings, and JavaBeans.
ADF Business Components supports the use of Groovy language in places where access to entity object and view object attributes is useful,
- including attribute validators (for entity objects),
- attribute default values (for either entity objects or view objects),
- transient attribute value calculations (for either entity objects or view objects),
- bind variable default values (in view object query statements and view criteria filters), and placeholders for error messages (in entity object validation rules).
Additionally, ADF Business Components provides a limited set of builtin keywords that can be used in Groovy expressions.
article by steve http://radio-weblogs.com/0118231/2007/05/22.html
in addition to that, doc as detailed info http://download.oracle.com/docs/cd/E15523_01/web.1111/b31974/bcintro.htm#CEGJJJBA
adf.context
adf.error
adf.userSession - returns a reference to the ADF Business Components user session
adf.currentDate
adf.currentDateTime
- Example 1: http://andrejusb.blogspot.com/2010/06/groovy-string-operations-in-oracle-adf.html
- Example 2:http://andrejusb.blogspot.com/2009/11/groovy-validation-hint-in-oracle-adf.html
- Example 3: using source fields :
- Example1: View Criteria : http://andrejusb.blogspot.com/2010/02/default-value-for-date-field-in-query.html
- Example2: Default Values : http://formattc.wordpress.com/2010/04/02/custom-java-bind-variable-in-a-where-clause-of-an-adf-view-object/
- Example3:http://download.oracle.com/docs/cd/E15523_01/web.1111/b31974/bcquerying.htm#CEGDGIJH
Note : On Java Based Calculation, you need to add the calculation logic in transient attribute method which gets exposed into entity implementation class ( there is no need to specify any data into Literal or Expression field in entity attribute wizard ), if calculation logic is added in your custom method ( not transient exposed method ) in entity impl class then you explicitly need to call your custom method in expression field as "adf.object.mycustomcalculationmethod()" in entity wizard.
Note : when you want to reference the method of an entity implementation class in a validation rule (> or < or etc..), you use the source prefix instead object: source.getDefaultSalaryForGrade()
Use of the source prefix is necessary in validators because the object keyword implies the validation rule object instead of the entity object (where the method is defined).
To allow you to reference members of the validator object (JboValidatorContext), you can use these keywords in your validation rule expression:
-
newValue: in an attribute-level validator, to access the attribute value being set
-
oldValue: in an attribute-level validator, to access the current value of the attribute being set
For example, you might use the following expression to specify a dynamic validation rule check of the salary for a salesman.
if (Job == "SALESMAN"){
return newValue < source.getMaxSalaryForGrade(Job)
}
else
return true
Aggregate functions with groovy
-
rowSetAttr.sum(GroovyExpr)
-
rowSetAttr.count(GroovyExpr)
-
rowSetAttr.avg(GroovyExpr)
-
rowSetAttr.min(GroovyExpr)
-
rowSetAttr.max(GroovyExpr)
For example, in a Dept entity object you could add a transient attribute that displays the sum of all employee salaries that is calculated by this expression:
EmployeesInDept.sum("Sal")Or, assume that you want the calculation of the salary total for specific departments to include each employee's benefits package, which varies with job role:
EmployeesInDept.sum("Sal + adf.object.getBenefitsValue(Job)")Create DepartmentVO
Create EmployeesVO
Create ViewLink b/w DepartmentVO and EmployeesVO selecting DepartmentId.
add expression adf.object.Employees5.getRowSet().sum("(Salary > 2000 ? Salary : 0)") on SUMVO transiant attributed created in DepartmentVO ( this sums up salary > 2000 per department ) that's it.
EO + VO
Here is some tips from frank on how to use above.
create a master/detail relation between Departments and Employees using the HR schema
- For the Departments VO, create a RowImpl class from the Java section
- create an attribute SumFromEO in the Departments EO
- in the DepartmentsVO, create a new attribute from EO pointing to SumFromEO
- Also, in the VO, create a new attribute SumFromVO
- make sure the attribue is transiesnt
- set the created attribute on the VO "SumFromVO" and "SumFromEO" to use an Expression
- Add the following Groovy expression to the SumFromEO transient attribute on the EO
adf.object.Employees1.sum("Salary");
- Add the following Groovy expression to the SumFromVO transient attribute on the VO
adf.object.EmployeesView.sum("Salary");
When you run this then both, the EO based VO attribute and the VO attribute should show the same value
groovy script code snipts
Example 1:
String acctnumber = newValue;
sumofdigits = 0;
digit = 0;
addend = 0;
timesTwo = false;
range = acctnumber.length()-1..0
range.each {i ->
digit = Integer.parseInt (acctnumber.substring (i, i + 1));
if (timesTwo) {
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sumofdigits += addend;
timesTwo = !timesTwo;
}
modulus = sumofdigits % 10;
return modulus == 0;
Example 2:
newValue <= (new java.sql.Timestamp(System.currentTimeMillis())
Back to OTN