Read Grid Data in Create/Edit Mode
Read Grid Data in Edit Mode
/**
* Since TGE 1.19 only! Can be used only as scripted validator of issue transition.
* Cannot be run from the Script Console.
*/
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.plugin.PluginAccessor
import com.opensymphony.workflow.InvalidInputException
// find the TGE custom field
String tgeCustomFieldName = "TGE_TEST";
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectByName(tgeCustomFieldName);
String tgeCustomFieldId = tgeCustomField.getId();
// get user
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = jiraAuthenticationContext.getLoggedInUser();
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;
// read grid data
Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");
def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);
List<Map<String, Object>> gridDataList = new ArrayList<Map<String, Object>>();
StringBuilder result = new StringBuilder();
try {
def gridData = tgeGridDataManager.readGridDataInEditMode(issue, tgeCustomField, null, null, 0, 10, user);
gridDataList = gridData.getValues();
result.append("Grid ID=" + tgeCustomFieldId + " data in edit mode: " + gridDataList);
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data in edit mode cannot be retrieved: " + e.getMessage() + "\n");
}
// check if grid is empty
if (gridDataList.isEmpty()) {
// throw an exception with the field ID and an error message for TGE field
// it will prevent transition with the noted error, user will stay on the transition screen
invalidInputException = new InvalidInputException(tgeCustomFieldId, "Add at least one record to TGE grid to proceed")
throw invalidInputException;
}
// check if any row has an 'Open' status selected
for (int rowNumber = 0; rowNumber < gridDataList.size(); rowNumber++) {
Map<String, Object> rowData = gridDataList.get(rowNumber);
Map<String, Object> status = rowData.get(
"istatus" // name of the column from TGE configuration
);
String statusName = status.get(
"name" // pre-defined property for 'list', 'userlist', 'radio' column types
);
if ("Open" // name of the status I want to forbid
.equals(statusName)) {
// throw an exception with the field ID and an error message for TGE field
// it will prevent transition with the noted error, user will stay on the transition screen
invalidInputException = new InvalidInputException(tgeCustomFieldId, "Row #" + (rowNumber + 1) + " of TGE grid has an 'Open' status selected");
throw invalidInputException;
}
}
println(result.toString());
return result.toString();