4.2. Storing Values Between Scripts

When a script terminates its scope is cleared and any variables are lost. Sometimes it is helpful to store values which can then be retrieved at a later point, for example, by a different script. ReportServer provides script developers with two mechanisms to easily store and retrieve values at a later point. The script registry is kept in memory by ReportServer at all times and, thus, allows for very fast access. However, when ReportServer is restarted the registry is cleared. To store values persistently, ReportServer offers the so called PropertiesService. Values stored via this service will be pushed to the database.

The registry is available via the GLOBALS object. Consider the following script registryCnt.groovy.

def registry = GLOBALS.services['registry']
def cnt = registry.containsKey("myCnt") ? registry.get("myCnt") : 0

cnt++;
registry.put("myCnt", cnt)

"The count is at: " + cnt

Executing this script multiple times will yield the following output

reportserver$ exec registryCnt.groovy
The count is at: 1
reportserver$ exec registryCnt.groovy
The count is at: 2
reportserver$ exec registryCnt.groovy
The count is at: 3

The registry implements java.util.Map<String,Object>. You can find detailed documentation on the various methods at http://docs.oracle.com/javase/7/docs/api/java/util/Map.html.

Note you can list, add and modify all entries in the registry with the registry terminal command. You can find more information on the Admin Guide: https://reportserver.net/en/guides/admin/main/.

To store values persistently you need to use the PropertiesService (net.datenwerke.gf.service.properties.PropertiesService: https://reportserver.net/api/latest/javadoc/net/datenwerke/gf/service/properties/PropertiesService.html) which is a regular ReportServer service. Let us implement our same counter script as persistentCnt.groovy.

import net.datenwerke.gf.service.properties.PropertiesService

def registry = GLOBALS.getInstance(PropertiesService)
def cnt = registry.containsKey('myCnt') ? registry.get('myCnt') as int : 0

cnt++
registry.setProperty('myCnt', cnt as String)

"The count is at: $cnt"

Note the as int and as String. The PropertiesService stores all its properties in form of character strings. If we execute the script, we get the following output.

reportserver$ exec persistentCnt.groovy
The count is at: 1
reportserver$ exec persistentCnt.groovy
The count is at: 1
reportserver$ exec persistentCnt.groovy
The count is at: 1

This is not quite as expected. If we have a look at the corresponding database table RS_PROPERTY you will see that the table is still empty. Alternatively, you can use the following terminal command

desc Property "hql:from Property"

which lists all entities of type Property. The reason is simple. We did not run the exec command in commit mode and, thus, the property change was not persisted. If we run the command again, this time with the -c flag, we get the following output.

reportserver$ exec -c persistentCnt.groovy
The count is at: 1
reportserver$ exec -c persistentCnt.groovy
The count is at: 2
reportserver$ exec -c persistentCnt.groovy
The count is at: 3

We can now also inspect the property using the desc command from before, which now yields:

d		key		version		value
1		myCnt	2			3
Note you can list, add and modify all entries in the properties mapping with the properties terminal command. You can find more information on the Admin Guide: https://reportserver.net/en/guides/admin/main/.