Calculated fields
A calculated field is a field that receives a value based on a given formula. The given formula can be specified as an expression in the spring expression language (SpEL), making use of various helper functions.
You turn another base type into a calculated field by supplying the formula
attribute.
The actual field value is still stored as the original base type.
Calculation is supported for the following field types:
Field values are only calculated when saving a document. See also the section on using documents in code.
Value calculation
A single value type with a formula will be visualised as a read-only control.
document-definition:
name: store
content:
- id: item
label: "name"
type: "String"
- id: amount-items
label: "# items"
type: number
- id: buy-value
type: number
- id: sell-value
type: number
- id: profit-per-item
type: number
formula: "$(sell-value) - $(buy-value)" (1)
1 | Supply the formula that represents the value of the field.
In this case, the value is the difference between the sell-value and the buy-value |
Accessing field values
A formula
is a SpEL expression that is executed against an EvaluationContext
.
The context allows accessing values of the document fields.
Accessing a field value can be done with the using a shorthand $()
syntax.
Some examples:
$(someField)
$(someField.nestedField)
$(someField.nestedField:0)
$(fieldsetCollection[0].someField)
$(fieldsetCollection[].someField)
This can be used to calculate values across all collection fields.
For example sum($(orderLines[].items[].count))
would calculate the total of the count
field of all items across all order lines.
$(.someField)
This is especially useful for calculated fields in fieldsets that are part of a collection.
$(..someField)
Available functions
Various helper functions are available for defining your formula
.
These functions are implemented in DynamicDocumentFormulaFunctions
.
The following table lists all functions that are available by default.
Method | Parameters | Remarks |
---|---|---|
|
Collection of objects |
Returns how many items are present in the given collection. |
|
Collection of objects |
Returns how many items are present in the given collection that differ from |
|
Collection of objects |
Returns how many |
|
Collection of objects |
Flatten an array of objects into a single collection with all |
|
Collection of objects |
Flatten an array of objects into a stream.
Recursively flattens any member that is also an array or collection.
Unlike |
|
Collection of objects |
Sort a collection of objects (in natural order). |
|
Collection of objects |
Reverse a collection of objects. |
|
Collection of objects |
Get the first item from a collection.
If the collection is empty, |
|
Collection of objects |
Get the last item from a collection.
If the collection is empty, |
|
Collection of numeric values |
Returns the smallest value in the collection. Supports nested collections of numeric values and will recurs through them. |
|
Collection of numeric values |
Returns the largest value in the collection. Supports nested collections of numeric values and will recurs through them. |
|
Collection of numeric values |
Returns the sum of the values in the collection Supports nested collections of numeric values and will recurs through them. |
|
Collection of numeric values |
Returns the average of the values in the collection. Supports nested collections of numeric values and will recurs through them. |
|
Collection or array of objects |
Returns whether the collection or array contains at least one item. |
|
Collection or array of objects |
Returns whether the collection or array contains zero elements. |
|
Collection of boolean values |
Returns whether the provided values are all true |
|
Collection of boolean values |
Returns whether one of the provided values is true |
|
Collection of textual |
Returns the concatenated result of the provided values |
|
Separator and a collection of textual values |
Returns a single text that joins the provided textual values wit the provided separator. |
Customizing the evaluation context
It is possible to customize the evaluation context for calculated fields on a document level. This can be used to add additional helper components or for example to enable direct access to beans.
See the separate section for details.