Tutorials

Document Locking Right After its Creation

Updated: March 18, 2024

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.

Creating the Event Handler

  1. In Studio, go to Automation > Event Handlers.
  2. Click on New to create a new event handler.
  3. Give it the ID AutoLockOnItemCreation and click on Next. Ignore the validation error for now: it is here because we have not yet defined what our event handler will trigger.
  4. In the Event Handler Definition section, select the event "Document Created".
  5. In the Event Handler Activation section, select the DocumentationItem document type. Hold the "ctrl" key to select several document types.
  6. 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.
  7. In the Event Handler Execution section, click on Create.
  8. Call the automation chain AutoLockOnItemChain. You are now displayed the automation chain definition screen, in Automation > Automation Chains.

Defining the Automation Chain

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.

  1. Leave the default Fetch > Context.FetchDocument. It will create an input document (the newly created document) from the context for the next operation.
  2. Add the operation Document > Document.Lock.
  3. Leave the owner parameter empty
  4. Click on Save.

Testing Your Changes

We now want to test what we did.

  1. Update your Nuxeo Platform instance with the latest changes from Studio.
  2. 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).

Congratulations

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.