Openbravo has dinner with Groovy

In the previous two posts (Openbravo meets Groovy and Openbravo has lunch with Groovy) we saw how to install Groovy Adapter module, we saw how you can execute Groovy code on Openbravo; we also saw how we can define a Java process, use Groovy in order to define its business logic, and become more “agile and responsive” to customer change requestes. I’d like to close this series of Groovy and Openbravo posts showing you how to use this module to write callouts and event handlers.

Ledies and gentlemen Groovy callouts!!!

Openbravo allows to developer to manage the event raised when the user fills a field and moves the focus to another field. This events may be managed through callouts; within application dictionary the developer can link a DB column to an already defined callout. A callout is a Java class that handles this kind of situations. Imagine you want to implement a callout that, when the user chooses a Product Category within the Product UI, fills the product description with a string and the actual date. In Java you can do that with this simple class:

public class JavaProductCallout extends SimpleCallout {
  protected void execute(CalloutInfo info) throws ServletException {
	Date d=new Date();
	info.addResult("inpdescription","Description updated "+d)


How I wrote in the last post, that’s standard Openbravo, but we can improve the response time to next requirements, about this feature, using Groovy. In order to reach this improvement we have to define a Java class like this:

public class SimpleGroovyScriptProductCallout extends BaseGroovyScriptCallout {
  protected String getGroovyScriptName() {
    return "GPC";

This class hasn’t business logic; it just points to a Groovy Script defined in the Grovvy Console UI and named “GPC”. Pay attention that all Groovy scripts linked to class that extends BaseGroovyScriptCallout have injected an object “info” instance of CalloutInfo. That’s why it is enough to define this Groovy script in order to implement the same business logic we defined with JavaProductCallout.

def date=new Date()
info.addResult("inpdescription","Description updated $date");

Groovy Event handlers? Why not?

“The business entity event allows you to implement business logic which reacts to specific events which are fired when entities are updated, deleted or inserted into the database” this how is defined an event handler on the Openbravo official documentation. This is the basic structure of a Java event handler:

public class ProductEventHandler extends EntityPersistenceEventObserver {
  private static Entity[] entities = { ModelProvider.getInstance().getEntity(Product.ENTITY_NAME) };
  protected Entity[] getObservedEntities() {
    return entities;

  public void onUpdate(@Observes EntityUpdateEvent event) {
    //manage update event

  public void onSave(@Observes EntityNewEvent event) {
    //manage save event

  public void onDelete(@Observes EntityDeleteEvent event) {
   //manage delete event

How you can see there are three methods that manage update, save and delete events; you have to put the business logic within these three methods; there is a method, getObservedEntities(), that returns an array of the entities whose this class wants to manage that events. In order to use Groovy language (using Groovy adapter module) it’s enough to define like this one:

public class SimpleGroovyScriptProductEH extends BaseGroovyScriptEventHandler {
  private Entity[] entities = { ModelProvider.getInstance().getEntity(Product.ENTITY_NAME) };
  protected void initChainOfResp() {
    this.respChain.put(EntityNewEvent.class, "UPDATE_SAVE_PRODUCT_HANDLER");
    this.respChain.put(EntityUpdateEvent.class, "UPDATE_SAVE_PRODUCT_HANDLER");
  protected Entity[] getObservedEntities() {
    return this.entities;

Let’s start from getObservedEntities(), it holds the same semantic; instead initChainOfResp() is a method that defines the mapping between the event and the name of the Groovy script that will be invoked when the event occurs. In this sample both New and Update events will be managed by UPDATE_SAVE_PRODUCT_HANDLER script.


This script has to be defined using the Groovy console UI.

def obj= event.targetInstance
def entity=obj.entity
event.setCurrentState(entity.getProperty("description"),"$name SimpleGroovyResourceScriptProductEH "+new Date())

It’s easy to understand what this code does; the only thing to keep in mind is that BaseGroovyScriptEventHandler class injects the object “event” and you can use it managing the linked event object.

What’s the next step?

Ok, it’s all for Openbravo and Groovy, they have met, they had lunch and at the end of the day they had dinner; I don’t know if I will add other stuff to this module. I need time to think about this thing, have you any suggestion? Have you found some bug? Please add a comment or a tweet and stay in touch!


About amicidiroberto

Curioso, onesto, polemico ed innamorato della programmazione, del mondo IT, ma soprattutto di mia moglie e delle mie figlie
This entry was posted in Groovy, Java, Openbravo. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s