Function Types

Top  Previous  Next

At the beginning it can be confusing to decide which type of function we should use.

There are three types of functions that can be used when developing applications.

1. Compile Time Functions: CScript Functions ( % Functions)

They can be used in the properties of the elements used in the pages and they are called when the pages are being compiled. After the compilation is finished they do not exist anymore.

If, for example, "Page Name: %vof(PAGE_NAME)" is the value of a text element, during the compilation this function will be replaced with the value it returns; e.g. "Page Name: Products".

See Compile Time Functions

2. Run Time Functions: MScript Functions ( @ Functions )

MScript Functions are executed by the MoreMotion AF during processing of a request at the server. The Java classes can call the MoreMotion AF's function resolver to execute the MScript functions embedded in the given strings. MoreMotion AF itself can resolve the MScript functions existing in the data source definitions to provide dynamic database queries.  

Where can we use MScript Functions?

1.In Datasource Definitions
2.In the properties of the process based components (e.g. mor > RelDB, mor > ADOM, mor > Mailer, etc)
3.In any parameter that will be processed by a Java class extended from MoreMotion AF Base Classes.

 

If, for example, a RelDB Query contains MScript functions, they are resolved before the query is executed.

SELECT * FROM customer WHERE ID = @vof(i:ID)

 

In this example, @vof(i:ID) is replaced with the value of the request parameter "ID" first and then the query is executed.

 

Resolving MScript functions using MoreMotion AF API

If we are developing an ActionSerice, a DataService or a Process, we can resolve the MScript functions existing in strings as follows:

 

// For Services

String myResolvedParameter = request.resolve(myParameter); 

 

// For Processes

String myResolvedParameter = resolve(myParameter); 

 

 

3. Transformation Time Functions: XSL Functions

XSL functions are executed at the XSL Transformation time. This is the last step of the request handling process .

1.The request is forwarded to the server
2.The request is initiated on the server; Process and Service classes are executed, ADOM's are populated.
3.Page XML Data is prepared out of the ADOM's
4.The XSL Transformation is started with the XSL Stylesheet and the Page XML Data inputs; The XSL functions are executed at this moment.

 

MoreMotion Application Studio, provides the "$vof()" macro that helps to specify XSL functions in the element properties easily. This macro is expanded into the XSL functions during build.

Examples:

$vof() macro usage

XSL Expansion

$vof(/person/NAME)

  <xsl:value-of select="/root/person/NAME"/>

$vof(VAT_INCL = 'true' ? PRICE ; PRICE * VAT)

  <xsl:choose>

    <xsl:when test="VAT_INCL = 'true'">

      <xsl:value-of select="PRICE"/>

    </xsl:when>                     

    <xsl:otherwise>

      <xsl:value-of select="PRICE * VAT"/>

    </xsl:otherwise>

  </xsl:choose>

 

The thing to keep in mind is whether it be the native XSL functions or be the XSL functions expanded from $vof() macros, are executed at the last step (4th step) of the steps given above; NOT before.

Therefore the $vof() macro is used incorrectly in the following data base query and there will never be a result set that contains records.

  select * from customer where NAME = '$vof(NAME)' <-- WRONG USAGE!!!

 

In other words, we cannot use XSL functions where we are supposed to use MScript functions.

Using a mixture of the functions

It is possible to use different types of functions together. For example you should not be confused when you see such a definition:

$vof(%vof(name))

 

%vof(name) is a CScript function and it will return the name of the current element. Assume that it is 'EMAIL'. After the %vof(name) is resolved what remains is $vof(EMAIL) which will be expanded to <xsl:value-of select="EMAIL"/>.

 
Another Example:

Assume that you want to develop a database independent application and therefore you need to check the type of the database and execute a query according to this. You can accomplish using MScript functions that as follows

 

  INSERT INTO orders (NAME) VALUES('@vof(i:NAME)');

  @doif(sysparam(dbtype) = 'mysql')

    selecT LAST_INSERT_ID() as v:ORDER_ID;

  @doelse()

    selecT @@IDENTITY as v:ORDER_ID;

  @doend()

 

 

You can also do it as follows for a better performance:

 

  INSERT INTO orders (NAME) VALUES('@vof(i:NAME)');

  %doif(s:dbtype = 'mysql')

    selecT LAST_INSERT_ID() as v:ORDER_ID;

  %doelse()

    selecT @@IDENTITY as v:ORDER_ID;

  %doend()

 

 

After the compile-time functions executed, depending on the value of your build parameter s:dbtype, assume that it is 'mysql' the result will be:

 

  INSERT INTO orders (NAME) VALUES('@vof(i:NAME)')

    selecT LAST_INSERT_ID() as v:ORDER_ID;

 

 

See Build Parameters.