Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
TAGS
Community showcase
Published in Apps & Integrations

Popular cloud apps like Scriptrunner, Draw.io, and more are now free for up to 10 users 🎉

We have exciting news for small teams! Cloud apps on the Marketplace are now able to set their 1-10 user tier to a flat rate of $0. That means, if your team is under 10 people, you can install popula...

184 views 1 12
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you