MGP-001: Expressions Cheat Sheet

modeling_guideline
Tags: #<Tag:0x00007f64bad35538>

#1

MGP-001 | 2017-12-05 | v1.1

The edoras one backend is a Java application, based on Spring. For that reason, Backend Expressions are formed like this: #{your_expression}. This allows you to call most static and instance methods of the Java programming language. For more information, see here.

The frontend is an AngularJS application which uses expressions wrapped in double curly braces: {{your_expression}}. You can use most JavaScript functions. There are a number of online references which can be consulted, for instance the Mozilla Developer Network. For instance, the documentation about Numbers not only describes important principles but also lists many useful functions which can be applied to functions.

In general, the following rule applies: Use Frontend Expressions in forms, everywhere else use Backend Expressions.

Common Tasks in Frontend and Backend Expressions

Both types of expressions offer similar capabilities. The following table shows how to achieve some of the most common tasks.

Description product is equal to selectedProduct
Frontend {{product == selectedProduct}}
Backend #{product == selectedProduct}

 

Description amount is equal to 0 and it is made sure that amount is a number
Frontend {{amount === 0}}
Backend #{amount == 0}

 

Description product is equal to the string 'abc'
Frontend {{product == 'abc'}}
Backend #{product == 'abc'}

 

Description product is not equal to 'abc'
Frontend {{product != 'abc'}}
Backend #{product != 'abc'}

 

Description amount is less than 1000
Frontend {{amount < 1000}}
Backend #{amount < 1000}

 

Description amount is less or equal to 1000
Frontend {{amount <= 1000}}
Backend #{amount <= 1000}

 

Description amount is greater than 1000
Frontend {{amount > 1000}}
Backend #{amount > 1000}

 

Description amount is greater or equal to 1000
Frontend {{amount >= 1000}}
Backend #{amount >= 1000}

 

Description product has a value (exists)
Frontend {{product}} or {{product != null}}
Backend #{product != null}

 

Description product was defined
Frontend {{product !== undefined}}
Backend #{product != null}

 

Description product has no value / is false / is falsy
Frontend {{!product}}
Backend #{product == null || product == false}

 

Description Boolean value isGoldClient is true
Frontend {{isGoldClient}} or {{isGoldClient == true}}
Backend #{isGoldClient} or #{isGoldClient == true}}

 

Description Boolean value isGoldClient is false
Frontend {{!isGoldClient}} or {{isGoldClient == false}}
Backend #{!isGoldClient} or #{isGoldClient == false}

 

Description Logical And (is true if all variables evaluate to true)
Frontend {{isGoldClient && income > 7000}}
Backend #{isGoldClient && income > 7000} or #{allOf(isGoldClient, income > 7000)}

 

Description Logical Or (is true if one of the variables evaluate to true)
Frontend {{isGoldClient||income > 12000}}
Backend {anyOf(isGoldClient||income > 12000)}

 

Description Check if map / object client contains a variable clientId
Frontend {{client.hasOwnProperty('clientId')}}
Backend #{client.containsKey('clientId')}

 

Description Safely retrieve variable clientId on map / object client
Frontend {{client.name || ''}}
Backend #{client.name}

 

Description Get the number of elements in suppliers
Frontend {{suppliers.length}}
Backend #{suppliers.size()}
Description Get the index (position) of clientId in the clients array (Array) or list (Backend)
Frontend {{clients.indexOf(clientId)}}
Backend #{clients.indexOf(clientId)}

Frontend Expressions

This section offers a number of hints and a complete reference of AngularJS expressions can be found here.

Filters

Filters are a way how data can be formatted. There are a few built-in filters and it is possible to add more in the custom.js file in your backend.

Filter Date
Description Displays a date with a specified format.
Syntax {{date_expression|date : format : timezone}}
Output {{root.creationDate|date:'yyyy-MM-dd HH:mm:ss'}}
Example 2017-11-27 13:44:00

 

Filter Currency
Description Displays a currency along with its symbol and with a specified fraction size.
Example Input {{currency_expression|currency : symbol : fractionSize}}
Example Output {{root.price|currency:'CHF' :0}}
Example 200 CHF

 

Filter Number
Description Displays a number with a specified fraction size. Uses the locale of the client.
Syntax {{number_expression|number : fractionSize}}
Example Input {{root.quantity|number:2}}
Example Output 222.22

 

Filter Lowercase
Description Converts a string to lowercase.
Syntax {{ lowercase_expression|lowercase}}
Example Input {{root.name|lowercase}}
Example Output my casename

 

Filter Uppercase
Description Converts a string to uppercase.
Syntax {{ upercase_expression|uppercase}}
Example Input {{root.name|uppercase}}
Example Output MY CASENAME

 

Filter Limit
Description Limits the number of elements in a list.
Syntax {{ limitTo_expression|limitTo : limit : begin}}
Example Input {{root.clients|limitTo: 2 : 0}}
Example Output ["Client A", "Client Z"]

 

Filter Order By
Description Orders a list by a criterion (use ‘id’ for strings). There is an option to reverse the order.
Syntax {{ orderBy_expression|orderBy : expression : reverse : comparator}}
Example Input {{root.clients|orderBy:name}}
Example Output [{"id": "929", "name": "Client A"}, {"id": "121", "name": "Client B"}, {"id": "9111", "name": "Client C"}]

Truthiness and Falsiness

Unlike Java, JavaScript knows the concept of “truthy” and “falsy”. This differs from the strict definition where a value is only true if it evaluates to a true boolean value as it is the case with Backend Expressions.

A value in Frontend Expressions evaluates to true true if:

  • If a value is the boolean value true
  • If a value is a string, e.g. 'Product A'
  • If a value contains an object, e.g. a date, a case etc.
  • If a value is a number (positive or negative, with or without fraction)
  • If a value is {} or []
  • If a value is Infinity or -Infinity

A value in Frontend Expression evaluates to false if:

  • If a value is the boolean value false
  • If a value is null
  • If a value is undefined
  • If a value is 0
  • If a value is an empty string, i.e. '' or "".

Backend Expressions

A complete reference of the Spring Expression Language can be found here. This subchapter lists a number of special features only available in Backend Expressions.

Useful Backend Methods

Description Add all elements of a list to another list
Syntax #{concatenateListWithElements(List<base>, T... toAppend)}
Example #{concatenateListWithElements(clients, newClient, oldClient)}

 

Description Check if all conditions are true
Syntax #{allOf(Boolean... operands)}
Example #{allOf(root.isGoldClient && root.income > 20000, root.location = 'ch')}

 

Description Check if one of the conditions are true
Syntax #{anyOf(Boolean... operands)}
Example #{anyOf(root.isGoldClient, root.isDiamondClient)}

 

Description Gets values from a list of maps, useful for lists based on multi-element subforms
Syntax #{extractValuesFromCollectionOfMaps(Collection<Map<String, T>> maps, String mapKey, boolean skipNullValues)}
Example #{extractValuesFromCollectionOfMaps(root.clients, 'name', true)}

Safe Navigation

Often you as a modeler cannot guarantee that a variable exists in a certain process state. If you try to access the variable in a Backend Expression and the variable does not exist then the resolution of the Backend Expression results in an exception which then rollbacks the current transaction. This is usually not what you want.
Use the following Backend Expression to check if the ‘foo’ variable exists or not:
#{variablesMap.containsKey('foo')}
Use the following Backend Expression to safely get the ‘foo’ variable:
#{variablesMap.foo}

Index of

Use following Frontend Expression {{$index+1}} if you want to show an index of array in a subform with multi elements.