Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I create a new property to an object?

Rafael Costa
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.
March 2, 2022

I would like add a property called "owner" to some groups. Is it possible?

2022-03-02_16-06-25.png

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
March 2, 2022

I think there are 3 choices.

I'm most familiar with OfBizPropertyUtils: that stores values in propertyentry and propertytext tables (at least for jira):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.core.ofbiz.util.OFBizPropertyUtils
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def GROUP_EDITOR_PROPERTY = 'com.acme.jira.group-editor'
def properties = OFBizPropertyUtils.getCachingPropertySet(GROUP_EDITOR_PROPERTY, 1)
def group = ComponentAccessor.groupManager.getGroup('bamboo-users')

if (properties.exists(group.name)) {
def editorsJson = properties.getText(group.name)
def editorUserNames = new JsonSlurper().parseText(editorsJson)
return editorUserNames.collect { ComponentAccessor.userManager.getUserByName(it as String) }
} else {
def editor = ComponentAccessor.userManager.getUserByName('editorName')
def editorJson = new JsonBuilder([editor]).toString()
properties.setText(group.name, editorJson)
}

 

But I think the JsonEntityPropertyManager may also do the trick but I haven't tried to use it with custom entities or properties. This would store data in entiry_property

And finally, there is the CrowdService that stores data in cwd_group_attributes

import com.atlassian.jira.component.ComponentAccessor

def GROUP_EDITOR_PROPERTY = 'com.acme.jira.group-editor'
def cs = ComponentAccessor.crowdService

def group = cs.getGroupWithAttributes('bamboo-users')

cs.setGroupAttribute(group, GROUP_EDITOR_PROPERTY, 'editorName')
//reload the group
group = cs.getGroupWithAttributes(group.name)
group.getValue(GROUP_EDITOR_PROPERTY)
Rafael Costa
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.
March 3, 2022

greaat! I can set a property with 2nd way!!!

but now how can I know all properties that I can define and how get all this properties?

PD Sheehan
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.
March 3, 2022

I forgot to include the link to the GroupWithAttributes Javadoc.

In all cases I shared, you define the properties/attributes you want to store yourself.

These are completely custom. I find the best way to explore what properties have already been defined by the system or by other plugins is to use the database.

In the case of GroupWithAttributes, in my environment, there was nothing until I added my own property. There is a similar UserWithAttribute that typically holds several attributes stored by Jira (like last failed login etc).

Here is a groovy example for listing all the attributes for a given group:

import com.atlassian.jira.component.ComponentAccessor
def
cs = ComponentAccessor.crowdService
def group = cs.getGroupWithAttributes(group.name)
group.getKeys()


In my example above, the key for the new Attribute is  'com.acme.jira.group-editor' .

You can call it whatever you want. I use com.mycompany as a prefix to ensure I don't accidentally overwrite a system or plugin attribute.

Like Rafael Costa likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events