What we really need is an function that will return a handle to the current session. We have created some new FormTypes that need database access. We need them to use the currently active session. My code is below. It fills in a combo box from the database. Our app takes care of generating the user forms.
I think we had tried the Context before but could not figure out how to get a handle from within the formtype code.
package org.activiti.custom;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.activiti.custom.spring.CustomQuery;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.form.AbstractFormType;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.db.DbSqlSession;
public class EnumSelectRespEngFormType extends AbstractFormType {
protected Map<String, String> values;
public EnumSelectRespEngFormType() {
}
public EnumSelectRespEngFormType(Map<String, String> values) {
this.values = values;
}
public String getName() {
return "enumselectrespeng";
}
@Override
public Object getInformation(String key) {
Connection c = null;
if (key.equals("values")) {
Map<String,ProcessEngine> pe = ProcessEngines.getProcessEngines();
for (Entry<String, ProcessEngine> enumEntry : pe.entrySet()) {
System.out.println("ProcessEngine name="+enumEntry.getKey());
ProcessEngineImpl pei = (ProcessEngineImpl)enumEntry.getValue();
c = ((DbSqlSession)pei.getDbSqlSessionFactory().openSession()).getSqlSession().getConnection();
}
String qry = "select cu.username,cu.FIRST_NAME||' '||cu.LAST_NAME from cm_users cu,cm_group cg where upper(cu.USERNAME)=upper(cg.USERNAME) and upper(cg.GROUPNAME)='STARSCNRESPENG'";
//Connection connection = Context.getCommandContext().getDbSqlSession().getSqlSession().getConnection();
String[][] qryResults = CustomQuery.getResultSet(c,qry, 2);
Map<String, String> v = new HashMap<String,String>();
if ((qryResults != null) && (qryResults.length > 0)) {
for (int count = 0; count < qryResults.length; count++) {
v.put(qryResults[count][0], qryResults[count][1]);
}
}
return v;
}
return null;
}
@Override
public Object convertFormValueToModelValue(String propertyValue) {
validateValue(propertyValue);
return propertyValue;
}
@Override
public String convertModelValueToFormValue(Object modelValue) {
if(modelValue != null) {
if(!(modelValue instanceof String)) {
throw new ActivitiIllegalArgumentException("Model value should be a String");
}
validateValue((String) modelValue);
}
return (String) modelValue;
}
protected void validateValue(String value) {
if(value != null) {
if(values != null && !values.containsKey(value)) {
throw new ActivitiIllegalArgumentException("Invalid value for enum form property: " + value);
}
}
}
}