Postfunction velocity input template parameter retrieval problem!

Good day.

I am fairly new to jira development, I have searched for a similar topic and found nothing, so, hopefully, this will not be a double question.

I have a problem with getting a velocity input parameter in the factory class, count in this case. I can not retrieve it from the FunctionDescriptor, can anyone please tell me what I am doing wrong?

This is the code in the input/edit velocity template:

<tr>
    <td class="fieldLabelArea">
        Number of subtasks:
    </td>
    <td nowrap>
        <input name="count" id="count" value="$count"/>
    </td>
</tr>

the view template:

Creates a subtask/subtasks for a transitioning issue! Current number of subtasks for a transitioning issue is set to $count!

 And the postfunction factory class:

public class CreateSubTaskForIssuePostFunctionFactory extends AbstractWorkflowPluginFactory implements WorkflowPluginFunctionFactory{
    private static final Logger log = LoggerFactory.getLogger(CreateSubTaskForIssuePostFunctionFactory.class);
    private final static String COUNT="count";
    private final static String DEFAULT_COUNT="1";
        
    @Override
    protected void getVelocityParamsForEdit(Map<String, Object> velocityParams, AbstractDescriptor abstractDescriptor) {
        getVelocityParamsForInput(velocityParams);    
        getVelocityParamsForView(velocityParams,abstractDescriptor);
    }

    @Override
    protected void getVelocityParamsForInput(Map<String, Object> velocityParams) {
        velocityParams.put(COUNT, DEFAULT_COUNT);
    }

    @Override
    protected void getVelocityParamsForView(Map<String, Object> velocityParams, AbstractDescriptor abstractDescriptor) {
            velocityParams.put(COUNT, getCount(abstractDescriptor));        
    }
    
    private String getCount(AbstractDescriptor abstractDescriptor) {
        if(!(abstractDescriptor instanceof FunctionDescriptor)) {
            throw new IllegalArgumentException("Descriotor must be a FunctionDescriptor!");
        }
        FunctionDescriptor functionDescriptor = (FunctionDescriptor) abstractDescriptor;
        String numberOfSubtasks = (String) functionDescriptor.getArgs().get(COUNT);
        if (numberOfSubtasks == null)log.error("Something is wrong! Count is: "+numberOfSubtasks);
        return (numberOfSubtasks != null && numberOfSubtasks.trim().length()>0)?numberOfSubtasks:DEFAULT_COUNT;
    }
    
    public Map<String, ?> getDescriptorParams(Map<String, Object> formParams) {
        if(formParams != null && formParams.containsKey(COUNT)) {
            return MapBuilder.build(COUNT, extractSingleParam(formParams, COUNT));
        }
        return MapBuilder.emptyMap();
    }

}

 

1 answer

1 accepted

0 vote
Alexey Matveev Community Champion Jan 12, 2018

Hello,

Here is my code which works. Try to compare.

My edit vm

<tr>
    <td class="fieldLabelArea">
        $i18n.getText("cint_post.json.label")
    </td>
    <td nowrap>
        <textarea class="textarea" name="jsonField" id="jsonField" >$jsonField</textarea>
    </td>
</tr>

My view vm

$i18n.getText("cint_post.json.message") '$jsonField'

My class

import com.atlassian.jira.plugin.workflow.AbstractWorkflowPluginFactory;
import com.atlassian.jira.plugin.workflow.WorkflowPluginFunctionFactory;
import com.atlassian.jira.workflow.JiraWorkflow;
import com.atlassian.jira.workflow.WorkflowManager;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.opensymphony.workflow.loader.*;
import webwork.action.ActionContext;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.HashMap;
import java.util.Map;

/**
 * This is the factory class responsible for dealing with the UI for the post-function.
 * This is typically where you put default values into the velocity context and where you store user input.
 */
@Named
public class CintCreateOrUpdateFactory extends AbstractWorkflowPluginFactory implements WorkflowPluginFunctionFactory
{
    public static final String FIELD_MESSAGE = "jsonField";

    private WorkflowManager workflowManager;

    @Inject
    public CintCreateOrUpdateFactory(@ComponentImport WorkflowManager workflowManager) {
        this.workflowManager = workflowManager;
    }

    @Override
    protected void getVelocityParamsForInput(Map<String, Object> velocityParams) {
        Map<String, String[]> myParams = ActionContext.getParameters();
        final JiraWorkflow jiraWorkflow = workflowManager.getWorkflow(myParams.get("workflowName")[0]);

        //the default message
        velocityParams.put(FIELD_MESSAGE, "" );

    }

    @Override
    protected void getVelocityParamsForEdit(Map<String, Object> velocityParams, AbstractDescriptor descriptor) {
        getVelocityParamsForInput(velocityParams);
        getVelocityParamsForView(velocityParams, descriptor);
    }

    @Override
    protected void getVelocityParamsForView(Map<String, Object> velocityParams, AbstractDescriptor descriptor) {
        if (!(descriptor instanceof FunctionDescriptor)) {
            throw new IllegalArgumentException("Descriptor must be a FunctionDescriptor.");
        }

        FunctionDescriptor functionDescriptor = (FunctionDescriptor)descriptor;
        String message = (String)functionDescriptor.getArgs().get(FIELD_MESSAGE);

        if (message == null) {
            message = "No Message";
        }

        velocityParams.put(FIELD_MESSAGE,message);
    }


    public Map<String,?> getDescriptorParams(Map<String, Object> formParams) {
        Map params = new HashMap();

        // Process The map
        String message = extractSingleParam(formParams,FIELD_MESSAGE);
        params.put(FIELD_MESSAGE,message);

        return params;
    }

}

Thank you very much for your prompt response! The problem was with using "count" in the velocity context, once I have renamed it to something else it worked! Now I  just need to validate the fact that is a numeric value and I am done

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

727 views 6 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you