Collection form type in Activiti

Here I post an example of collection form type in Activiti. I’ve seen actually only are supported the basic types in the form:

  • boolean
  • long
  • string
  • enum
  • date
  • user

A collection form type could be useful to work with lists and show a list of typed object in the form. Here the basic implementation of the collection form type:

package org.activiti.engine.impl.form;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.impl.form.AbstractFormType;
public class CollectionFormType<T> extends AbstractFormType {
    private Map<String, T> values = new HashMap<String, T>();
    @Override
    public String getName() {
        return "collection";
    }
    @Override
    public Collection<T> convertFormValueToModelValue(String propertyValue) { // key1|key3
        String[] ids = propertyValue.split("\\|");
        Collection<T> ret = new ArrayList<T>();
        for (String id : ids) {
            T element = values.get(id);
            if (element != null) {
                ret.add(element);
            }
        }
        return ret;
    }
    @Override
    @SuppressWarnings("unchecked")
    public String convertModelValueToFormValue(Object modelValue) {
        Collection<T> modelValues = (Collection<T>) modelValue;
        values.clear();
        for (T value : modelValues)
            values.put(value.toString(), value);
        return modelValue.toString();
    }
    @Override
    public Object getInformation(String key) {
        if ("values".equals(key)) {
            return values;
        } else {
            return null;// TODO
        }
    }
}

With this implementation simply set in your activiti form property the type ‘collection’. To view the rendered form property in the activiti explorer, a renderer must be implemented. Here a working sample:

package org.activiti.engine.impl.form;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.activiti.engine.impl.form.AbstractFormType;
public class CollectionFormType<T> extends AbstractFormType {
    private Map<String, T> values = new HashMap<String, T>();
    @Override
    public String getName() {
        return "collection";
    }
    @Override
    public Collection<T> convertFormValueToModelValue(String propertyValue) { // key1|key3
        String[] ids = propertyValue.split("\\|");
        Collection<T> ret = new ArrayList<T>();
        for (String id : ids) {
            T element = values.get(id);
            if (element != null) {
                ret.add(element);
            }
        }
        return ret;
    }
    @Override
    @SuppressWarnings("unchecked")
    public String convertModelValueToFormValue(Object modelValue) {
        Collection<T> modelValues = (Collection<T>) modelValue;
        values.clear();
        for (T value : modelValues)
            values.put(value.toString(), value);
        return modelValue.toString();
    }
    @Override
    public Object getInformation(String key) {
        if ("values".equals(key)) {
            return values;
        } else {
            return null;// TODO
        }
    }
}

This renderer uses the reflection to render all beans inside the list. Automatically it will show all getter methods of the bean. The patch can be found in the activiti codehaus page:

http://jira.codehaus.org/browse/ACT-1367