Groovy Samples (attribute validators,transient attribute value calculations,bind variable and attribute default values )

 
Groovy Samples (attribute validators,transient attribute value calculations,bind variable and attribute default values )
 

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
adf.object

1) adf.object  

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.


Entity object script validation rules: The context is the validator object (JboValidatorContext) merged with the entity on which the validator is applied. 



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.


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())

Back to OTN
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值