An Enum can be used in all those scenarios where it is useful to have constant information directly on the model, such as a list of states or classifications. In the model, constants defined in Enums are globally available to all other classes, which may have associations to a particular Enum class or reference the values of individual literals in math expressions, in WHERE conditions on queries, or filters at the Application schema level.
Create an Enum #
Right-click on a free spot in the workspace (Canvas) to open the
Database menu; from the drop-down menu choose
New enum class. Alternatively, click on the palette button(
Create a new enum).
name will be created automatically.
A literal is a constant object with global visibility; in the simplest form it is a string, or more precisely an object with a single
name attribute of type
string, with a unique uppercase value in the context of the Enum class. The
name attribute is mandatory, since it identifies the literal itself in the list of literal objects, and is always Object title.
Add a literal #
Right-click on an Enum class to open its
Class menu and select
New literal; once the literal is added, the count of total objects in the class will increase by one.
Multi-attribute literals #
Through literals it is possible to represent structured information by defining other attributes on the Enum class (besides
All platform attributes and all native attributes except
File are allowed, while derived attributes are disabled. By construction, it is not possible to declare unique constraints, domain restrictions or indexes on Enum attributes, nor set them as required; the only required attribute is
Modifying literals #
The literals fields can be filled in through a dedicated editor. To open it, you need to open the
Class menu of the Enum by right-clicking on it, and select the
Edit literal objects option.
In the example shown in the figure we have added the attribute
discount_percentage to the Enum Discount_code. The values of this attribute for the two literals in the class were changed by filling in the fields in the table visible on the right.
Referencing an Enum #
A first way to use an Enum in the model is to create associations towards it, so you can associate an object to one of the declared literals. The only relationships allowed with Enum classes are unidirectional associations, with the Enum as target; thus only roles towards the Enum are navigable, with Exactly one (
1) or Zero or one (
It is also possible to refer to Enum literals in expressions with the following syntax:
<EnumClassName>[LITERAL_NAME].name. The (
Enum constants) in the Expression editor shows the list of all Enums in the model, and for each of them, the list of literals; for each literal, you can then select the attributes declared on the Enum, as well as all of its platform attributes. This allows you to remove hardwired values from expressions and instead refer to information directly available in the model.
Enum in the generated application #
In the web client, the default representation for literals equipped with only the
name attribute is a Dropdown menu with the names of the literals as label; if new attributes are added to the Enum, the representation automatically changes to a Single-row table.
Enum with a single attribute #
Let’s see an example of an Enum class: we have modeled a shipment (Shipping) and we want to be able to control its progress. To represent the possible states of the shipment we could have used native attribute; however, since it is static information (that therefore will not be modified at database level) it is more effective to bind this information to the model through an Enum. We therefore created the Enum class Shipping_status with the literals
status points to the attribute
name, which has the names of the literals as values.
The math attribute
/completed: boolean simply indicates whether the shipment was completed or not by referencing via square brackets a literal in its expression:
Shipment_state[DELIVERED]. name=state.. This way, when
DELIVERED as its value it is
TRUE, and therefore the shipment is completed.
In the next figure,
/completed has been selected; the Designer shows the attributes and the literal used to calculate it circled in red.
Enum with multiple attributes #
Let’s now look at the example of a slightly more complex Enum. We have modeled a set of discount codes (Discount_code) that can be applied to the value of a sale (Sale).
/total_price attribute is calculated by summing the values found by the query pointing at the price (
price) of the products (Product). The
discount_percentage attribute, added to the Discount_code Enum, indicates the percentage discount applied. We created a query on Sale that points to this attribute, so we can use it to calculate the final price. The association to Discount_code is Zero or one (
01) because the discount code can also not be applied.
To obtain the final discounted price, we finally created a math attribute,
total_discounted_price, which multiplies the total price of the sale by the percentage discount applied, through the expression
total_price * discount_percentage.