Apache Velocity 无法从使用 Jira Template Renderer 发送的数据映射中获取值

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

提问人:Preethi H R 提问时间:10/14/2022 最后编辑:Preethi H R 更新时间:10/17/2022 访问量:129

问:

我开发了一个适用于服务器的 Jira 插件,其中我使用 Template Renderer (https://docs.atlassian.com/atlassian-template-renderer/1.5.2/apidocs/com/atlassian/templaterenderer/TemplateRenderer.html 的渲染方法从 Get Servlet 将所需的数据发送到速度模板文件)

代码如下:

@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());

在下面添加 populateDataForPlugin 代码:

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;
    }

在我的速度文件中,我从数据映射中获取 RuleId,该数据映射使用以下代码使用模板渲染器传递:

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

但该值未在 Jira UI 上填充,如随附的屏幕截图所示:

Jira_ss

但是,当我在后端获取相同的规则 ID 值时,这些值按预期存在。我附上了截图:Output_ss

问题是数据存在于后端,但值未在 Jira UI 上填充。任何人都可以帮我解决这个问题。

Jira 软件版本:9.2.0

谢谢和问候, 普雷蒂 H R

Java Velocity Jira 插件 Apache Velocity

评论

0赞 AbdulKarim 10/15/2022
您能否分享一下 service.populateDataForPlugin(pkey, pid, configuration) 中发生的事情;因为问题可能是由 contextMap 不包含$rule变量引起的。您也可以使用以下命令进行验证: #if($rule != null) Rule: $rule & Rule ID: $rule.getID() #end
0赞 Preethi H R 10/17/2022
嗨,阿卜杜勒卡里姆,我在上面添加了service.populateDataForPlugin方法代码片段,并尝试按照建议打印出上述行,这是输出“规则:AO_ADD0BF_MANAGE_FIELD_RULES {ID = 1067} &规则ID:$rule.getID()&规则字段:$rule.getFields()”
0赞 Preethi H R 10/17/2022
正如我们在上面看到的,正在获取规则的值,但规则 ID 和字段中存在的值不会在 Jira UI 上获取

答: 暂无答案