Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Post-function script to update Insight object with values from issue

Jon Hill April 7, 2018

I have an Insight object type called Asset.

I have an Insight object type called Bin, which has an attribute of Type object and Type Value Asset that is named "Assets added to bin." 

I'm writing a new workflow in which the user selects a specific Bin from a Jira customfield and N number of Assets from another Jira customfield (both tie back to Insight).  When the issue is submitted, my goal is for the Assets selected to be added into the selected Bin's "Assets added to bin" attribute.

It took me several days of banging my head against the wall, but I've come up with the following groovy post-function script to do what I've described above. 

I'm posting here for two reasons:  First, I wish I had this kind of sample code when I was putting my own script together (some of the sample code on Riada's page was nonfunctional and none of it really explained what was going on).   So I hope someone out there is starting out with Insight post functions and finds this useful.

Second, I'd really appreciate a little code review.  What I'm doing (I've summarized the main steps in the beginning) seems a little circuitous.  Is there a way to just retrieve the Bin's "Assets added to bin"'s AttributeValueBeans list (steps 1-6 except use objAttrib instead of newObjectAttributeBean), skip step 7, add the assets discovered in the issue to the list (steps 8-10), and re-save the (newly updated) objAttrib?  I tried a couple of variants but got exception errors that didn't provide much useful information for debugging.

Anyway, here's the code.  Thanks!

 

int insightSchemaID = 5;
int jiraCustomFieldIDForAffectedObject = 11002;
int jiraCustomFieldIDForDisposalBin = 11407;
int insightAssetsAddedToBinID = 42702;


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import java.util.ArrayList;
import java.util.stream.Collectors;


/*
Step 1: retrieve the Disposal Bin reference from the issue
Step 2: find the referenced Disposal Bin in Insight
Step 3: find the Insight attribute
Step 4: prep
Step 5: find the attribute for the bin we're about to update
Step 6: retrieve list of current assets in the Insight attribute
Step 7: populate list of values to be added with what's already in Insight
Step 8: retrieve the Affected Object references from the issue
Step 9: find the Assets in Insight that are referenced in Jira and add them to the list
Step 10: dedupe the list
Step 11: add list to the new ObjectAttribute
Step 12: assign the new ObjectAttribute the original ObjectAttribute's ID
Step 13: save changes
*/


/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


/* ------------------------------------------------------------- */
/* Step 1: retrieve the Disposal Bin reference from the issue */
/* ------------------------------------------------------------- */
CustomField disposalBinField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForDisposalBin);

if (disposalBinField == null) {
log.warn("Bin field not found in issue. Custom field identifier was " + jiraCustomFieldIDForDisposalBin + ". Exiting.");
return false;
}
else {
log.info("Bin field: " + disposalBinField);
}

def disposalBinID = disposalBinField.getValue(issue);
//Although this field is defined in Jira as Insight Object (single select), it returns an array so we need to pull the first item from the value returned
if (disposalBinID.isEmpty()) {
log.warn("Bin value not found. Exiting");
return false;
}
else {
log.info("Bin ID from Jira issue: " + disposalBinID[0]);
}

 

/* ------------------------------------------------------------- */
/* Step 2: find the referenced Disposal Bin in Insight */
/* ------------------------------------------------------------- */

/* Find disposal bin object in Insight */
def objBin = objectFacade.loadObjectBean(disposalBinID[0].id);


if (objBin == null) {
log.warn("Could not find Disposal Bin in Insight. Exiting.");
return false;
}
else {
log.info("Disposal Bin in Insight: " + objBin);
}

/* ------------------------------------------------------------- */
/* Step 3: find the Insight attribute */
/* ------------------------------------------------------------- */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(insightAssetsAddedToBinID); //the ID of the object type attribute "Assets added to bin"
if (objectTypeAttributeBean == null) {
log.warn("Could not find 'Assets added to bin' field in Insight. Exiting.");
return false;
}
else
{
log.info("Insight attribute: " + objectTypeAttributeBean);
}

/* ------------------------------------------------------------------- */
/* Step 4: prep */
/* ------------------------------------------------------------------- */
//create an empty attribute that will store all the values and will ultimately replace the attribute associated with the current bin
def newObjectAttributeBean = objBin.createObjectAttributeBean(objectTypeAttributeBean);
//create a list to populate with values old and new
def values = newObjectAttributeBean.getObjectAttributeValueBeans();


/* ------------------------------------------------------------------- */
/* Step 5: find the attribute for the bin we're about to update */
/* ------------------------------------------------------------------- */
def objAttrib = objectFacade.loadObjectAttributeBean(objBin.getId(), insightAssetsAddedToBinID);

if (objAttrib == null) {
log.warn("ObjAttrib is null; will create a new one.");
objAttrib = objBin.createObjectAttributeBean(objectTypeAttributeBean);
}
else {
log.info("Insight attribute definition: " + objAttrib);
}

/* ------------------------------------------------------------------- */
/* Step 6: retrieve list of current assets in the Insight attribute */
/* ------------------------------------------------------------------- */
def existingAssetsInBin = objAttrib.getObjectAttributeValueBeans();
if (existingAssetsInBin == null)
{
log.warn ("Unable to retrieve any assets from this bin.");
}
else {
log.info("existingAssetsInBin list: " + existingAssetsInBin);
}

/* ------------------------------------------------------------------- */
/* Step 7: populate list of values to be added with what's already in Insight */
/* ------------------------------------------------------------------- */
for (def existingAsset:existingAssetsInBin) {
log.info("Existing asset found: " + existingAsset.getValue());
def objAsset = objectFacade.loadObjectBean(existingAsset.getValue());
log.info("objAsset: " + objAsset);
def assetId = objAsset.getId();
log.info("Asset ID: " + assetId);
def newObjectAttributeValueBean = newObjectAttributeBean.createObjectAttributeValueBean();
newObjectAttributeValueBean.setReferencedObjectBeanId(assetId); //add an asset to the attribute
values.add(newObjectAttributeValueBean);
log.info("Array of values size is now: " + values.size());
}
//we'll use another for loop later to add more values to the current list

log.info("values list: " + values);
for (def value:values) {
log.info("Value: " + value);
}


/* --------------------------------------------------------------- */
/* Step 8: retrieve the Affected Object references from the issue */
/* --------------------------------------------------------------- */
CustomField affectedObjectField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForAffectedObject);
if (affectedObjectField == null) {
log.warn("Affected Object field not found in issue. Custom field identifier was " + jiraCustomFieldIDForAffectedObject + ". Exiting");
return false;
}
else {
log.info("Affected Object field: " + affectedObjectField);
}

//identify the values of the individual objects found
def affectedObjects = affectedObjectField.getValue(issue);
if (affectedObjects == null) {
log.warn("User failed to enter any objects in the Disposal Bin field. Exiting");
return true;
}
else {
log.info("Affected objects value from Jira issue: " + affectedObjects);
}


/* ----------------------------------------------------------------------------------------- */
/* Step 9: find the Assets in Insight that are referenced in Jira and add them to the list */
/* ----------------------------------------------------------------------------------------- */
for (def affectedObject:affectedObjects) {
log.info("loop");
if (affectedObject != null) {
log.info("Asset object: " + affectedObject);
def assetId = affectedObject.getId();
log.info("Asset object ID: " + affectedObject.getId());

def newObjectAttributeValueBean = newObjectAttributeBean.createObjectAttributeValueBean();
newObjectAttributeValueBean.setReferencedObjectBeanId(assetId); //add an asset to the attribute
values.add(newObjectAttributeValueBean); // Add the value to the object attribute
}
else {
log.warn("Asset object is null!");
}
}
for (def value:values) {
log.info("Value: " + value);
log.info("Class: " + value.getClass());
//class for each value should be MutableObjectAttributeValueBean
}
log.info("Values to be added to newObjectAttributeBean, before dedupe: " + values);

/* -------------------------------------------------------- */
/* Step 10: dedupe the list */
/* -------------------------------------------------------- */
def dedupedValues = values.stream().distinct().collect(Collectors.toList());
log.info("Values to be added to newObjectAttributeBean, after dedupe: " + dedupedValues);
for (def value:dedupedValues) {
log.info("Deduped value: " + value);
log.info("class: " + value.getClass());
}

/* ------------------------------------------------------------------------ */
/* Step 11: add list to the new ObjectAttribute */
/* ------------------------------------------------------------------------ */
newObjectAttributeBean.setObjectAttributeValueBeans(dedupedValues);
log.info("newObjectAttributeBean: " + newObjectAttributeBean);
log.info("newObjectAttributeBean ID: " + newObjectAttributeBean.getId());
log.info("Final list of values that are being added to newObjectAttributeBean: " + newObjectAttributeBean.getObjectAttributeValueBeans());

/* --------------------------------------------------------------------------- */
/* Step 12: assign the new ObjectAttribute the original ObjectAttribute's ID */
/* --------------------------------------------------------------------------- */
def objAttribute = objectFacade.loadObjectAttributeBean(objBin.getId(), insightAssetsAddedToBinID);
if ( objAttribute != null) {
/* If attribute exists reuse the old id for the new attribute */
newObjectAttributeBean.setId( objAttribute.getId());
log.info("List of values before: " + objAttribute.getObjectAttributeValueBeans());
log.info(" objAttribute:" + objAttribute);
log.info(" objAttribute ID: " + objAttribute.getId());
}

/* ------------------------------------------------------------------------ */
/* Step 13: save changes */
/* ------------------------------------------------------------------------ */
try {
objAttribute = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
log.info("Successfully stored attribute");
}
catch (Exception vie) {
log.warn("Exception storing objAttribute: " + vie.getMessage());
return false;
}


//to do: add a comment to the Issue describing what was done.


return true;

 

3 comments

Comment

Log in or Sign up to comment
Eric Collins September 11, 2018

Excellent work - have you found a better way or made any improvements?

Jon Hill September 20, 2018

Actually, I have!  I've created a classes.groovy file that I store on the application server and have my other scripts reference it.  The new functions available for calling are:

  • GetInsightObject(key)
  • GetInsightObject(Id)
  • GetInsightValue for a given attribute of a given object 
  • SetInsightValue for a given attribute of a given object (returns boolean)
/*
HOW TO CALL THESE CLASSES
1. Start with the following block:
def PATH_TO_SCRIPTS = "/opt/atlassian/jira-data/scripts";
File insightHelperClassFile = new File(PATH_TO_SCRIPTS, "classes.groovy");
Class insightHelperClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(insightHelperClassFile);
GroovyObject insightHelperObject = (GroovyObject) insightHelperClass.newInstance();

2. To use the classes, call them as methods of insightHelperObject:
def value = insightHelperObject.GetInsightValue(log, 38049, 42526);
log.info "value: " + value;
log.info "object: " + insightHelperObject.getObject(38049);

boolean SetInsightValue (def log, int InsightObjectId, int InsightAttributeId, def NewValue)
*/


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

import java.util.ArrayList;
import java.util.stream.Collectors;


public Object GetInsightObject(int key) {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


public Object GetInsightObject(String key) {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


def GetInsightValue (def log, int InsightObjectId, int InsightAttributeId) {
log.info("GetInsightValue function has been called.");
def insightObjectAttributeValueBeans = GetInsightObject(log, InsightObjectId, InsightAttributeId);

if (insightObjectAttributeValueBeans == null) {
log.warn("Could not find values for this field. Returning null.");
return null;
}
else {
def insightValues = insightObjectAttributeValueBeans[0];
if (insightValues == null) {
log.warn "Could not find a value for this field; returning null";
}
else {
def insightValue = insightValues.getValue();
log.info("Value for this object's field: " + insightValue);
log.info("Class: " + insightValue.getClass());
return insightValue;
}
}
}





def GetInsightObject (def log, int InsightObjectId, int InsightAttributeId) {
log.info("GetInsightObject function has been called.");

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


def obj = objectFacade.loadObjectBean(InsightObjectId);
if (obj == null) {
log.warn("Cannot find the Insight object. UsingInsightObjectId: " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("Insight object is: " + obj); }

attribType = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId);
if (attribType == null) {
log.warn("Cannot find the attribute type in the object definition. Using InsightAttributeId " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("Attribute type for Insight object: " + attribType); }

def attrib = objectFacade.loadObjectAttributeBean(InsightObjectId, InsightAttributeId);
if (attrib == null) {
log.warn("Cannot find the field in Insight. Using ID " + InsightAttributeId + ". Exiting.");
return false;
}
else {
log.info("Insight attribute found: " + attrib);
}



def attribValues = attrib.getObjectAttributeValueBeans();
if (attribValues == null) {
log.warn("Could not find values for this field. Returning null.");
return null;
}
else {
log.info("Returning ObjectAttributeValueBean");
return attribValues;
}


/*

// EXAMPLES FOR USING THIS FUNCTION
objectId = 29461 //zny7jhillold
//objectId = 4815
//attributeId = 42713 //truefalse
attributeBuildDateId = 42682 //BuildDate
attributeCostId = 42647 //Cost (double)
attributePONumId = 42648 //purchase order number (integer)
attributeId = 42629 //Site (referenced object)
attributeId = 42626 //description (text)
attributeId = 42530 // Name (textshort)
attributeId = 42641 //ManagementOption1 (Select)
attributeId = 42632 //Lifecycle State (Status)



def result = GetInsightValue(log, objectId, attributeId); //boolean
return "result: " + result;
//result = GetInsightValue(log, objectId, attributeId); //date
//result = GetInsightValue(log, objectId, attributeId) //double
//result = GetInsightValue(log, objectId, attributeId); //integer
//result = GetInsightValue(log, objectId, attributeId); //referencedobject
//result = GetInsightValue(log, objectId, attributeId); //text




*/
}








boolean SetInsightValue (def log, int InsightObjectId, int InsightAttributeId, def NewValue) {
log.info("SetInsightValue function has been called.");

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


def obj = objectFacade.loadObjectBean(InsightObjectId);
if (obj == null) {
log.warn("Cannot find the Insight object. UsingInsightObjectId: " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("Insight object is: " + obj); }

log.info("Object found: " + obj.getName());

attribType = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId);
if (attribType == null) {
log.warn("Cannot find the attribute type in the object definition. Using InsightAttributeId " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("Attribute type for Insight object: " + attribType); }


newAttrib = obj.createObjectAttributeBean(attribType);
log.info("Created newAttrib:" + newAttrib);

def newAttribValue = newAttrib.createObjectAttributeValueBean();
log.info("Created newAttribValue");

log.info("Trying to set value " + NewValue);
log.info("Class of new value is " + NewValue.getClass());

log.info("----");
try {
newAttribValue.setValue(attribType, NewValue);
} catch (Exception vie) {
log.warn("Unable to set attribute " + attribType + " to " + NewValue + ". Exiting.");
log.warn(vie.getMessage());
return false;
}


log.info("About to save value");
def attribValues = newAttrib.getObjectAttributeValueBeans();
attribValues.add(newAttribValue);
newAttrib.setObjectAttributeValueBeans(attribValues);

try {
newAttrib = objectFacade.storeObjectAttributeBean(newAttrib);
} catch (Exception vie) {
log.warn("Unable to save changes. Exiting.");
log.warn(vie.getMessage());
return false;
}
log.info("Successfully saved value");
log.info("===================");

return true;

/*
EXAMPLES FOR USING THIS FUNCTION
objectId = 29461 //zny7jhillold
objectId = 4815
attributeId = 42713 //truefalse
//attributeId = 42682 //BuildDate
//attributeId = 42647 //Cost (double)
//attributeId = 42648 //purchase order number (integer)
//attributeId = 42629 //Site (referenced object)
//attributeId = 42626 //description (text)
//attributeId = 42530 // Name (textshort)
//attributeId = 42641 //ManagementOption1 (Select)
//attributeId = 42632 //Lifecycle State (Status)


boolean result = SetInsightValue(log, objectId, attributeId, true); //boolean
result = SetInsightValue(log, objectId, attributeId, Date.parse("yyyy-MM-dd", "2018-05-03")); //date
result = SetInsightValue(log, objectId, attributeId, new BigDecimal(5.1).doubleValue()) //double
result = SetInsightValue(log, objectId, attributeId, 5); //integer
result = SetInsightValue(log, objectId, attributeId, 10057); //referencedobject
result = SetInsightValue(log, objectId, attributeId, "DISPOSED"); //text
*/
}
Like Daniel Bargalló Yuste likes this
Eric Collins September 21, 2018

Incredible!!

Thank you for sharing - any other awesome stuff you would like to share? :)

Eric Collins September 21, 2018

Second question, do you have any examples of adding an attachment from a JIRA issue to a referenced object?

Jon Hill September 21, 2018

I've never needed to do this so I can't comment.  I did see this post on how to do it via REST.  https://community.atlassian.com/t5/Marketplace-Apps-questions/Upload-attachment-using-Insight-REST-API/qaq-p/663186

Eric Collins October 2, 2018

@Jon Hill, thanks for the assist. Using your revised classes, how do you go about setting multiple referenced objects? 

Jon Hill October 3, 2018

Funny you should ask.  I just updated my classes.groovy script so that the GetInsightValue and GetInsightObject functions return a list.  I also updated GetInsightValue to handle items of type Status.  I have not done anything with the Set functions, though, so be warned.

BTW, I'm assuming there's no way to add an attachment in this forum.  Apologies for how long this thread has gotten.

/*
HOW TO CALL THESE CLASSES
1. Start with the following block:
def PATH_TO_SCRIPTS = "/opt/atlassian/jira-data/scripts";
File insightHelperClassFile = new File(PATH_TO_SCRIPTS, "classes.groovy");
Class insightHelperClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(insightHelperClassFile);
GroovyObject insightHelperObject = (GroovyObject) insightHelperClass.newInstance();

2. To use the classes, call them as methods of insightHelperObject:
def value = insightHelperObject.GetInsightValue(log, 38049, 42526);
for (def val: value) {log.info "Value: " + val;}

boolean SetInsightValue (def log, int InsightObjectId, int InsightAttributeId, def NewValue)
*/


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

import java.util.ArrayList;
import java.util.stream.Collectors;


public Object GetInsightObject(int key) {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


public Object GetInsightObject(String key) {
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().
findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
return objectFacade.loadObjectBean(key);
}


def GetInsightValue (def log, int InsightObjectId, int InsightAttributeId) {
/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);

/* Get Insight Configuration Facade from plugin accessor */
Class configureFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ConfigureFacade");
def configureFacade = ComponentAccessor.getOSGiComponentInstanceOfType(configureFacadeClass);



log.info("GetInsightValue: GetInsightValue function has been called.");


def attribType = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId).getType().name();
def insightObjectAttributeValueBeans = GetInsightObject(log, InsightObjectId, InsightAttributeId);

log.info ("GetInsightValue: insightObjectAttributeValueBeans: " + insightObjectAttributeValueBeans);
if (insightObjectAttributeValueBeans == null) {
log.info ("GetInsightValue: Null value received. Returning null.");
return null;
}

insightObjectAttributeValueBeansCount = insightObjectAttributeValueBeans.size();
log.info ("GetInsightValue: insightObjectAttributeValueBeans count: " + insightObjectAttributeValueBeansCount);
ArrayList listOfValues = new ArrayList();

for (def insightObject: insightObjectAttributeValueBeans) {
if (insightObject == null) {
log.info("GetInsightValue: insightObject is null. Moving on.");
listOfValues.add(null);
}
else {
log.info ("GetInsightValue: object: " + insightObject);

def value = insightObject.getValue();
log.info("GetInsightValue: value: " + value);

if (attribType.equals("STATUS")) {
result = configureFacade.loadStatusTypeBean(value).getName();
log.info ("GetInsightValue: Result: " + result);
log.info ("GetInsightValue: Result class: " + result.getClass().toString());
listOfValues.add(result);
}
else {
log.info ("GetInsightValue: Value class: " + value.getClass().toString());
listOfValues.add(value);
}
}
}

return listOfValues;
}


def GetInsightObject (def log, int InsightObjectId, int InsightAttributeId) {
log.info("GetInsightObject function has been called.");

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


def obj = objectFacade.loadObjectBean(InsightObjectId);
if (obj == null) {
log.warn("GETINSIGHTOBJECT: Cannot find the Insight object. UsingInsightObjectId: " + InsightAttributeId + ". Exiting.");
return null; }
else { log.info("GETINSIGHTOBJECT: Insight object is: " + obj); }

def attribOTAB = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId);
log.info ("GETINSIGHTOBJECT: attribOTAB: " + attribOTAB);
log.info ("GETINSIGHTOBJECT: attribOTABType: " + attribOTAB.getType().name());

def attribOA = objectFacade.loadObjectAttributeBean(InsightObjectId, InsightAttributeId);
log.info ("GETINSIGHTOBJECT: attribOA: " + attribOA);
if (attribOA == null) {
log.warn("GETINSIGHTOBJECT: attribOA is null. Either the value of the attribute is null or the InsightAttributeId is bad. Using InsightAttributeId " + InsightAttributeId);
return null;
}
else {
log.info("GETINSIGHTOBJECT: Insight Attribute: " + attribOA);
}

def attribType = attribOTAB.getType();
if (attribType == null) {
log.warn("GETINSIGHTOBJECT: Cannot find the Insight Attribute Type. Using InsightAttributeId " + InsightAttributeId);
return null;
}
else {
log.info("GETINSIGHTOBJECT: Insight Attribute Type: " + attribType.name());
}

def attribValues = attribOA.getObjectAttributeValueBeans();
if (attribValues == null) {
log.warn("GETINSIGHTOBJECT: Could not find values for this field. Returning null.");
return null;
}
else {
log.info("GETINSIGHTOBJECT: Returning ObjectAttributeValueBean");
return attribValues;
}


/*

// EXAMPLES FOR USING THIS FUNCTION
objectId = 29461 //zny7jhillold
//objectId = 4815
//attributeId = 42713 //truefalse
attributeBuildDateId = 42682 //BuildDate
attributeCostId = 42647 //Cost (double)
attributePONumId = 42648 //purchase order number (integer)
attributeId = 42629 //Site (referenced object)
attributeId = 42626 //description (text)
attributeId = 42530 // Name (textshort)
attributeId = 42641 //ManagementOption1 (Select)
attributeId = 42632 //Lifecycle State (Status)



def result = GetInsightValue(log, objectId, attributeId); //boolean
return "result: " + result;
//result = GetInsightValue(log, objectId, attributeId); //date
//result = GetInsightValue(log, objectId, attributeId) //double
//result = GetInsightValue(log, objectId, attributeId); //integer
//result = GetInsightValue(log, objectId, attributeId); //referencedobject
//result = GetInsightValue(log, objectId, attributeId); //text




*/
}








boolean SetInsightValue (def log, int InsightObjectId, int InsightAttributeId, def NewValue) {
log.info("SETINSIGHTVALUE: SetInsightValue function has been called.");

/* Get Insight Object Facade from plugin accessor */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Get Insight Object Type Facade from plugin accessor */
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


def obj = objectFacade.loadObjectBean(InsightObjectId);
if (obj == null) {
log.warn("SETINSIGHTVALUE: Cannot find the Insight object. UsingInsightObjectId: " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("SETINSIGHTVALUE: Insight object is: " + obj); }

log.info("SETINSIGHTVALUE: Object found: " + obj.getName());

attribType = objectTypeAttributeFacade.loadObjectTypeAttributeBean(InsightAttributeId);
if (attribType == null) {
log.warn("SETINSIGHTVALUE: Cannot find the attribute type in the object definition. Using InsightAttributeId " + InsightAttributeId + ". Exiting.");
return false; }
else { log.info("SETINSIGHTVALUE: Attribute type for Insight object: " + attribType); }


newAttrib = obj.createObjectAttributeBean(attribType);
log.info("SETINSIGHTVALUE: Created newAttrib:" + newAttrib);

def newAttribValue = newAttrib.createObjectAttributeValueBean();
log.info("SETINSIGHTVALUE: Created newAttribValue");

log.info("SETINSIGHTVALUE: Trying to set value " + NewValue);
log.info("SETINSIGHTVALUE: Class of new value is " + NewValue.getClass());

log.info("SETINSIGHTVALUE: ----");
try {
newAttribValue.setValue(attribType, NewValue);
} catch (Exception vie) {
log.warn("SETINSIGHTVALUE: Unable to set attribute " + attribType + " to " + NewValue + ". Exiting.");
log.warn(SETINSIGHTVALUE: vie.getMessage());
return false;
}


log.info("SETINSIGHTVALUE: About to save value");
def attribValues = newAttrib.getObjectAttributeValueBeans();
attribValues.add(newAttribValue);
newAttrib.setObjectAttributeValueBeans(attribValues);

try {
newAttrib = objectFacade.storeObjectAttributeBean(newAttrib);
} catch (Exception vie) {
log.warn("SETINSIGHTVALUE: Unable to save changes. Exiting.");
log.warn(SETINSIGHTVALUE: vie.getMessage());
return false;
}
log.info("SETINSIGHTVALUE: Successfully saved value");
log.info("SETINSIGHTVALUE: ===================");

return true;

/*
EXAMPLES FOR USING THIS FUNCTION
objectId = 29461 //zny7jhillold
objectId = 4815
attributeId = 42713 //truefalse
//attributeId = 42682 //BuildDate
//attributeId = 42647 //Cost (double)
//attributeId = 42648 //purchase order number (integer)
//attributeId = 42629 //Site (referenced object)
//attributeId = 42626 //description (text)
//attributeId = 42530 // Name (textshort)
//attributeId = 42641 //ManagementOption1 (Select)
//attributeId = 42632 //Lifecycle State (Status)


boolean result = SetInsightValue(log, objectId, attributeId, true); //boolean
result = SetInsightValue(log, objectId, attributeId, Date.parse("yyyy-MM-dd", "2018-05-03")); //date
result = SetInsightValue(log, objectId, attributeId, new BigDecimal(5.1).doubleValue()) //double
result = SetInsightValue(log, objectId, attributeId, 5); //integer
result = SetInsightValue(log, objectId, attributeId, 10057); //referencedobject
result = SetInsightValue(log, objectId, attributeId, "DISPOSED"); //text
*/
}
Eric Collins October 3, 2018

Thank you again. Do you keep any of this in a repo? It would be awesome to collaborate on this and other things. 

Martin Cleaver
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.
January 11, 2019

Have suggested Riada subsume into their code base: https://jira.riada.io/browse/ICS-1083

Jon Hill January 11, 2019

I like Eric's idea of putting this into a repo.  If Riada is okay with my doing so, I will try.  I've spent some time refactoring and debugging my class.gr script and it's easier to use now than it was.

Eric Collins January 11, 2019

@Jon Hill Do you want to contact me directly?egcollins85@gmail.com

Gleb Kartashov August 7, 2019

@Jon Hill 

Hi! I'm kinda late for this discussion, but could you explain something to me?

I'm using your setInsightValue function inside Scripted (Groovy) operation on issue (JMWE add-on) and when I'm trying to update an object with boolean value this error occurs:

groovy.lang.MissingMethodException: No signature of method: script15651608805311264077721.setInsightValue() is applicable for argument types: (java.lang.Integer, java.lang.Integer, java.lang.Boolean) values: [27, 227, true]

 What is causing it? I thought the newValue input argument could be any type. 

Also, same error when using date/time value instead of boolean

Jon Hill August 7, 2019

did you add the log variable at the beginning?

 

e.g. setInsightValue(log, 27, 227, true)

Like Gleb Kartashov likes this
Gleb Kartashov August 7, 2019

Thank for responding!

Is the log necessary? I've modified the function to consume only object params and new value, and removed all logging actions

Jon Hill August 8, 2019

No, not necessary.  Just be sure that you remove it from the function's parameter list.

Marco Brundel
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.
May 9, 2022

Hi @Jon Hill ,

the code you shared solved my problem updating an attribute. Thanks for sharing.

greetings, Marco

TAGS
AUG Leaders

Atlassian Community Events