Workflow guard scripts#
sysman sysadmin
You can assign a workflow guard script to state transitions in workflows. This script runs
when an item is loaded for editing
when it is saved with the state transition the script is assigned to
In the first case, the guard script thus influences the states made available in the item’s state selection list during editing. In the second case, the guard script can block the desired state transition.
A guard script can perform arbitrary checks and returns either “true” or “false”.
Examples#
The following script is attached in the workflow to the transition from the initial state (black circle) to the first regular state. It prevents automatically generated e-mails, such as out-of-office messages, from creating an item in the system.
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import com.aurel.track.admin.customize.scripting.BINDING_PARAMS;
import com.aurel.track.admin.customize.scripting.GroovyScriptExecuter;
import com.aurel.track.util.emailHandling.EmailCleanUtils;
public class EmailGuardScript {
private static String PARAMETER_SCRIPT_NAME = "EmailGuardParams";
private static String PARAMETER_SCRIPT_METHOD = "getRejectPatterns";
public Map<String, Object> handleEvent(Map<String, Object> inputBinding) {
String subject = (String)inputBinding.get(BINDING_PARAMS.EMAIL_SUBJECT);
if (subject!=null) {
List<Pattern> rejectPatterns =
(List<Pattern>)GroovyScriptExecuter
.getParameterInstanceGroovyHandler(
PARAMETER_SCRIPT_NAME, PARAMETER_SCRIPT_METHOD);
boolean patternFound = EmailCleanUtils.patternFound(subject,
rejectPatterns);
inputBinding.put(BINDING_PARAMS.GUARD_PASSED, !patternFound);
}
return inputBinding;
}
}
The following parameter script, which belongs to the script above, shows how you can separate logic and configuration from each other:
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import com.aurel.track.util.emailHandling.EmailCleanUtils;
public class EmailGuardParams {
private EmailGuardParams() {
List<String> rejectStrings = new LinkedList<String>();
/******************Reject subject strings************************/
// the regEx which if found in email subject no item/comment will
// be added
rejectStrings.add("Undelivered Mail Returned to Sender");
rejectStrings.add("außer Haus");
rejectStrings.add("out of.*?house");
rejectStrings.add("out of.*?office");
rejectStrings.add("be out of the office");
/***********Do not modify after this line **********************/
rejectPatterns = EmailCleanUtils.compilePatterns(rejectStrings);
}
private static EmailGuardParams instance = null;
private static List<Pattern> rejectPatterns;
public static EmailGuardParams getInstance() {
if (instance==null) {
instance = new EmailGuardParams();
}
return instance;
}
public static List<Pattern> getRejectPatterns() {
return rejectPatterns;
}
}