Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

...

Info

This page is describes our to use all Java APIs for Table Grid Next Generation

...

Table of Contents
maxLevel4

Info

Alternatively, check the javadoc for JAVA API.

JAVA API Examples

readRows

Code Block
languagejava
themeEclipse
firstline1
titlereadRows method
linenumberstrue
Read data of the grid.
@param issueId - id of the issue where rows should be added
@param customFieldId - id of the grid custom field where data should be read from
@param user - Jira user which will be used to control permissions
@param columnIds - set of columns' ids to filter columns for the returned result
@return object of Grid meta data and rows
GridFieldData readFieldData(Long issueId, CustomFieldId customFieldId, ApplicationUser user, Set<String> columnIds);

...

languagejava
themeEclipse
firstline1
titlereadRows usage example
linenumberstrue
collapsetrue

...

Server / Datacenter. You can use an app such as ScriptRunner for Jira to be able to run Groovy / Java scripts directly inside Jira Software Server / Datacenter.

Info

Check the Javadoc for Table Grid Next Generation Server / Datacenter for more detail documentation.

(dead link, we are working on it)

Contents

Table of Contents
maxLevel4

Java API Examples

Expand
titlereadFieldData usage example

readFieldData usage example

The example script reads all data from a Grid custom field with default configuration called API Sample in issue TEST-1.

Code Block
languagegroovy
// This script reads all data from a Grid custom field with default
// configuration named "API Sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor

...


import com.atlassian.jira.issue.CustomFieldManager

...


import com.atlassian.jira.issue.Issue

...


import com.atlassian.jira.issue.IssueManager

...


import com.atlassian.jira.issue.fields.CustomField

...


import com.atlassian.plugin.PluginAccessor

...


import com.atlassian.jira.user.ApplicationUser

...


...


...

// 

...

Get 

...

issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)

...


Issue issue = issueManager.getIssueObject("TEST-

...

1")

...


Long issueId = issue.getId

...

()

...



// 

...

Get 

...

TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)

...


CustomField 

...

tgngCustomField = customFieldManager.getCustomFieldObjectsByName("

...

API 

...

Sample").get(0)

...


Long 

...

tgngCustomFieldId = 

...

tgngCustomField.getIdAsLong()

...



ApplicationUser 

...

user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

...



// 

...

Get 

...

GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()

...


Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")

...


def 

...

gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

...



...

// 

...

Read 

...

field 

...

data

...

def 

...

fieldData = 

...

gridService.readFieldData(issueId, 

...

tgngCustomFieldId, 

...

user, null)

...


def 

...

gridRows 

...

= fieldData.getRows(

...

)

...


StringBuilder result = new 

...

StringBuilder()
for (row in gridRows) {
    def 

...

columns = 

...

row.

...

getColumns()
 

...

   

...

result.append("Order: ${columns.get("jseq")}, ")
    result.append("Summary: ${columns.get("jsummary")}, ")
    def 

...

assignee = 

...

columns.

...

get("jassignee")

...


    

...

result.append("Assignee: ${assignee == null ? "null" : assignee.get("username")}, ")
    

...

result.

...

append("Status: ${columns.

...

get("jstatus")};<br>")
}

return result

...

addRows

Code Block
languagejava
themeEclipse
firstline1
titleaddRows method
linenumberstrue
Add rows to the grid.
@param issueId - id of the issue where rows should be added
@param customFieldId - id of the grid custom field where data should be added
@param rows - list of maps with columns' ids and values
@param user - Jira user which will be used to control permissions
@return list of rows' ids
List<String> addRows(Long issueId, CustomFieldId customFieldId, List<Map<String, Object>> rows, ApplicationUser user);

...

languagejava
themeEclipse
firstline1
title addRows usage example
linenumberstrue
collapsetrue

...

Expand
titleaddRows usage example

addRows usage example

The example script adds 2 rows to a Grid custom field with default configuration called API Sample in issue TEST-1.

Code Block
languagegroovy
// This script adds two rows to Grid custom field with default configuration
// named "API Sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.user.ApplicationUser

// Get issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
Issue issue = issueManager.getIssueObject("TEST-1")
Long issueId = issue.getId()

// Get TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName("API Sample").get(0)
Long tgngCustomFieldId = tgngCustomField.getIdAsLong()

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")
def gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

// Add a row
// Only "key" is required for userlist columns
gridService.addRows(issueId, tgngCustomFieldId, user, [
    ["jsummary": "Buy groceries", "jassignee": ["key": user.getKey()], "jstatus": "Open"],
    ["jsummary": "Plan vacation", "jassignee": ["key": user.getKey()], "jstatus": "In Progress"]
])

Expand
titleupdateRows usage example

updateRows usage example

The example moves all rows assigned to a current user in status In Review to status Closed in a Grid custom field with default configuration called API Sample in issue TEST-1.

Code Block
languagegroovy
// This script moves all rows assigned to a current user in status "In Review"
// to status "Closed" in a Grid custom field with default configuration named
// "API Sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor

...


import com.atlassian.jira.issue.CustomFieldManager

...


import com.atlassian.jira.issue.Issue

...


import com.atlassian.jira.issue.IssueManager

...


import com.atlassian.jira.issue.fields.CustomField

...


import com.atlassian

...

.

...

plugin.PluginAccessor

...


import com.atlassian.jira.user.ApplicationUser

...



// 

...

Get 

...

issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)

...


Issue issue = issueManager.getIssueObject("TEST-

...

1")

...


Long issueId = issue.getId

...

()

...



// 

...

Get 

...

TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)

...


CustomField 

...

tgngCustomField = customFieldManager.getCustomFieldObjectsByName("

...

API 

...

Sample").get(0)

...


Long 

...

tgngCustomFieldId = 

...

tgngCustomField.getIdAsLong()

...



ApplicationUser 

...

user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

...



// 

...

Get 

...

GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()

...


Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")

...


def 

...

gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

...



...

// 

...

Get 

...

GridRow 

...

class
Class gridRowClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridRow")

// Read field data
def fieldData = gridService.readFieldData(issueId, 

...

tgngCustomFieldId, 

...

user, null)

...


...

def 

...

gridRows 

...

= fieldData.getRows()

// 

...

Update rows

...

def updates = new 

...

ArrayList()

...


for 

...

(row in gridRows) {
    def columns = row.getColumns()
    def assignee = columns.get("

...

jassignee")

...


    if (assignee != null && assignee.get("key") == user.getKey() && columns.get("jstatus") == "In Review") {
        def update = gridRowClass.newInstance()
     

...

 

...

 

...

 

...

update.

...

setRowId(

...

row.

...

getRowId()

...

)
        // 

...

We need to specify only the columns which are being updated
        update.setColumns(["jstatus": "Closed"])
        

...

updates.

...

add(update)
    }
}

...


...

updateRows

Code Block
languagejava
themeEclipse
firstline1
titleupdateRows method
linenumberstrue
Update rows of the grid.
@param issueId - id of the issue where rows should be added
@param customFieldId - id of the grid custom field where data should be added
@param changes - list object that represent changes for specific rows
@param user - Jira user which will be used to control permissions
void updateRows(Long issueId, CustomFieldId customFieldId, List<GridRow> changes, ApplicationUser user);

...

languagejava
themeEclipse
firstline1
titleupdateRows usage example
linenumberstrue
collapsetrue

...

gridService.updateRows(issueId, tgngCustomFieldId, user, updates)

Expand
titledeleteRows usage example

deleteRows usage example

The example script deletes all rows assigned to a current user in status Closed in a Grid custom field with default configuration called API Sample in issue TEST-1.

Code Block
languagegroovy
// This script deletes all rows assigned to a current user in status "Closed" in
// a Grid custom field with default configuration named "API Sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor

...


import com.atlassian.jira.issue.CustomFieldManager

...


import com.atlassian.jira.issue.Issue

...


import com.atlassian.jira.issue.IssueManager

...


import com.atlassian.jira.issue.fields.CustomField

...


import com.atlassian.plugin.PluginAccessor

...


import com.atlassian.jira.user.ApplicationUser

...



// 

...

Get issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)

...


Issue issue = issueManager.getIssueObject("TEST-

...

1")

...


Long issueId = issue.getId

...

()

...



// 

...

Get 

...

TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)

...


CustomField 

...

tgngCustomField = customFieldManager.getCustomFieldObjectsByName("

...

API 

...

Sample").get(0)

...


Long 

...

tgngCustomFieldId = 

...

tgngCustomField.getIdAsLong()

...



ApplicationUser 

...

user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

...



// 

...

Get 

...

GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()

...


Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")

...


def 

...

gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

...



...

// 

...

Read 

...

field data
def 

...

fieldData = 

...

gridService.readFieldData(issueId, 

...

tgngCustomFieldId, 

...

user, null)

...


def 

...

gridRows 

...

= fieldData.getRows(

...

)

...


// Ids of 

...

rows to delete
def toDelete = new ArrayList()
for (row in gridRows) {
    def columns = row.getColumns()

...


    def 

...

assignee = 

...

columns.

...

get("jassignee")

...


    if (assignee != null 

...

&& assignee.get("key") == user.getKey() && columns.get("jstatus") == "Closed") {
      

...

  

...

toDelete.add(row.getRowId())

...

    }
}

...

gridService.

...

deleteRows(

...

GridRows class

...

languagejava
themeEclipse
firstline1
titleGridRow class
linenumberstrue

...

issueId, tgngCustomFieldId, user, toDelete)

Expand
titlereadFieldDataInEditMode usage example

readFieldDataInEditMode usage example

The example script checks whether a sample Grid custom field contains values on transition and throws an error otherwise.

Code Block
languagegroovy
// This script checks that Grid custom field named "API Sample" contains at least one row.
// Can be used only as scripted validator of issue transition. Cannot be run from the Script Console.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.opensymphony.workflow.InvalidInputException
@WithPlugin("tge.cloud")
import com.idalko.tgng.jira.server.api.GridFieldData
import com.idalko.tgng.jira.server.api.GridService

// Get TGNG custom field id
String tgngCustomFieldName = "API Sample"
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName(tgngCustomFieldName).iterator().next()
Long tgngCustomFieldId = tgngCustomField.getIdAsLong()

// Get user
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get GridService
GridService gridService = ComponentAccessor.getOSGiComponentInstanceOfType(GridService)
 
// Read field data
GridFieldData fieldData = gridService.readFieldDataInEditMode(issue, tgngCustomFieldId, user)
 
// Check if the grid is empty
if (fieldData.rows.size() == 0) {
    // Throw an exception 

...

with 

...

the 

...

field id and an error message for TGNG field

...

 

...

 

...

 

...

GridFieldDate class 

...

languagejava
themeEclipse
firstline1
titleGridFieldData class
linenumberstrue

...

 // it will prevent transition with the noted error, user will stay on the transition screen.
    throw new InvalidInputException(tgngCustomField.id, "Add at least one record to grid to proceed")
}

Related articles

Extract Information from the Grid using the Java API