11. Accessing Data using Groovy

PROBLEM

There is plenty information regarding the use of Groovy on the Groovy website and in various Groovy books. In most cases a developer using Groovy with Skyway Builder will want to interact with the data (stored in variables) that exists in the respective component. For example if a developer is using Groovy in the implementation of a Controller, she's likely going to need to access the model variables. If the developer is using Groovy in the implementation of an operation, she's likely going to need access to input, output, and operation variables.

SOLUTION

Since the Skyway modeling artifacts are being generated to standard java code, there really isn't anything special with accessing data access objectd in variables from the Invoke Groovy Step. Variables within the repsective components are implicitly accessible to the Invoke Groovy Step.

HOW IT WORKS

Here's an example of some Groovy code that's accessing operation variables. There are several variables defined in the GetFlickrContent operation of the the FlickrViewer sample project.

Figure 3.3. Accessing Variables using Groovy

Accessing Variables using Groovy

As shown in the following code that's used in the Groovy step, the service variables are natively available to the Groovy script.

Example 3.4. Accessing data using Groovy

System.out.println("inFeed: "+inFeed);
def xmlFeed = new XmlParser().parse(inFeed);1
def idx = 0;
xmlFeed.channel.item.each { item ->
idx++;
def title = item.title.text()
currentEntry = new flickrviewer.domain.Entry();2
currentEntry.id = idx;
currentEntry.flickrlink = item.link.text()
currentEntry.imagelink = item.media.'@url'.text()
System.out.println("DEBUG: "+item.'media:content'.text());
currentEntry.title = item.title.text()
currentEntry.content = item.description.text()
System.out.println("Title: "+currentEntry.title);
System.out.println("Link: "+currentEntry.imagelink);
entries.add(currentEntry);3
}

1

inFeed is an operation input variable

2

currentEntry is an operation variable (type = Entry)

3

entries is an operation variable that stores of collection of entry objects (type = Entry)

RELATED RECIPES

  1. Implementing Controller Methods using Actions and Steps

  2. Using Groovy in Actions

  3. Accessing Data using Groovy