Active Objects in a ScriptRunner plugin

profgrape August 14, 2017

Hi,

I'm digging up an oldie-but-goodie: using Active Objects in a ScriptRunner groovy class (impemlements CannedScript).  Everything I could find on the topic is in these two posts from 2012-2013:

https://community.atlassian.com/t5/Answers-Developer-Questions/Access-to-Active-Object-entity-in-my-groovy-class/qaq-p/484709

https://community.atlassian.com/t5/Answers-Developer-Questions/example-Groovy-Script-accessing-Active-Object-Tables/qaq-p/542479 

As-expected, I can't obtain an ActiveObjects instance through OSGi.  And also noted in the posts above, there are good reasons for NOT using SQL.

Before I go ahead and store my data in a separate MongoDB instance, has anyone gotten anywhere using Active Objects in a groovy class?

Thanks,

Shaun

 

1 answer

3 votes
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 10, 2017

So, ScriptRunner has come a long way since those posts! While you certainly could store your data in some external store, you should be able to use Active Objects in the context of your CannedScript.

First, I'm going to assume you've already exhausted the somewhat simpler option: using Atlassian's PluginSettings API. For a lot of scripters out there, that's going to be the more maintainable way forward. Indeed, we use that in our very own Template Comments for JIRA Service Desk plugin, as you can see in its source (particularly the CannedCommentsService).

That said, we do use ActiveObjects too.

So, with that assumption, a relatively simple way to get an instance of ActiveObjects would be like so:

import com.atlassian.activeobjects.external.ActiveObjects
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
def ao = ScriptRunnerImpl.getOsgiService(ActiveObjects)

Then you could use the ao variable to access an instance of ActiveObjects.

Another path forward would be to use the ScriptRunner class com.onresolve.scriptrunner.runner.util.AOPropertyPersister, which has some static methods for saving data.

This sample script shows how to save a simple POGO (Plain Old Groovy Object) using AOPropertyPersister.

import com.onresolve.scriptrunner.runner.util.AOPropertyPersister

String myActiveObjectsPropertyKey = "com.mycompany.something.something.darkside"

class MyConfig {
int id
String name
String data
}

def instance = new MyConfig(id: 1, name: "mine", data: "Some stuff here")
AOPropertyPersister.saveTyped(instance, myActiveObjectsPropertyKey) //This saves the data

def data = AOPropertyPersister.loadTyped(myActiveObjectsPropertyKey, MyConfig) //This loads an instance of the above class
log.debug data
assert data.name == "mine"
assert data.id == 1
assert data.data == "Some stuff here"
log.debug "Finished executing demo script of AO"

Full disclosure: both those APIs (ScriptRunnerImpl and AOPropertyPersister) are technically subject to change. We have no immediate plans to break them, but as always, make sure to test your scripts in a test environment when undergoing upgrades and so forth. That goes double for use cases where you get into persistence and other things that, as Gary Bernhardt so eloquently put it, are stateful and fail. :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events