Tuesday, January 24, 2012

A simple approach to generate reports using scala

I was wondering if it was possible to create one executable script containing both the report template and its configuration. So I tried to write a scala script to test this idea :
exec scala -nocompdaemon -usejavacp -savecompiled -deprecation "$0" "$@"

import sys.process._
import java.io.{File, FileOutputStream}

println("Let's generate and display a report")

val message = "hello"
val guys = List("Marge", "Bart", "Omer")

val smallreport = 
< html>
  < head>
    < title>Hello guys report< /title>
  < /head>
  < body>
    < h1>{message}< /h1>
    {guys map {guy=> 
      < h2>{guy}< /h2>
    } }
  < /body>
< /html>

val reportfile = File.createTempFile("report", ".html")

new FileOutputStream(reportfile) {

List("firefox", "-new-window", reportfile.toURI.toURL.toString) !

Note: I've added one space inside each HTML TAG, looks like SyntaxHighlighter doesn't like HTML or XML embedded within scala

Make this script executable (chmod u+x reportingScript.scala) and execute it (./reportingScript.scala), if firefox is available on your operating system, the following window will be displayed :

Now we can say :
- it is possible to combine a report template, and data access in a single executable script !
- By using a custom scala startup command, it should be possible to simplify many operations, extending defaults imports, ...

In a next post I'll try to make such script simpler.

No comments:

Post a Comment