Virtual Properties in CRUD actions#

Since version 4.2.3, Portofino supports virtual properties in its CRUD pages. These are properties that are not directly derived from the CRUD's table (itself derived from the configured query); they are, instead, obtained from an OGNL expression defined by the user. OGNL is an "object navigation language" and as such it supports both reading and writing object properties, arbitrarily nested. It supports navigating collections, conditionals and other nontrivial constructs, so its expressiveness is quite high. Of course, caution is advised with such a powerful tool - loading half of the database in memory is just a .fk_to_huge_table away. With that in mind, virtual properties can cover some advanced customisations that previously were possible only by extending CrudAction with great expertise. The typical case, which has been asked several times by users on the mailing list, is to include in a CRUD page a property from a related table. Virtual properties have to be configured by hand in the CRUD's configuration.xml with the format:
<virtual-property type="java.lang.String" name="myProperty" expression="fk_other_table.other_property" />

The type is mandatory since Portofino cannot infer it.

The expression is the OGNL expression to evaluate. The root object against which properties are resolved is the object loaded from the database, so you can access its properties and navigate its relationships - but you cannot reach other virtual properties or user-defined selection providers.

Virtual properties are not searchable and not sortable.

Since the order of a CRUD's fields depends on the order of properties in the model and virtual properties are not present in the model, by default they end up last in the crud page. However, a new attribute has been added to the CRUD configuration top level element, useLocalOrder, which when set to true will ignore the order of properties in the model and use the one in the configuration.xml file:

<configuration useLocalOrder="true" ...>
...
</configuration>

Note 1: since the OGNL expression is evaluated on the object(s) loaded from the database, you can add whatever property you want to said object(s) using the existing CRUD extension points and refer to it in your virtual properties.

Note 2: unfortunately, OGNL lacks a handy null-safe navigation operator like Groovy's elvis (?.). It does, however, support Java's ternary operator (condition ? true-branch : false-branch), which is a verbose way to write the same thing.

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-3) was last changed on 21-Jun-2017 23:14 by alessio.stalla@manydesigns.com