In this step, we will see how we can trigger an automated action when a document is created. The goal is to prevent users other than the creator to edit the document until he decides it is ok to release it.
Although there are several options, we choose the locking one. We will create an automation chain to lock the document for every users but its creator. An event handler will trigger this automation chain whenever a new documentation item is created.
- In Studio, go to Automation > Event Handlers.
- Click on New to create a new event handler.
- Give it the ID
AutoLockOnItemCreationand click on Next. Ignore the validation error for now: it is here because we have not yet defined what our event handler will trigger.
- In the Event Handler Definition section, select the event "Document Created".
- In the Event Handler Activation section, select the
DocumentationItemdocument type. Hold the "ctrl" key to select several document types.
- In Current Document is, choose "Regular Document", to make sure it is only fired for usual documents. You do not want it when creating a proxy (used by the publishing system) for instance.
- In the Event Handler Execution section, click on Create.
- Call the automation chain
AutoLockOnItemChain. You are now displayed the automation chain definition screen, in Automation > Automation Chains.
The automation chain will leverage the operation that is called Document > Lock. This operation needs a "document" in input and returns a document. In the mean time the operation locked the input document for the current user.
The newly created document is put in the context as well as the user who triggered the automation chain. Because the the chain is triggered at the creation of the document, this user is obviously the creator.
Since we have everything required by the chain provided by default, our automation chain will be very simple.
- Leave the default Fetch > Context Document. It will create an input document (the newly created document) from the context for the next operation.
- Add the operation Document > Lock.
- Leave the owner parameter empty
- Click on Save.
We now want to test what we did.
- Update your Nuxeo Platform instance with the latest changes from Studio.
- Create a new Technical Documentation Item document.
It is locked and you see that it is locked by the creator of the document (Administrator is the example below).
You now have documents that cannot be modified by someone else than the creator of the document. You have added you first business logic thanks to Studio.
To sum up what we saw:
- Any automated action is done through automation chains.
- Automation chains are an aggregation of atomic operations.
- Automation chains can be triggered by several means. We did it through an event captured by a Event Handler.