Preethi H R
Preethi H R

Reputation: 43

Apache Velocity not able to fetch the values from data map sent using Jira Template Renderer

I have developed a Jira Plugin for Server in which I am sending the required data to the velocity template files from the Get Servlet using render method of Template Renderer (https://docs.atlassian.com/atlassian-template-renderer/1.5.2/apidocs/com/atlassian/templaterenderer/TemplateRenderer.html)

The code for that is given below:

@ComponentImport
private final TemplateRenderer templateRenderer;

Map<String, Object> data = service.populateDataForPlugin(pkey, pid, token,
manageFieldsRule, hideFieldService, mandateFieldService, configuration);

// To render the velocity template UI with required data
templateRenderer.render("templates/init.vm", data, res.getWriter());

Adding populateDataForPlugin code below:

import com.aidt.managefields.entity.ManageFieldRules;


// Populate data map with required values to fetch it in doGet servlet
    @Override
    public Map<String, Object> populateDataForPlugin(String pkey, String pid, String token,
            ManageFieldsRule manageFieldsRule, HideFieldService hideFieldService,
            MandateFieldService mandateFieldService, List<AmfConfigurations> configuration) {

        Map<String, Object> data = Maps.newHashMap();
        data.put("pkey", pkey);
        data.put("pid", pid);

        // Fetch existing Manage field rules from database
        List<ManageFieldRules> exisitingRules = manageFieldsRule.getRules();

        List<ManageFieldRules> projectSpecificHidingRules = new ArrayList<>();
        List<ManageFieldRules> projectSpecificMandateRules = new ArrayList<>();

        // Fetch Manage field rules present in current project
        if (exisitingRules != null && !exisitingRules.isEmpty()) {
            for (ManageFieldRules rule : exisitingRules) {
                if (rule.getPid().equals(pid) && rule.getPkey().equals(pkey) && rule.getOperation().equals("Hide")) {
                    String hideIssueTypeIds = rule.getIssueType().replaceAll("\\[", "").replaceAll("\\]", "");
                    rule.setIssueType(hideIssueTypeIds);
                    projectSpecificHidingRules.add(rule);
                }
                if (rule.getPid().equals(pid) && rule.getPkey().equals(pkey) && rule.getOperation().equals("Mandate")) {
                    String mandateIssueTypeIds = rule.getIssueType().replaceAll("\\[", "").replaceAll("\\]", "");
                    rule.setIssueType(mandateIssueTypeIds);
                    projectSpecificMandateRules.add(rule);
                }
            }
        }
        data.put("hideRuleList", projectSpecificHidingRules);
        data.put("mandateRuleList", projectSpecificMandateRules);

        final Project project = projectManager.getProjectByCurrentKey(pkey);

        // Fetch all issue types mapped to current project
        Collection<IssueType> projectSpecificIssueTypes = ComponentAccessor.getIssueTypeSchemeManager()
                .getIssueTypesForProject(project);

        // Convert collection of issue type to list of maps with issue type id and issue type name
        List<Map<String, Object>> allIssueTypesInProject = ManageFieldsUtility
                .getMapOfIssueTypesSpecificToProject(projectSpecificIssueTypes);
        data.put("allIssueTypesInProject", allIssueTypesInProject);

        // Fetch all system fields in jira instance except fields which are already mandated
        List<Field> allSystemFields = mandateFieldService.findSystemFields();

        // Fetch all custom fields if current project is present in it's context
        List<CustomField> projectSpecificCustomFields = hideFieldService
                .projectSpecificCustomFields(Long.parseLong(pid), issueTypes);

        // Fetch list of all custom fields excluding locked and message custom fields
        List<CustomField> customFieldsExLockedMessageCf = hideFieldService
                .fetchCustomFieldsExLockedMessage(projectSpecificCustomFields, managedConfigurationItemService);

        // Concat System fields and Custom fields
        List<Field> allFields = Stream.concat(allSystemFields.stream(), customFieldsExLockedMessageCf.stream())
                .collect(Collectors.toList());

        // Fetch final list of fields which are eligible to hide in current project
        List<Map<String, Object>> eligibleFieldsToHide = ManageFieldsUtility
                .eligibleFieldsToHide(customFieldsExLockedMessageCf, allFields, exisitingRules, configuration);
        data.put("hideIssueFields", eligibleFieldsToHide);

        // Fetch all custom fields in jira instance
        List<CustomField> allCustomFields = cfm.getCustomFieldObjects();

        // Fetch system fields, custom fields excluding locked fields and message custom fields
        List<Map<String, Object>> allFieldsExLockedMessageCf = mandateFieldService
                .findAllFieldsExLockedMessageFields(allCustomFields, managedConfigurationItemService);
        data.put("systemCustomFields", allFieldsExLockedMessageCf);

        // Fetch final list of fields which are eligible to mandate in current project
        List<Map<String, Object>> eligibleFieldsToMandate = ManageFieldsUtility.eligibleFieldsToMandate(allFields,
                exisitingRules, configuration);
        data.put("mandateIssueFields", eligibleFieldsToMandate);

        // Fetch all custom fields excluding locked and message custom fields
        List<Map<String, Object>> allCustomFieldsExLockedMessageCf = hideFieldService
                .findAllCustomFieldsExLockedMessageCf(managedConfigurationItemService);
        data.put("allCustomFields", allCustomFieldsExLockedMessageCf);

        // Add generated unique CSRF token
        data.put("csrfToken", token);

        return data;
    }

In my velocity file, I am fetching RuleId from data map which is passed using template renderer using the following code:

#set ($ruleId = $rule.getID())
RuleId is $ruleId

But the value is not getting populated on Jira UI as seen in the attached screenshot:

Jira_ss

But when I fetch the same Rule Id value in the backend, the values are present as expected. I am attaching the screenshot: Output_ss

The issue is data is present in the backend but the values are not getting populated on the Jira UI. Can anyone please help me in resolving this issue.

Jira Software version: 9.2.0

Thanks & Regards, Preethi H R

Upvotes: 0

Views: 171

Answers (0)

Related Questions