An Action is a unit of work that can be carried out on a node. Actions are commonly used in conjunction with Rules, but that is not mandatory. When you create Rules for a folder, you can specify certain Actions to occur to nodes added to the folder. For example, when a Word document is added to a folder, you may want a PDF to be automatically generated, or a notification email to be sent. There are a number of built-in Actions available by default (there IDs in parentheses):
- Execute Script (script)
- Copy (copy)
- Move (move)
- Checkin (check-in)
- Checkout (check-out)
- Link to category (link-category)
- Add Aspect (add-features)
- Remove Aspect (remove-features)
- Add simple workflow (simple-workflow)
- Start workflow (start-workflow)
- Cancel workflow (cancel-workflow)
- Send email (mail)
- Transform and copy content (transform)
- Transform and copy image (transform-image)
- Extract common metadata fields (extract-metadata)
- Import (import)
- Export (export)
- Specialise type (specialise-type)
- Create version (create-version)
- Increment counter (counter)
- Set property value (set-property-value)
- Create thumbnail (create-thumbnail)
- Execute all rules on node (execute-all-rules)
You can also create custom Actions to do whatever you want when content is added to a folder.
While Actions are typically triggered by Rules (see Folder rules), you can also invoke them directly by selecting them from a menu item. The ActionService also allows you to call them directly from code. Any piece of code that can access the ActionService can invoke the Action, for example:
- JavaScript
- Workflow
- Web script
- Java
The following example shows how to invoke the Add Aspect out-of-the-box action with the ActionService:
boolean executeAsync = true;
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, "cm:titled");
Action addAspectAction = serviceRegistry.getActionService().createAction("add-features", params);
if (a != null) {
serviceRegistry.getActionService().executeAction(addAspectAction, docNodeRef, true, executeAsync);
} else {
thrownew RuntimeException("Could not create add aspect action");
}
Another example of how to execute a script:
boolean executeAsync = true;
// JavaScript file node reference
NodeRef scriptRef = ...
Action action = serviceRegistry.getActionService().createAction("script");
action.setParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF, scriptRef);
if (action != null) {
serviceRegistry.getActionService().executeAction(action, docNodeRef, true, executeAsync);
} else {
thrownew RuntimeException("Could not create execute script action");
}
In this example we invoke a custom action with the Spring bean id send-as-email (more information about this repository action implementation can be found in Actions:
import org.alfresco.service.cmr.action.Action;
public void sendEmailWithDoc(String to, String subject, String bodyText, NodeRef docNodeRef) {
boolean executeAsync = true;
Map<String, Serializable> aParams = new HashMap<String, Serializable>();
aParams.put("to", to);
aParams.put("subject", subject);
aParams.put("body_text", bodyText);
Action a = serviceRegistry.getActionService().createAction("send-as-email", aParams);
if (a != null) {
serviceRegistry.getActionService().executeAction(a, docNodeRef, true, executeAsync);
} else {
throw new RuntimeException("Could not create send-as-email action");
}
}
See also: