Workflow utilities

From OpenKM Documentation
Jump to: navigation, search

Workflow logger

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

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);
	}
}
-- LOGGER
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');