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

Executes the SQL sentences from the OpenKM administration database query feature.

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

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 transaction

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();
	}
}