Storing Java Array Value into process variable


#1

Can anyone tell me what is the procedure to store ArrayList value into a process variable.For Example:
package org.login.demo.TaskLogin;

import java.awt.List;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.slf4j.Logger;

//@ManagedBean(name = “ProjectData”, eager = true)
public class ProjectTaskDelegate implements JavaDelegate {

@Override
public void execute(DelegateExecution execution) throws Exception {
	ArrayList<String> projectid=new ArrayList<>();	
	try {
		Class.forName("org.postgresql.Driver");
		System.out.println("Driver Found");
	}catch(Exception e) {
		e.printStackTrace();
	}
	Connection con=null;
	try {
		con=DriverManager.getConnection("jdbc:postgresql://localhost:5000/Camunda","postgres","******");
		Statement st=con.createStatement();
		String sql="SELECT * FROM projectList;";
		ResultSet rs=st.executeQuery(sql);
		Projects proj=new Projects();
		while (rs.next()) {
			String projectId=rs.getString("projectid").toString().trim();
			//String projectName=rs.getString("projectname").toString().trim();
			//String client=rs.getString("client").toString().trim();
			//String projectType=rs.getString("projecttype").toString().trim();
			projectid.add(projectId);	
			}
		System.out.println(projectid);
		execution.setVariable("projectArray", projectid);
	con.close();
		
	}catch(Exception excep) {
		excep.printStackTrace();
	}


}

}

Thanks a lot.
And I know process variable taskes a single value.What i am trying to achive from this code is that i want to display all the data from the array to the usertask.sorry dor noob question.


#2

Setting a process variable works exactly as you have shown above (trusting that you would of course close the database resources after usage). IRL I would also advise to factor out such business logic into a separate service and use

HttpConnector http = Connectors.getConnector(HttpConnector.ID);

to call it. The Camunda delegate beans are recreated from scratch on every usage, you won’t be happy for long if you try to use them as application objects directly, without DI.

The generated forms in the tasklist wrongly tell you that there are no variables if they are of a complex type. But IIRC you should see your variable in the Cockpit on a running process.

In user forms you need to use an embedded html form to display complex objects such as an array.

See https://github.com/camunda/camunda-bpm-examples/tree/master/usertask/task-form-embedded-serialized-java-object for an example where a complex object is loaded into the angular scope. Once you have your projectArray in scope, you use an angularjs ng-repeat directive and bootstrap for styling, sth like

<div class="table-responsive">
  <table class="table">
    <tr ng-repeat="project in projectArray">
      <td>{{project.attr1}}</td>
    </tr>
  </table>
</div>

#3

hi dschulten,
Thanks for your help .while passing the process variable i got org.camunda.bpm.engine.ProcessEngineException: Cannot serialize object in variable ERROR in my Console.i tried different ways to serialize the arrayList but everytime it produce Serialized values=null.can you tell me the way to serialize an arrayList and pass all the array elements through process variable …Thanks a lot.


#4

Have you got a full stacktrace with cause, to show what goes wrong with the serialization? Also, something seems wrong with your code sample, you call

String projectId=rs.getString("projectid").toString().trim();
projectid.add(projectId); // <-- won't compile

When you update the sample, please make sure you indent the entire code by four spaces, that makes it easier on the eye in the forum.


#5

package org.login.demo.TaskLogin;

import java.awt.List;
import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.value.ObjectValue;
import org.slf4j.Logger;

import com.bean.SampleExecutionBean;

public class ProjectTaskDelegate implements JavaDelegate {
ArrayList lstSampleExecution ;
@Override
public void execute(DelegateExecution execution) throws Exception {
// TODO Auto-generated method stub

	java.util.List<Object> projectid=new ArrayList<>();
		
	try {
		Class.forName("org.postgresql.Driver");
		System.out.println("Driver Found");
		
	}catch(Exception e) {
		e.printStackTrace();
	}
	Connection con=null;
	try {
		con= DriverManager.getConnection ("jdbc:postgresql://localhost:5000/Camunda","postgres","athisaya");
		Statement st = con.createStatement();
		String sql = "SELECT * FROM projectlist;";
		ResultSet rs= st.executeQuery(sql);
		while (rs.next()) {				
			String projectId =rs.getString("projectid").trim();
			String projectName =rs.getString("projectname").trim();
			String client = rs.getString("client").trim();
			String projectType = rs.getString("projecttype").trim();
			if ( lstSampleExecution == null ) {
			lstSampleExecution = new ArrayList<>();
			}
			
			SampleExecutionBean objSampleExecutionBean  = new SampleExecutionBean();
			
			objSampleExecutionBean.setProjectId (projectId);
			objSampleExecutionBean.setProjectName (projectName);
			objSampleExecutionBean.setCilent (client);
			objSampleExecutionBean.setProjectType (projectType);
			
			lstSampleExecution.add (objSampleExecutionBean);
			}
		rs.close();
		con.close();

		execution.setVariable ("SampleExecutionData", lstSampleExecution);
	}catch(Exception excep) {
		excep.printStackTrace ();
	}

}

}

This is my updated code and i have created model class for SampleExecutionBean.i added all the data fetched from the DB into IstSampleExecution .so when i pass the Array value into the process variable console brings Serialization error .I know it is a newbie question.And really thanks for spending your time with this …can you tell me where i am wrong and enlighten me…thanks a lot


#6

Please do post the exception stacktrace all the way down to the last caused by: line. I hope to see why it cannot serialize.