Postfunction velocity input template parameter retrieval problem!

Laurentiu Nicolae January 12, 2018

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 votes
Answer accepted
Alexey Matveev
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 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;
    }

}
Laurentiu Nicolae January 13, 2018

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