Behaviours validation does not work.

Tihomir Nikolov _Nemetschek Bulgaria_
Contributor
March 12, 2012

Hi.

I have tried to use the behaviours plugin with JIRA 4.4 but for some reason it looks like the values are not parced correct.

here is the expample:

import org.ofbiz.core.entity.GenericValue
FormField formComponent = getFieldById(fieldChanged)
FormField formSubcomponent = getFieldByName ("Subcomponent")
Object componentFormValue = formComponent.getFormValue()
List componentIds = []
if (componentFormValue instanceof List) {
componentIds.addAll(componentFormValue as List)
}
else {
// could be an empty string if all components are deselected
if (componentFormValue) {
componentIds.add(componentFormValue)
}
}
Map fieldOptions = [:]
fieldOptions.put ("-1", "None")
for (Long componentId : componentIds) {
try {
Long.parseLong(componentId)
} catch (NumberFormatException e) {
log.error ("Could not get component Id as Long")
return
}
GenericValue component = componentManager.getProjectManager().getComponent(Long.parseLong(componentId))
switch (component?.get("name")) {
case "Component 1" :
fieldOptions.putAll (["Lemons":"Lemons", "Oranges":"Oranges"])
break
case "Component 2" :
fieldOptions.putAll (["Spinach":"Spinach", "Celery":"Celery"])
break
}
}
formSubcomponent.setFieldOptions (fieldOptions)
The problem is that it shows either all values no matter what is the component setting.
It looks like che if is not working as expected. Can someone help ?

6 answers

0 votes
Brent Webster
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.
June 6, 2013

After banging my head for a day and a bit, this C/Perl guy believes I have found the problem. In the catalina.out log file, I kept seeing the error:

Cannot cast object '10600' with class 'java.lang.String' to class 'java.lang.Long'

As part of the for loop, the componentId cannot be cast as Long, it is a String so change it from

for (Long componentId : componentIds) {

to

for (String componentId : componentIds) {

and it all works now.

Next, problem to tackle only disable inline editting of this SubComponent field and not all the fields!

0 votes
John Woolley April 17, 2013

Same issue here as well. In the logs I get

2013-04-18 11:58:54,697 http-bio-8080-exec-10 ERROR user 718x10939x1 y738lx 10.9.39.188 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [onresolve.jira.groovy.BehaviourManagerImpl] Something went wrong with method run in class
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'com.atlassian.jira.util.json.JSONObject$Null' to class 'java.lang.Long'

0 votes

Hi,

I have the same problem with the same example: it shows either all values no matter what is the component setting.Can anybody help me?. I'm working on Jira 5.2.7.

Thanks in advanced.

Brent Webster
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.
June 6, 2013

I hope someone has an answer because I'm trying to do the same thing in Jira 5.2.2.

0 votes
Wolfgang Fellner
Contributor
May 30, 2012

Hi Jamie,

i try to get the example running, too. But i also get an error using the following validator script (based on your example):

import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.ComponentManager
ComponentManager componentManager = ComponentManager.getInstance()

FormField formComponent = getFieldById(fieldChanged)
    FormField formSubcomponent = getFieldByName ("Subcomponent")
 
    Object componentFormValue = formComponent.getFormValue()
    List componentIds = []
 
    if (componentFormValue instanceof List) {
        componentIds.addAll(componentFormValue as List)
    }
    else {
        // could be an empty string if all components are deselected
        if (componentFormValue) {
            componentIds.add(componentFormValue)
        }
    }
 
    Map fieldOptions = [:]
    fieldOptions.put ("-1", "None")
 
    for (Long componentId : componentIds) {
        try {
            Long.parseLong(componentId)
        } catch (NumberFormatException e) {
            log.error ("Could not get component Id as Long")
            return
        }
 
        GenericValue component = componentManager.getProjectManager().getComponent(Long.parseLong(componentId))
 
        switch (component?.get("name")) {
            case "Component 1" :
                fieldOptions.putAll ([10002:"Lemons", 10003:"Oranges"])
                break
 
            case "Component 2" :
                fieldOptions.putAll ([10004:"Spinach", 10005:"Celery"])
                break
 
        }
    }
    formSubcomponent.setFieldOptions (fieldOptions)

I always get the following error:

127.0.0.1 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json
[onresolve.jira.groovy.BehaviourManagerImpl] Something went wrong with method run in class
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10002' with class 'java.lang.String' to class 'java.lang.Long'

Using JIRA 4.4.5, Behaviour plugin 0.5.0, Script runner 2.0.6

Regards, Wolfgang

JamieA
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 31, 2012

Cannot cast object '10002' with class 'java.lang.String' to class 'java.lang.Long'

Not sure which bit of your code is failing but it needs casting to a long, eg

thing as Long

or "new Long (thing)"

A b June 19, 2012

You can surround the number with quotes, i.e [10002:"Lemons" to be ["10002":"Lemons"

0 votes
Wolfgang Fellner
Contributor
May 30, 2012

Hi Jamie,

i'm trying to get the example working for me to (in a new test instance).

Test project is created with the Components "Component 1" and "Component 2", mulitselect field "Subcomponents" is created with the example values.

I use the following behavior validation script (based on your example):

import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.ComponentManager
ComponentManager componentManager = ComponentManager.getInstance()

FormField formComponent = getFieldById(fieldChanged)
    FormField formSubcomponent = getFieldByName ("Subcomponent")
 
    Object componentFormValue = formComponent.getFormValue()
    List componentIds = []
 
    if (componentFormValue instanceof List) {
        componentIds.addAll(componentFormValue as List)
    }
    else {
        // could be an empty string if all components are deselected
        if (componentFormValue) {
            componentIds.add(componentFormValue)
        }
    }
 
    Map fieldOptions = [:]
    fieldOptions.put ("-1", "None")
 
    for (Long componentId : componentIds) {
        try {
            Long.parseLong(componentId)
        } catch (NumberFormatException e) {
            log.error ("Could not get component Id as Long")
            return
        }
 
        GenericValue component = componentManager.getProjectManager().getComponent(Long.parseLong(componentId))
 
        switch (component?.get("name")) {
            case "Component 1" :
                fieldOptions.putAll ([10002:"Lemons", 10003:"Oranges"])
                break
 
            case "Component 2" :
                fieldOptions.putAll ([10004:"Spinach", 10005:"Celery"])
                break
 
        }
    }
    formSubcomponent.setFieldOptions (fieldOptions)

But when i try to edit an issue i get thw following errors in log:

127.0.0.1 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json
[onresolve.jira.groovy.BehaviourManagerImpl] Something went wrong with method run in class
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10002' with class 'java.lang.String' to class 'java.lang.Long'

Regards,

Wolfgang

0 votes
JamieA
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 13, 2012

I think we are having the same conversation here https://studio.plugins.atlassian.com/wiki/display/JBHV/Miscellaneous+Behaviours+Examples?focusedCommentId=67797592#comment-67797592 right?

There are two problems. That is only sample code, so unless you have components called Component 1 or Component 2 then neither of the two cases will match.

However, in that case, you should see no options, not all options.

You have no definition for componentManager, you need to add near the top:

import com.atlassian.jira.ComponentManager

ComponentManager componentManager = ComponentManager.getInstance()
This code must be erroring at runtime, have you checked your logs? You need to.
The final problem that you haven't got to is that this sample code code is out of date, now you need to set the value IDs. So if I try to submit the issue after choosing one of the sub-values, I will get:
Invalid value 'Lemons' passed for customfield 'Subcomponent'. Allowed values are: 10030[Lemons], 10031[Oranges], 10032[Spinach], 10033[Celery], -1

Therefore I need to change the code to:

fieldOptions.putAll (["10030":"Lemons", "10031":"Oranges"])

Tihomir Nikolov _Nemetschek Bulgaria_
Contributor
March 13, 2012

Hi, Jamie.

Thanks for the support.

Yes, this is the same conversation.

It seems like I need the log files, so I made the same set up on a sandbox JIRA.

I enabled the log file.

After enabling the logging, in the atlassian-jira.log I see only:

{code}2012-02-20 00:26:41,737 http-8443-7 DEBUG tnikolov 26x17135x1 z4e44 192.168.164.1 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [onresolve.jira.groovy.BehaviourManagerImpl] Returning map: [:]
{code}

I have Component 1 and Component 2 for this project.

I have also added a fifth subcomponent- Onions in the list.

I do not get the error message for wrong ID's (or I do not know where to look for it)

If I choose either Component 1 or Component 2 or some other component from the list (e.g Component 3, for wich there is no rule in the code), I see all the five options in the subcomponent field.

Is it possible for some reason the code not to be able to recognize the parced value of the component and to put all values because of this ?

Thnak you and excuse for doubling the issue.

I am really keen on using behaviours.

Cheers, Tihomir.

JamieA
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 15, 2012

Have you definitely mapped this behaviour to this project, and that there are no other behaviours also mapped (just because it will confuse debugging)?

Tihomir Nikolov _Nemetschek Bulgaria_
Contributor
March 26, 2012

Hi, Jamie.

I Confirm that the behaviour is attached to the project I am testing with.

Cheers, Tihomir.

M October 30, 2013

Hi Tihomir,

Did you manage to get this issue working? Currently, I experiencing the same problem.

Regards,

MG

Anirudh Koutha April 21, 2015

I am using JIRA Behaviour Plugin to populate Multi-Select customfield options based on a custom field select list option as indicated in https://jamieechlin.atlassian.net/wiki/display/JBHV/Miscellaneous+Behaviours+Examples

JIRA version : 5.2.7 and

Behaviour Plugin - 0.5.3

But I keep hitting this and The script populates the full list of options. I see no error in the logs except this.

/rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/runvalidator.json [onresolve.jira.groovy.BehaviourManagerImpl] Returning map: [:]

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events