Smart Sites Java References

JAVA references

Smar Sites exposes some objects to Velocity Context:

  • $block: Allow you to import and evaluate in-process components written in HTML and VTL. This is very useful for reusing complex piece of code (ex. a menu or a table) or macros.
  • $db: Provide database access with some query methods.
  • $sys: Expose some utility functions (i.e. “$sys.now”)
  • $math: Expose math functions like rnd(), round(), etc..
  • $fmt: Provide useful Format functions for dates and numbers

The component Pattern: $block

Write reusable components is always better than “copy and paste”.
$block allow you to import into master document any block of code (HTML, VTL or other).
We call those blocks of code simply “components”.

Your components can be saved in same  directory of Templates (see also “Introduction“).
I prefer use a prefix to name components (“block_”), but this is absolutely optional.

Functions:

  • get(String blockName).
    Usage: $block.get(“myblock.html”).
    Behavior: Insert a block of text into current document

Accessing the database: $db

Smart Sites database is a schema-less database (see also “Smart Sites Database“).

The $db object reference allow you access the database and query it to extract data.

Functions:

  • find(String collection)
    Usage: $db.find(“items”)
    Behavior: Return a collection of objects. This method has no filter, so return all items in collection
  • find(String collection, String filterExpression)
    Usage: $db.find(“items”, “name=ITEM1&description=ITEM DESCRIPTION”)
    Behavior: Return a filtered collection of objects. The filter is a simple expression of type [field-name][comparator][value].
    Supported comparators are =, >, <. Using “&” you can concatenate more condition with AND logic operator (OR will be supported later).
  • findOne(String collection, String filterExpression)
    Usage: $db.findOne(“items”, “name=ITEM1&description=ITEM DESCRIPTION”)
    Behavior: Return first item of a filtered collection of objects. This is useful when you are filtering for a unique key and you are sure that result is always a single entity.
  • findById(String collection, String id)
    Usage: $db.findById(“items”, “123″)
    Behavior:  Returns an entity searching for unique key “ID”. This two functions do the same: $db.findOne(“items”, “_id=1″), $db.findById(“items”, “1″).

Utility and System functions: $sys

$sys contains some useful functions.

Functions:

  • getNow()
    Usage: $sys.now or $sys.getNow()
    Behavior:  Return current Date and Time. To format the result you can use $fmt.
  • getTomorrow()
    Usage: $sys.tomorrow $sys.getTomorrow()
    Behavior:  Return tomorrow Date and Time. To format the result you can use $fmt.
  • getYesterday()
    Usage: $sys.yesterday or $sys.getYesterday()
    Behavior:  Return yesterday Date and Time of . To format the result you can use $fmt.

Random and Round functions: $math

$math exposes round and random functions.

Functions:

  • random(Double min, Double max)
    Usage: $math.random(0,100)
    Behavior:  Return a random number between “min” and “max” parameters.
  • randomAscii(Integer length)
    Usage: $math.randomAscii(5)
    Behavior:  Return a random ASCII String. The parameter “length” indicates length of String.
    Characters will be chosen from the set of characters whose  ASCII value is between 32 and 126.
  • randomInt(Integer digitLength)
    Usage: $math.randomInt(5)
    Behavior:  Return a random Integer of “digitLength” digits. The parameter “digitLength” indicates the number of digits.
  • randomString(Integer length)
    Usage: $math.randomString(5)
    Behavior:  Return a random String. The parameter “length” indicates length of String.
  • round(Double value, Integer decimals)
    Usage: $math.round(5.3453, 2)
    Behavior:  Return a rounded value with a specified number of decimals.
    i.e. round(1.234, 2) = 1.23
    i.e. round(1.236, 2) = 1.24
  • roundCeil(Double value, Integer decimals)
    Usage: $math.roundCeil(5.3453, 2)
    Behavior:  Return a ceil rounded value with a specified number of decimals.
    i.e. roundCeil(1.121, 2) = 1.13
  • roundFloor(Double value, Integer decimals)
    Usage: $math.roundFloor(5.3453, 2)
    Behavior:  Return a floor rounded value with a specified number of decimals.
    i.e. roundFloor(1.129, 2) = 1.12

Format everything: $fmt

$fmt help you format numbers and dates.

Functions:

  • formatDate(Date date)
    Usage: $fmt.formatDate(date)
    Behavior: Return a formatted date using the Operating System locale.
  • formatDate(Date date, String dateFormat)
    Usage: $fmt.formatDate(date, “yyyyMMdd”)
    Behavior: Return a formatted date using specified date format.
  • formatDate(String inputDate, String inputDateFormat, String outputDateFormat)
    Usage: $fmt.formatDate(“dd-MM-yyyy”, “21-01-1968″, “yyyyMMdd”)
    Behavior: Return a formatted date using specified date format. “inputDate” parameter accept a String representing a date in format declared in “inputDateFormat” parameter. The date parser will accept “inputDate” if formatted conforming to “inputDateFormat”.
  • formatMonth(Object month)
    Usage: $fmt.formatMonth(2)
    Behavior: Return a formatted Month using the Operating System locale. Result of $fmt.formatMonth(2) will be “February”.
  • formatNumber(Object number)
    Usage: $fmt.formatNumber(123452.1234)
    Behavior: Return a formatted Number using the Operating System locale. Result of $fmt.formatNumber(123452.1234) will be “123,452.1234″ or “123.452,1234″ depending if you are using English format or Italian format..