This example builds on Example 3, producing an HTML report that demonstrates looping and conditional code in Velocity and enumerates the children of selected objects. The code also shows how to create a multi-page report using CSS facilities.
This is an advanced example. Setting up the HTML template itself is described in more detail in Example 1.
Setting up Parameters
As in the previous examples, issue a Velocity directive to create a variable based on run-time parameters.
#set($regulation = $dashboardParameters.getControl("PolicySet.id1", "Control or Questionnaire")) #set($assessmentProject = $dashboardParameters.getProject("AuditProject.id1", "Program")) $dashboardParameters.initChart("/Shared Reports/System/Controls Compliance/Compliance Level of a Control in a Project", "small") $dashboardParameters.initChart("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control","large") |
Inserting Page Breaks
You can produce arbitrarily complex HTML code, including Cascading Style Sheets (CSS), Javascript, and other technologies that are beyond the scope here to discuss. The following code illustrates how embedding CSS can paginate the output of your report. First, define the style in head:
<head> <style> p.breakpage {page-break-before: always} </style> </head> |
This code specifies a page break before all paragraphs of class "breakpage."
To start on a fresh page, insert the HTML:
Looping
Iterating over a set of objects is accomplished using the #foreach Velocity directive. The following loop prints the title and description of each child of the specified Program.
#set($sections = $DT.getChildren($regulation)) #foreach($section in $sections) <h2><a name='$section.getTitle()'> $section.getTitle()</a></h2> <p>$section.getDescription()</p> #end |
Note that $DT is an abbreviation for $dashboardTemplate.
Conditional Output
It can be useful to output certain HTML snippets only under particular circumstances. Velocity supports the #if directive for this purpose. You can set up index variables in your loop in order to output HTML only at the end of the loop, for example, or at the beginning.
#set ($i = 1) #foreach($section in $sections) #if ($i != 1 ) <br/><br/><br/> <hr> </br><br/> #end #set ($i = $i+1) <!-- output $section information --> #end |
This code establishes an index variable, $i, and outputs a horizontal rule (<hr>) every time through the loop except the first. This will output section 1, horizontal rule, section 2, horizontal rule, and so on.
Example Template
The complete HTML template for this example is:
#set($regulation = $dashboardParameters.getControl("PolicySet.id1", "Control or Questionnaire")) #set($assessmentProject = $dashboardParameters.getProject("AuditProject.id1", "Program")) $dashboardParameters.initChart("/Shared Reports/System/Controls Compliance/Compliance Level of a Control in a Project", "small") $dashboardParameters.initChart("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control","large") <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/> <title> Full report</title> <style> p.breakpage {page-break-before: always} </style> <link rel="stylesheet" href="/spc/css/homepage.css" > </head> <body margin marginheight="10" topmargin="10" leftmargin="10">> <center> <table border='0' width='750'> <tr> <td> <h1 align=left>$regulation.getTitle() </h1> $regulation.getDescription() <br/><br/><br/><br/><br/> <br/><br/><br/> <p > <br/><br/><br/> <h1 align='left'>Compliance / Risk Summary for $regulation.getTitle() </h1> <br/><br/><br/> <a name='Overall compliance'> <h2 align='left'>Overall compliance </h2></a> <br/><br/><br/> <table border='0' width='100%'> <tr> <td ALIGN=LEFT valign='BOTTOM'> <b>Project key information</b><br><br> <b>Project Name: <i>$assessmentProject.getName()</i></b><br><br> <b>Project Description:</b> <i>$assessmentProject.getDescription()</i> <br> <br> <b>Project Owner:</b> <i>$assessmentProject.getAuthor() </i> <br> <b>Project Status:</b> <i>$assessmentProject.getStatusDB() </i> <br> <b>Project Workflow:</b> <i>$assessmentProject.getRaWorkflowTemplate().getName()</i> <br> <b>Survey Taking:</b> <i>$assessmentProject.getSurveyTakingPreferences().getName() </i> <br> <br/><br/> <b>Regulation or Control Framework:</b> <i>$regulation.getTitle()</i> <br> <b>Regulation or Control Desciption:</b><i>$regulation.getDescription()</i> <br/><br/><br/> </td> <td ALIGN='center' valign='center'> $dashboardParameters.setControl("/Shared Reports/System/Controls Compliance/Compliance Level of a Control in a Project", "PolicySet.id1", $regulation) $dashboardParameters.getChart("/Shared Reports/System/Controls Compliance/Compliance Level of a Control in a Project") <tr></tr> <td ALIGN=LEFT valign='BOTTOM'> $dashboardParameters.setControl("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control", "PolicySet.id1", $regulation) $dashboardParameters.getChart("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control") </td>> </tr> </table> <br/><br/><br/> <a name='Detailed report by sections'> <h1 align='left'>Detailed report by sections </h1></a> <br/><br/> #set ($i = 1) #set($sections = $DT.getChildren($regulation)) #foreach($section in $sections) <br/><br/><br/> #if ($i!=1 ) <br/><br/><br/> <hr> </br><br/> #end #set ($i = $i+1) <h2 align='left'><a name='$section.getTitle()'> $section.getTitle()</a></h2> <br/><br/> <p align='left'>$section.getDescription()</p> <br/><br/> <table border='0' width='100%'> <tr> <td width='50%'> $dashboardParameters.setControl("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control", "PolicySet.id1", $section) $dashboardParameters.getChart("/Shared Reports/System/Compliance/Compliance Score in a Project for Children of a Control") </td> </tr> </table> <br/><br/> #set($sections_b = $DT.getChildren($section)) #foreach($section_b in $sections_b) <br/><br/> <h3 align='left'>$section_b.getTitle()</h3> <br/><br/> <p align='left'>$section_b.getDescription()</p><br/> <br/><br/> #end #end </td> </tr> </table> </center> </body> </html> |