Studio

HOWTO: Prepopulate Metadata at Creation

Updated: April 3, 2024

This tutorial provides step by step to set property values automatically when a user opens the document creation layout.

In many cases, it can be convenient to initialize a document with a title, or other metadata:

  • A document title can hold a counter which is automatically calculated
  • A document property can be filled according to the document location
  • Another property can be filled from the current date or the user information

Useful Functions

To achieve this, we can use several useful functions. Here is a non exhaustive list of the recurrent functions:

Function Use Case Example
CurrentDate.* Set information related to the current date. It can calculates dates before or after the current date. @{CurrentDate.format("ddMMyyyy")}, @{CurrentDate.days(7)}, @{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}
CurrentUser.* Set information related to the current user. It can include the user firstname, lastname or any custom properties. @{CurrentUser.email}
Fn.getNextId(String) Obtain a unique value for the given key. Each time this function is called using the same key, a different string will be returned. Fn.getNextId("myCustomCounter")
@{Fn.getVocabularyLabel("voc", "key")} Get the the label associated to the given key from the given vocabulary, instead of its ID. @{Fn.getVocabularyLabel("status", "in_progress")}

Configuration Steps

To initialize metadata, we must:

  • Define an Automation Chain or Automation Scripting that assigns a value to the metadata.
  • Define an Event Handler that executes our automation chain/scripting when an empty document is created.

In this example, we will work with a type of document called Tender (Tender being the document type label, TEND as document type id), which has an associated schema called tend, which contains only one metadata called ref (String).

We want to initialize the reference at the moment of showing the user the creation form. The refinance must follow the following pattern: TEND-XXXXXX, where:

  • TEND: Document type
  • -: Fixed string
  • XXXXXX: Unique reference number

Create the Document Type

  1. In Modeler, go to Content Model > Document Types and create a new document type:
    • Feature ID: TEND
    • Label: Tender
  2. On the Schema tab, add a new field ref as a string.
  3. Save your changes.

Create the Automation Chain

In Nuxeo Studio Modeler > Automation > Automation Chains, create a new automation chain called AC_Tender_InitTenderAttributes, which includes the following operations:

- Context.FetchDocument
- Document.SetProperty:
    xpath: "tend:ref"
    save: "false"
    value: "@{Document.type}-@{Fn.getNextId (\"TEND\")}"

Create the Event Handler

Next, create an Event Handler from the Nuxeo Studio Modeler > Automation > Event Handlers that is responsible for executing the automation chain when the document is created as an "Empty Document".

We have to establish the following values in the configuration screen:

  • Events: Empty document created
  • Event handler activation:
    • Current document has one of the types: TEND
    • Current document has facet: Any
  • Current document is: Regular Document
  • Event handler execution: AC_Tender_InitTenderAttributes

Document Type Layouts

Generate the Tender layouts in Nuxeo Studio Designer > Layouts > Local Document Types and click on Configure Missing Layouts.

Once the previous steps are completed, you can deploy your changes. When a new TEND document, the reference field has a default value that varies each time.

Going Further

Our reference uses a correlative number (1, 2, 3...), so the references generated are TEND-1, TEND-2, TEND-3... But, what happens if we want our reference to always have the same length?

We just have to add a couple of calls to Java objects to format our correlative number with zeros to the left. Thus our references will have this aspect: TEND-0000004, TEND-0000005, TEND-0000006...

Fetch> Context.FetchDocument
Document> Document.SetProperty
XPath: tend: ref
save: false
value: TEND-@{java.lang.String.format("% 07d", java.lang.Integer.parseInt (Fn.getNextId ("TEND")))}

Integer.parseInt converts the identifier to integer, since Fn.getNextId returns a string. On the other hand the call to java.lang.String.format filled with zeros to the left to a maximum of 7 positions. When we use a java object in MVEL expressions we must use the name of the object including the name of the package in which it is contained to avoid possible conflicts.