Supercharged Datasources

A new feature in Vertical Site is expression language inside value fields in old datasources. This boosts the datasource functionality with more dynamics. The expression language is compatible with Unified Expression Language features in J2EE-5, and is therefore a standard.

Expression Basics

All expression values begin with ${ (dollar curly begin) and ends with } (curly end). You can have multiple expression values inside one data line.

The volume of my cube is ${10 * 10 * 10} m3. 
The largest number is ${max(100, 10}.
Current year is ${currentDate("yyyy")}, and current month is ${currentDate("MM")}.

Scoped Variables

To be able to put the expressions in a Vertical Site datasource we must have some scoped values like parameters, session values and even cookie values. All request parameters is accessed with param variable, session attribues with the session variable and cookie values with cookie variable.

To illustrate this, we have some examples:

Title request parameter is ${param.title}

If you need a default value, you can use the select method to select the first “non-empty” value.

Title request parameter is ${select(param.title, "no title")}

Or, if you have multiple parameters and only one is set.

Title request parameter is ${select(param.title, select(param.alternative, "no title"))}

Expression Functions

To make the expression feature more complete, we have a some expression functions that make things easier.

select(v1, v2)
Select the first “non-empty” value. Example: select(param.title, “no title”)
replace(source, regex, replacement)
Replaces the regular expression in source with replacement. Example: replace(“1:2”, ”:”, ”,”)
substring(source, begin)
Extracts substring from source from begin index. Example: substring(“myname”, 2)
substring(source, begin. end)
Extracts substring from source using begin index and end index. Example: substring(“myname”, 2, 6)
lower(source)
Transformes source to lower case. Example: lower(“MYNAME”)
upper(source)
Transformes source to upper case. Example: upper(“myname”)
trim(source)
Removes all spaces in each end of string. Example: trim(“ myname ”)
min(v1, v2)
Returns the minimum value of v1 and v2. Example: min(10, 100)
max(v1, v2)
Returns the maximum value of v1 and v2. Example: max(10, 100)
currentDate(format)
Format the current date with specified formatting rule. The rules follow SimpleDateFormat in java. Example: currentDate(“yyyy”)

Making It Work

One of the use cases for this expression language functionality is to make URL's prettier.

<datasources>
  <datasource name="someDataSource">
    <parameter name="query">data/title LIKE '${select(param.title_query, '')}'</parameter>
  </datasource>
</datasources>      

The above example uses a fictionous datasource, and will not actually work in any Vertical Site installations.

To be able to compose query strings with current date, you can use the following template. This illustrates "who has birthday today" problem.

<datasources>
  <datasource name="someDataSource">
    <parameter name="query">data/bdate LIKE '${currentDate('%-MM-dd)}'</parameter>
  </datasource>
</datasources>       

Comments

8 May 2009 15:21

Commented by Jonas Lepsøy

This is awesome! However, when trying to override the query parameter in the URL, a lot of problems appear. The portal functions have a tendency to rewrite % and ' to %25 and %27, which breaks the query. Even when writing the URL manually, this method is fairly unreliable. Could you show how to do string concatenation with parameters from the URL? Something like: data/bdate LIKE '${currentDate('%-' param.month '-dd')}' If this works (it doesn't of course), it is easy to show the people with birthdays specified by the parameter.

8 May 2009 15:26

Commented by Jonas Lepsøy

Hups, det ble en liten feil i den pseudokoden der. Jeg mente å skrive data/bdate LIKE '${'%-' param.month '-%'}'

If you want to comment on this article you need to be logged in.

Published in 2011

2010

2009

2008

2007