从属性渲染器访问流程变量

时间:2014-06-16 10:09:19

标签: activiti

我正在尝试编写一个渲染器,根据前一个任务中设置的字符串数组输出select列表。在上一个任务的execute方法中,我有:

List<String> names =  new ArrayList<String>();
names.add("Bob");
names.add("Fred");

delegate.setVariable("names", names);

然后我尝试扩展EnumFormPropertyRenderer,并覆盖getPropertyField

@Override
public Field getPropertyField(FormProperty formProperty) {

    ComboBox comboBox = new ComboBox(getPropertyLabel(formProperty));

    // Bits copied from getPropertyField in EnumFormPropertyRenderer        

    if (values != null) {
        ...

我的问题是,我无法在names内找到访问我的数组getPropertyField的方法 - 它不是我可以看到的formProperty的一部分,我也看不到它ProcessEngines.getDefaultProcessEngine().getRuntimeService()因为我似乎无法访问执行ID(这是获取变量的任何调用的必需参数)。

所以 - 在getPropertyField内,我怎样才能获得我的数组names?或者我只是从一开始就从错误的角度处理问题?

1 个答案:

答案 0 :(得分:0)

我不确定这是否是“理想”的解决方案,但对我有用的是使用实例ID。在流程开始时,我添加了一个包含以下内容的服务任务:

    @Override
    public void execute(DelegateExecution delegate) throws Exception {

        delegate.setVariable("instanceId", delegate.getProcessInstanceId());

    }

在此过程中,我的任务像以前一样设置数组,根据当前委托设置变量:

    List<String> names =  new ArrayList<String>();
    names.add("Bob");
    names.add("Fred");

    delegate.setVariable("names", names);

然后,当我需要在我的下拉列表中显示它们时,我将它绑定到变量instanceId

    <activiti:formProperty id="instanceId"
        name="Please select the name" type="customSelection"
        variable="instanceId" />

这意味着在属性渲染器中我可以使用:

检索实例ID
    String currentInstanceId = (String) formProperty.getValue();

最后用:

检索我的数组
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    List<String> names = (List<String>) runtimeService.getVariable
        (currentInstanceId, "names");

填充下拉列表。

唯一可能的问题是它填充回变量instanceId,这在我的案例中不是问题,但可能在其他地方;如果再次需要相同的下拉菜单,则需要另一个呼叫才能重置instanceId的第一个服务任务。