Groovy Templates

iKnowBase supports the use of Groovy templates. When using Groovy templates, you will use the power of groovy to create the desired output, or perform the required actions.

combine static markup, standard FreeMarker functionality and iKnowBase model objects to create the desired output:

In order to best utilize the FreeMarker technology, you should read the FreeMarker documentation available at http://freemarker.org/docs. Some key features are described below, but this covers only a small subset of the capabilities.

About Groovy

Groovy is a full-featured programming language for the Java Virtual Machine. More information about groovy is available at the following sources:

Where can you use Groovy

Groovy is available in many places in iKnowBase.

Implicit objects

When using Groovy, iKnowBase will always provide you with a number of “implicit objects” available to the script. These are documented in the API Reference, under each component type, and are the same objects that are being made available to FreeMarker templates. However, Groovy templates have access to a number of additional objects.

For templates that run in any sort of viewer or page, the following objects are available:

Object Description
out A PrintWriter representing the output destination
html A MarkupBuilder useful for creating HTML or XML markup to the output destination
json A StreamingJsonBuilder useful for creating JSON markup to the output destination
iknowbase A RepositoryModel providing access to the iKnowBase database repository

When running in a web settings, where there is in fact an underlying HTTP servlet, the following objects are also available:

httpServletRequest A HttpServletRequest providing access to the underlying HttpServletRequest
httpServletResponse A HttpServletResponse providing access to the underlying HttpServletResponse

For templates that run as a Script Target, the following additional objects are also available:

Object Description
sout A ServletOutputStream representing the output destination

Examples

This chapter contains a couple of very small examples, indicating how you can use Groovy along with iKnowBase.

This example shows a couple of items. First, how you can use the iknowbase-object to get a Groovy SQL object, and then use that to query the database. Second, it shows how the implicit MarkupBuilder named html can be used to produce well-formatted html output.

 // Prepare some data
def pageTitle = "/SYSTEST/Action/GroovyScript-HtmlBuilder"
def users = iknowbase.sql.rows("select * from all_users where rownum <= 5")

// Output a complete html-document
html.html {
   head {
      title(pageTitle)
      style(type:"text/css","""
      table { border:thin solid black; width:100%; }
      th { background-color: #999 }
      """)
   }
   body {
      h1(pageTitle)
      table {
         tr {
            th("User ID")
            th("Username")
         }
         users.each { user->
            tr {
               td(user.user_id)
               td(user.username.toLowerCase())
            }
         }
      }
   }
}

This example shows how the implict StreamingJsonBuilder named json can automate the production of json:

def dbUsers = iknowbase.sql.rows("select * from all_users where rownum <= 5")

// Output a complete json-document
def userlist = [users:dbUsers.collect { [id:it.user_id, username:it.username.toLowerCase() ] }]
def userinfo = [userinfo:userlist]

json(userinfo)

This example shows how to use the documentService to create a document from an activitiy Script task.

import com.iknowbase.api.contentservices.v2.model.*;
def userref = new UserReference().withUsername(initiator)

def document = new Document()
document.documentIdentity = new DocumentReference(null, null, "activiti_" + execution.getProcessInstanceId(), null, null)
document.title = "Activiti Process - ProcessInstanceId: " + execution.getProcessInstanceId()
document.documentTypeReference = new ObjectReference(null, null, "TEMPDATA", null);

// Create ikb document
def documentReference = iknowbase.documentService.saveDocument (
   userref,
   document,
   SaveOperationEnumeration.INSERT,
   SaveOperationEnumeration.NONE,
   SaveOperationEnumeration.NONE,
   null, null, null, false
);