Preparing data using Groovy

You can use Groovy scripting language to prepare data for any presentation layer screen rendering tool, including the FreeMarker templating engine and OFBiz widgets.

Getting ready

Groovy has been integrated into the OFBiz framework, so using it is as simple as creating a text file with Groovy code and then pointing one or more Screen widget actions declarations to the location of the Groovy file.

Tip

Note: a side effect of integration is that execution context information, including the HTTP/HTTPS request message and request parameters, are always available in the Groovy runtime context.

By convention, Groovy files are located in the webapp/WEB-INF/actions directory of a containing Component. For example, we could have a Groovy file named myGroovyFile.groovy located in the ~myComponent/webapp/WEB-INF/actions directory.

How to do it...

To prepare "Groovy" data, follow these steps:

  1. Create a new text file or open an existing file.
  2. Import any required Java classes.
  3. Add any Groovy statements, including calls to the entity engine to extract data.
  4. Add Groovy statements to move any prepared data to the context.
  5. Close the file and save. Groovy files have the .groovy suffix.

Tip

Note: changes to Groovy script files do not require OFBiz restarts to be effective.

How it works...

Separating web page layout and style from data is the stuff Model-View-Controller (MVC) web application design patterns are made of. The OFBiz framework enables MVC separation through a number of architectural features, including the separation of web page screen (or "view") definitions from the data that is presented by the views. Web page views and screen layouts may be built using a variety of tools, including the OFBiz Screen widget, FreeMarker templates, and/or other OFBiz widgets.

All these tools support context aware, runtime merging of screen view definitions with data gathered and manipulated using the Groovy scripting language.

There's more...

Groovy is a dynamic scripting language with syntax similar to Java and with many additional features "inspired" by languages such as Python, Ruby, and Smalltalk (http://groovy.codehouse.org). Some commonly used Groovy patterns found within the OFBiz code base are listed in the following table:

Usage

Groovy code

Import statements

import.java.util*;

import org.ofbiz.base.util.UtilMisc;

To load request parameters from a Form or URL HTTP/HTTPS request

myRequestParam=parameters.someParam;

Set request parameters into the context directly from a URL request

// Note: all request parameters are

// assumed to be Strings

// Unless otherwise converted

context.paramA+ "_X" = parameters.AparamA

context.paramB+ "_Y" = parameters.AparamB

Create a new Java List structure

Check for an empty list

newList = [];

if(newList) { // do something }

Create an empty Java Map structure (key, value pairs)

someMap = [:];

someMap.keyName=value;

"Safe Navigation Operator"

(Used to avoid NullPointerException)

Z = ObjectY.find("Q");

R = Z?.AA?.BB

// R is null if Z or ZZ.AA are null

"Elvis Operator"

context.someStringValue = someStringValue ?: "Default Value";

context.someNumber =

someNumber + x ?: 0;

"For" loops

ListOfGenericValues.each {

x ->

// do Something here

}

See also

The OFBiz installation is replete with hundreds of examples of Groovy code. The best reference is to consult the code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.175