Workflow utilities

Workflow logger

Usually you want to log the workflow execution, sometimes for debugin purposes and sometimes for store information used to generate reports. You can use this logger class which makes use of a Database Metadata Table to do this.

Create database metadata definition

For OpenKM 7.1

Execute the SQL sentences in jBPM Console database:

create table JBPM_WORKFLOW_LOGGER (
LOG_ID bigint auto_increment,
LOG_DATE datetime,
LOG_CLASS varchar(128),
LOG_PROC_NAME varchar(128),
LOG_PROC_DEF bigint,
LOG_PROC_INS bigint,
LOG_TASK_NAME varchar(128),
LOG_TASK_DEF bigint,
LOG_TASK_INS bigint,
LOG_NODE varchar(64),
LOG_MSG varchar(256),
primary key (LOG_ID)
);

For OpenKM 6.4

Execute the SQL sentences from the OpenKM administration database query:

DELETE FROM OKM_DB_METADATA_TYPE WHERE DMT_TABLE='logger';
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col00', 'log_date', 'date');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col01', 'log_class', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col02', 'log_proc_name', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col03', 'log_proc_def', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col04', 'log_proc_ins', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col05', 'log_task_name', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col06', 'log_task_def', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col07', 'log_task_ins', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col08', 'log_node', 'text');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_VIRTUAL_COLUMN, DMT_TYPE) VALUES ('logger', 'col09', 'log_msg', 'text');

Logger class

For OpenKM 7.1

This class logs the values into the jBPM Console database table:

package com.openkm.workflow;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Calendar;

import javax.sql.DataSource;

import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.util.ContextWrapper;

public class WorkflowLogger {
	private static final Logger log = LoggerFactory.getLogger(WorkflowLogger.class);
	private static final String SQL = "insert into JBPM_WORKFLOW_LOGGER "
			+ "(LOG_DATE, LOG_CLASS, LOG_PROC_NAME, LOG_PROC_DEF, LOG_PROC_INS, LOG_TASK_NAME, LOG_TASK_DEF, LOG_TASK_INS, LOG_NODE, LOG_MSG) "
			+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

	@SuppressWarnings("rawtypes")
	public static void log(Class clazz, ExecutionContext ctx, String msg) {
		DataSource datasource = ContextWrapper.getContext().getBean(DataSource.class);
		ProcessDefinition procDef = ctx.getProcessDefinition();
		ProcessInstance procIns = ctx.getProcessInstance();
		Task task = ctx.getTask();
		TaskInstance taskIns = ctx.getTaskInstance();

		try (Connection con = datasource.getConnection()) {
			try (PreparedStatement ps = con.prepareStatement(SQL)) {
				ps.setTimestamp(1, new Timestamp(Calendar.getInstance().getTimeInMillis()));
				ps.setString(2, clazz.getCanonicalName());
				ps.setString(3, procDef.getName() + " v" + procDef.getVersion());
				ps.setLong(4, procDef.getId());
				ps.setLong(5, procIns.getId());

				if (task != null) {
					ps.setString(6, task.getName());
					ps.setLong(7, task.getId());
					ps.setLong(8, taskIns.getId());
				} else {
					ps.setNull(6, Types.NULL);
					ps.setNull(7, Types.NULL);
					ps.setNull(8, Types.NULL);
				}

				ps.setString(9, (String) ctx.getVariable("uuid"));
				ps.setString(10, msg);
				ps.execute();
			}
		} catch (SQLException e) {
			log.error(e.getMessage(), e);
		}
	}
}

 

OpenKM 6.4

This class logs the values into the OpenKM database metadata table:

package com.openkm.workflow;
 
import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
 
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskInstance;
 
import com.openkm.core.DatabaseException;
import com.openkm.dao.DatabaseMetadataDAO;
import com.openkm.dao.bean.DatabaseMetadataValue;
import com.openkm.frontend.client.util.metadata.DatabaseMetadataMap;
import com.openkm.util.DatabaseMetadataUtils;
import com.openkm.util.ISO8601;
 
public class WorkflowLogger {
	@SuppressWarnings("rawtypes")
	public static void log(Class clazz, ExecutionContext ctx, String msg) throws DatabaseException, IllegalAccessException,
			InvocationTargetException {
		ProcessDefinition procDef = ctx.getProcessDefinition();
		ProcessInstance procIns = ctx.getProcessInstance();
		Task task = ctx.getTask();
		TaskInstance taskIns = ctx.getTaskInstance();
 
		Map<String, String> logger = new HashMap<String, String>();
		logger.put(DatabaseMetadataMap.MV_NAME_TABLE, "logger");
		logger.put("log_date", ISO8601.format(Calendar.getInstance()));
		logger.put("log_class", clazz.getCanonicalName());
		logger.put("log_proc_name", procDef.getName() + " v" + procDef.getVersion());
		logger.put("log_proc_def", Long.toString(procDef.getId()));
		logger.put("log_proc_ins", Long.toString(procIns.getId()));
 
		if (task != null) {
			logger.put("log_task_name", task.getName());
			logger.put("log_task_def", Long.toString(task.getId()));
			logger.put("log_task_ins", Long.toString(taskIns.getId()));
		}
 
		logger.put("log_node", (String) ctx.getVariable("uuid"));
		logger.put("log_msg", msg);
		DatabaseMetadataValue dmv = DatabaseMetadataUtils.getDatabaseMetadataValueByMap(logger);
		DatabaseMetadataDAO.createValue(dmv);
	}
}

Saving workflow data in MySQL table in a transition

package com.openkm.workflow.archivage;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

public class InsertMysql_db implements ActionHandler {
	private static final long serialVersionUID = 1L;

	public void execute(ExecutionContext executionContext) throws Exception {
		String myDriver = "com.mysql.jdbc.Driver";
		String myUrl = "jdbc:mysql://localhost:3306/okmdb";
		Class.forName(myDriver);
		Connection conn = DriverManager.getConnection(myUrl, "kouadio", "1234");
		Statement st = conn.createStatement();
		st.executeUpdate("INSERT INTO WORKFLOW_ARCHIVAGE (Ordre, Date, Direction_Service, Ordonnateur, Nom_document, "
			+ "Justificatif_sortie, Description_document, Duree_probable, Observations) VALUES "
			+ "(3, 1, 'Informatique', 'User Test', 'Test', 'Un test', 'Essai', '2j', 'RAS')");
		conn.close();
	}
}