Creating your own Automation Action

You can create your own Automation Action.

Conditions:

  • The new Automation Action class must implement the "Action" interface.
  • The new Automation Action class must be declared under the package "com.openkm.automation.action".

The new Automation Action class must be within the OpenKM source code. You might be interested in OpenKM portable dev environment for it.

Validation interface:

package com.openkm.automation;

import java.util.HashMap;

public interface Action {
	public static final String METHOD_PRE = "executePre";
	public static final String METHOD_POST = "executePost";
	
	public void executePre(HashMap<String, Object> env, Object... params);
	public void executePost(HashMap<String, Object> env, Object... params);
}

Methods description

MethodTypeDescription

executePre(Map<String, Object> env, Object... params)

void

The method executed by the Automation event when the validation conditions succeed, on "pre" stage.

executePost(Map<String, Object> env, Object... params)

void

The method executed by the Automation event when the validation conditions succeed, on "post" stage.

Understanding env variable

The env variable is present on isValid method, it is a Map of values injected by automation. These map values provide information about the node involved on the event and other related information.

It takes some time, until you take control of the AutomationUtils class, because they are centralized, the methods to retrieve data.

 For example to retrieve the uuid of the node that caused the event.

String uuid = AutomationUtils.getUuid(env);

Understanding params variable

The env variable is present on isValid method, it is an array of Objects. This array is filled by Automation event based on the number of parameters set on Validation class.

The array can have serveral Objects with a distinct type, it is necessary retrieve the parameters to the correct object type.

It takes some time, until you take control of AutomationUtils class, because they are centralized, the methods to retrieve parameters.

For example to retrieve the first parameters as String object.

String param00 = AutomationUtils.getString(0, params);

Preventing recursion

When you create your automation actions, should consider recursion case. For example if your action goes linked with "CREATE_DOCUMENT" event and into the action you create a new document you can go into an infinite loop. For it when you create a new Action should take special care of this cases.

Basic recursive samples:

  • Automation task linked with "Create document" event when into is set an action that also creates a new document.
  • Automation task linked with "Set metadata group" event when into is set an action that also changes metadata group values.

Complex recursive sample:

  • Automation task linked with "Create document" event when into is set an action that changes metadata group values and automation task linked with "Set metadata group" event when into is set an action that creates a new document.

Register

Once you have created the new Automation Action, build the OpenKM.war file and deployed into the tomcat, it must be registered into the OKM_AUTO_METADATA table;

The OKM_AUTO_METADATA colums description:

ColumnDescription

AMD_ID

Autoincremental unique Id

AMD_AT

Indicates when the Automation Action can be executed, the values are:

  • "pre" indicate can be execute inmediatelly before the action who has fired it.
  • "post" indicate can be execute after the action who has fired it.

AMD_CLASS_NAME

Java class name.

AMD_DESC00

Description of the parameter 0.

AMD_DESC01

Description of the parameter 1.

AMD_DESC02

Description of the parameter 2.

AMD_GROUP

Type of automation, allowed values are:

  • "action".

AMD_NAME

Name.

AMD_SRC00

Source, allowed values are:

  • "" default value.
  • "okm:folder" indicate the value is a folder uuid.

When type "okm:folder" is applied will be shown a folder selector popup.

AMD_SRC01

Source, allowed values are:

  • "" default value.
  • "okm:folder" indicate the value is a folder uuid.

When type "okm:folder" is applied will be shown a folder selector popup.

AMD_SRC02

Source, allowed values are:

  • "" default value.
  • "okm:folder" indicate the value is a folder uuid.

When type "okm:folder" is applied will be shown a folder selector popup.

 AMD_TYPE01

 Allowed type of values are:

  • "" default value.
  • "text".
  • "textarea".
  • "integer".
  • "boolean".

The type will take effect on widget shown from UI.

 AMD_TYPE02

Allowed type of values are:

  • "" default value.
  • "text".
  • "textarea".
  • "integer".
  • "boolean".

The type will take effect on widget shown from UI.

AMD_TYPE03

Allowed type of values are:

  • "" default value.
  • "text".
  • "textarea".
  • "integer".
  • "boolean".

The type will take effect on widget shown from UI.

In case your Automation Action can be executed from "pre" and "post" stages, you must do an insert for each one.

Example

AddKeyword class:

package com.openkm.automation.action;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.automation.Action;
import com.openkm.automation.AutomationUtils;
import com.openkm.core.Config;
import com.openkm.dao.NodeBaseDAO;

public class AddKeyword implements Action {
	private static Logger log = LoggerFactory.getLogger(AddKeyword.class);
	
	@Override
	public void executePre(HashMap<String, Object> env, Object... params) {
	}
	
	@Override
	public void executePost(HashMap<String, Object> env, Object... params) {
		String keyword = AutomationUtils.getString(0, params);
		String uuid = AutomationUtils.getUuid(env);
		
		try {
			if (uuid != null) {
				if (Config.SYSTEM_KEYWORD_LOWERCASE && keyword != null) {
					keyword = keyword.toLowerCase();
				}
				
				NodeBaseDAO.getInstance().addKeyword(uuid, keyword);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}

Go to Administration > Database query and execute the sql:

INSERT INTO OKM_AUTO_METADATA (AMD_AT, AMD_CLASS_NAME, AMD_NAME, AMD_GROUP, AMD_TYPE00, AMD_SRC00, AMD_DESC00, AMD_TYPE01, AMD_SRC01, AMD_DESC01) VALUES ('post', 'com.openkm.automation.action.AddKeyword', 'AddKeyword', 'action', 'text', '', 'Keyword', '', '', '');

Depending on your database the query might have minimal changes.

Oracle:

INSERT INTO OKM_AUTO_METADATA (AMD_ID, AMD_AT, AMD_CLASS_NAME, AMD_NAME, AMD_GROUP, AMD_TYPE00, AMD_SRC00, AMD_DESC00, AMD_TYPE01, AMD_SRC01, AMD_DESC01) VALUES (HIBERNATE_SEQUENCE.nextval, 'post', 'com.openkm.automation.action.AddKeyword', 'AddKeyword', 'action', 'text', '', 'Keyword', '', '', '');

PostgreSQL:

INSERT INTO OKM_AUTO_METADATA (AMD_ID, AMD_AT, AMD_CLASS_NAME, AMD_NAME, AMD_GROUP, AMD_TYPE00, AMD_SRC00, AMD_DESC00, AMD_TYPE01, AMD_SRC01, AMD_DESC01) VALUES (nextval('hibernate_sequence'), 'post', 'com.openkm.automation.action.AddKeyword', 'AddKeyword', 'action', 'text', '', 'Keyword', '', '', '');

The OKM_AUTO_METADATA colums values for AddKeyword Automation Action:

ColumnDescription

AMD_ID

Unique Id

AMD_AT

"post"

AMD_CLASS_NAME

"com.openkm.automation.action.AddKeyword"

AMD_DESC00

"Keyword"

AMD_DESC01

""

AMD_DESC02

""

AMD_GROUP

"action".

AMD_NAME

"AddKeyword".

AMD_SRC00

"".

AMD_SRC01

"".

AMD_SRC02

"".

 AMD_TYPE01

"text".

 AMD_TYPE02

"".

AMD_TYPE03

"".