FROM THE BLOG

ADF Tips: How to groovy in ADF.

Groovy, a useful dynamic language for the JVM based on Java but adding powerful features you would often see in others scripting languages like Phyton and Ruby. Groovy is part of the ADF Framework, particullary inside the Business Components world. This post will resume some of the most useful line of code when working with ADF BC and a little bit of Groovy.

Remember that before implementing a functionality in you BC, ask yourselve if that could be done using groovy. Below, some common questions developers ask when impleneting a functionality:

How to call java methods using groovy? It well depends where is your java method declared.

– How to call java method declared in application module from an Entity Object? 

adf.object.getDBTransaction().getRootApplicationModule().yourMethodName()

You could even access a different application module by calling something like

adf.object.getDBTransaction().getRootApplicationModule().findApplicationModule(“yourAppModule”).yourMethodName()

– How to call java method declared in application module from a View Object?

adf.object.applicationModule.yourMethodName()

– How to call java method declared in ViewObjectimpl class?

adf.object.viewObject.yourMethodName()

– How to get max, min, avg, sum or count of an attribute in a ViewObject? Using Aggregate Functions; Groovy will become really handly when you need to calculate some additional information based on an attribute of your ViewObject. You can SUM, COUNT, AVG and get MAX, MIN of one particular attribute. It can be used in the current ViewObject or in its default, a ViewObject accessor using the following syntax to retireve the sum:

<ViewAccessorName>.SUM(“AttributeName”) (for view accessors)

– How to get next DB sequence number using groovy? Just for Entity Level Attribute.

new oracle.jbo.server.SequenceImpl(“YOUR_SEQ”, object.getDBTransaction()).getSequenceNumber()

– How to get a ViewAccessor row by key and get an attribute of it?

oracle.jbo.Key key = new oracle.jbo.Key(“yourKey”); return yourViewAccessor.findByKey(key,1)[0].getAttribute(“yourAttribute”)

– How to use logger in groovy?

Yes! That is also possible. https://blogs.oracle.com/jdevotnharvest/entry/using_adf_logger_with_groovy

– How to get current logged in userName?

adf.context.securityContext.userName

– How to get current date?

adf.currentDate

– How to get current stamp time?

adf.currentDateTime

– At entity level custom validation, how to access the EntityObject methods and properties?

using source keyword. E.g source.yourPublicMethodInEntityImpl()

– How to raise an error in Entity Validation?

adf.error.raise(“RESOURCE_BUNDLE_ERROR”)

– Other keywords in Entity Validation Level?

newValue, oldValue

– How to get the ADF Context?

adf.context

– How to get user session?

adf.userSession

Finally, please refer to the following document which will explain at glance all details about groovy in ADF 11g.

Introduction to Groovy 11g

4 comments

  1. Shahab Etminan

    on

    Reply

    Thank you

  2. john

    on

    Reply

    This is an awesome feature while using ADF

  3. Anil

    on

    Reply

    You could also add the below:
    How to fetch current Database date?
    adf.currentDBDate or adf.object.viewObject.getCurrentDatabaseDate();

    • Oralution

      on

      Reply

      Thanks Anil, this is useful.

Leave a comment

Your email address will not be published.

Social Media

Stay up-to-date with our latest ADF and related technical posts via your favourite social network services.