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,
Additionally, ADF Business Components provides a limited set of builtin keywords that can be used in Groovy expressions.
in addition to that, doc as detailed info http://download.oracle.com/docs/cd/E15523_01/web.1111/b31974/bcintro.htm#CEGJJJBA
adf.userSession - returns a reference to the ADF Business Components user session
Entity object attributes: reference any methods defined by the base implementation class as specified by the JavaDoc for EntityImpl, and you can reference the attributes of the entity instance.
- Example1 : http://blogs.oracle.com/raghuyadav/2010/06/sequnce_number_generation.html
Entity object script validation rules: The context is the validator object (JboValidatorContext) merged with the entity on which the validator is applied.
- 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 :
works with oldValue/newValue/source fields.
Bind variable in view objects: You can reference the structureDef property to access other information as well as the viewObject property to access the view object in which the bind variable participates. However, access to view object attributes is not supported.
- 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
Bind variable in view accessors: The view accessor can derive Groovy-driven values from the current view row in the view accessors view object used to formulate the list of valid choices.
Example1:
Transient attributes:
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
You can use the following built-in aggregate functions on Oracle Business Components RowSet objects:
-
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)")
Only VO
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())