Active Objects in a ScriptRunner plugin

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

72 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you