这是我的用例。
1. 我有一个“客户名称”文本字段和“所有客户”单选列表。
2. 问题解决后,我想选择“客户名称”中存在的值并希望添加到“所有客户”中。
如果“所有客户”中已经存在要添加的值,我能够实现这一点。但是,如果“所有客户”字段尚不存在,我想用新值填充它,以便将来可以选择。这个怎么做?可以这样做吗?
这是我的用例。
1. 我有一个“客户名称”文本字段和“所有客户”单选列表。
2. 问题解决后,我想选择“客户名称”中存在的值并希望添加到“所有客户”中。
如果“所有客户”中已经存在要添加的值,我能够实现这一点。但是,如果“所有客户”字段尚不存在,我想用新值填充它,以便将来可以选择。这个怎么做?可以这样做吗?
我找到了答案。这是完整的代码。它的 jira 插件会引发已解决的事件并从一个自定义字段中获取值并填充另一个字段。希望对其他人有所帮助。
package com.spmsoftware.plugin.listeners;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import java.util.List;
import java.util.Map;
/**
* User: adnan
* Date: 5/4/14
* Time: 4:49 PM
*/
public class IssueUpdateListener implements InitializingBean, DisposableBean {
private static final Logger LOGGER = Logger.getLogger(IssueUpdateListener.class);
private final EventPublisher eventPublisher;
// private final JiraAuthenticationContext authenticationContext;
public IssueUpdateListener(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
// this.authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
}
@Override
public void afterPropertiesSet() throws Exception {
eventPublisher.register(this);
}
@Override
public void destroy() throws Exception {
eventPublisher.unregister(this);
}
@EventListener
public void onIssueEvent(IssueEvent issueEvent) {
Long eventTypeId = issueEvent.getEventTypeId();
Issue issue = issueEvent.getIssue();
if (eventTypeId.equals(EventType.ISSUE_RESOLVED_ID)) {
MutableIssue mutableIssue = getMutableIssue(issue);
CustomField customerNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Name");
CustomField allCustomersCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Customers");
Object customerNameVal = mutableIssue.getCustomFieldValue(customerNameCF);
Option newOptions = addOptionToCustomField(allCustomersCF, customerNameVal.toString());
LOGGER.info("New updated option {}" + newOptions);
}
}
private MutableIssue getMutableIssue(Issue issue) {
MutableIssue mutableIssue;
if (issue instanceof MutableIssue) {
mutableIssue = (MutableIssue)issue;
} else {
mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.getKey());
}
return mutableIssue;
}
public Option addOptionToCustomField(CustomField customField, String value) {
Option newOption = null;
if (customField != null) {
List<FieldConfigScheme> schemes = customField.getConfigurationSchemes();
if (schemes != null && !schemes.isEmpty()) {
FieldConfigScheme sc = schemes.get(0);
Map configs = sc.getConfigsByConfig();
if (configs != null && !configs.isEmpty()) {
FieldConfig config = (FieldConfig) configs.keySet().iterator().next();
OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
Options l = optionsManager.getOptions(config);
int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1;
newOption = optionsManager.createOption(config, null, (long) nextSequence, value);
}
}
}
return newOption;
}
}