Difference between revisions of "Extend automation 6.0"

From OpenKM Documentation
Jump to: navigation, search
m (Jllort moved page Extend automation to Extend automation 6.0: implementation change)
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
OpenKM automation by default is not enabled to enable it you should execute some sql queries to enable it.  
+
Refer to [[Enable automation]] section to know how enable automation. By default automation is not enabled.
  
Refer to [[Automation]] section to configure automation rules in your OpenKM.
+
Refer to [[Automation]] section to configure automation rules in your OpenKM and get full explanation of available validators and actions.  
  
Refer to [[Extend automation]] to extend automation validators and actions.
+
=== Create a new validator ===
 +
Any validator should implements interface Validation:
  
[[File:Okm_admin_074.png|center]]
+
<source lang="java">
 +
public interface Validation {
 +
public static final String METHOD = "isValid";
 +
 +
boolean isValid(HashMap<String, Object> env, Object... params);
 +
}
 +
</source>
  
== Preliminar concepts ==
+
* At HashMap<String, Object> env are stored element values map.
* Rule is composed by validators and actions.
+
* Values set at validator form are stored to String params[].
* Validators are one or more conditions on a rule that are evaluated depending the elements which fired the automation event ( document, folder, etc... )
 
* Actions are some operations with element which fired the automation event ( document, folder, etc... )
 
* Rules is executed when all validators in a rule return true.
 
  
== Add new rule ==
+
{{Note|Refer to AutomationUtils class to get variables. Take a look at the latest doxygen class reference at [[Developer_Guide]].}}
Click [[File:new.png]] will be shown a screen like this:
 
  
[[File:Okm_admin_075.png|center]]
+
<source lang="java">
 +
public class PathContains implements Validation {
 +
private static Logger log = LoggerFactory.getLogger(PathContains.class);
 +
 +
@Override
 +
public boolean isValid(HashMap<String, Object> env, Object... params) {
 +
String path = AutomationUtils.getString(0, params);
 +
String parentPath = AutomationUtils.getParentPath(env);
 +
 +
try {
 +
if (parentPath.startsWith(path)) {
 +
return true;
 +
} else {
 +
return false;
 +
}
 +
} catch (Exception e) {
 +
log.error(e.getMessage(), e);
 +
}
 +
 +
return false;
 +
}
 +
}
 +
</source>
  
* '''Order''' field indicates execution preference among the other rules.
+
=== Create a new action ===
* '''Name''' field is some name to identify the rule.
+
Any validator should implements interface Action:
* '''Event''' field indicates on which type of events is evaluted the rule.
 
* '''At''' field has two possible values post or pre. '''post''' indicates execution before element is created at OpenKM, '''pre''' indicates execution when element is yet created. With example will be more clear, with '''post''' we can change the path where a document will be created, with '''pre''' the document is yet created but we can move to other path.
 
* '''Exclusive''' field enabled stop the evaluation is rule is executed. Automation can have several rules, one or more can be evaluated and executed at same time ( in order defined by field '''Order''' ), but if a rule with exclusive field enabled is executed then automation stops here ( it's similar concept than firewall rules ).
 
  
 +
<source lang="java">
 +
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);
 +
}
 +
</source>
 +
 +
 +
* At HashMap<String, Object> env are stored element values map.
 +
* Values set at validator form are stored to String params[].
 +
 +
{{Note|Refer to AutomationUtils class to get variables. Take a look at the latest doxygen class reference at [[Developer_Guide]].}}
 +
 +
<source lang="java">
 +
public class ExecuteScripting implements Action  {
 +
private static Logger log = LoggerFactory.getLogger(ExecuteScripting.class);
 +
 +
@Override
 +
public void executePre(HashMap<String, Object> env, Object... params) {
 +
}
 +
 +
@Override
 +
public void executePost(HashMap<String, Object> env, Object... params) {
 +
String script = AutomationUtils.getString(0, params);
 +
NodeBase node = AutomationUtils.getNode(env);
 +
String uuid = AutomationUtils.getUuid(env);
 +
 +
try {
 +
Interpreter i = new Interpreter();
 +
i.set("node", node);
 +
i.set("uuid", uuid);
 +
i.eval(script);
 +
} catch (Exception e) {
 +
log.error(e.getMessage(), e);
 +
}
 +
}
 +
}
 +
</source>
  
  
 
[[Category: Extension Guide]]
 
[[Category: Extension Guide]]

Latest revision as of 18:33, 7 September 2013

Refer to Enable automation section to know how enable automation. By default automation is not enabled.

Refer to Automation section to configure automation rules in your OpenKM and get full explanation of available validators and actions.

Create a new validator

Any validator should implements interface Validation:

public interface Validation {
	public static final String METHOD = "isValid";
	
	boolean isValid(HashMap<String, Object> env, Object... params);
}
  • At HashMap<String, Object> env are stored element values map.
  • Values set at validator form are stored to String params[].

Nota clasica.png Refer to AutomationUtils class to get variables. Take a look at the latest doxygen class reference at Developer_Guide.

public class PathContains implements Validation {
	private static Logger log = LoggerFactory.getLogger(PathContains.class);
	
	@Override
	public boolean isValid(HashMap<String, Object> env, Object... params) {
		String path = AutomationUtils.getString(0, params);
		String parentPath = AutomationUtils.getParentPath(env);
		
		try {
			if (parentPath.startsWith(path)) {
				return true;
			} else {
			 return false;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		
		return false;
	}
}

Create a new action

Any validator should implements interface Action:

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


  • At HashMap<String, Object> env are stored element values map.
  • Values set at validator form are stored to String params[].

Nota clasica.png Refer to AutomationUtils class to get variables. Take a look at the latest doxygen class reference at Developer_Guide.

public class ExecuteScripting implements Action  {
	private static Logger log = LoggerFactory.getLogger(ExecuteScripting.class);
	
	@Override
	public void executePre(HashMap<String, Object> env, Object... params) {
	}
	
	@Override
	public void executePost(HashMap<String, Object> env, Object... params) {
		String script = AutomationUtils.getString(0, params);
		NodeBase node = AutomationUtils.getNode(env);
		String uuid = AutomationUtils.getUuid(env);
		
		try {
			Interpreter i = new Interpreter();
			i.set("node", node);
			i.set("uuid", uuid);
			i.eval(script);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}