Embedding Scripts


The IDRS now allows for scripts to be embedded into RML documents.  Any scripting   language can be used in the IDRS if it is wrapped in the net.sourceforge.idrs.script.embedable.IDRSScriptLanguage   class.  the two currently support languages are JPython and BeanShell.    JPython (www.jpython.org) is a java implementation   of the popular Python (www.python.org) language.    Beanshell (www.beanshell.org) is a version of  java  with scripting features including typeless variables.  Both languages  are  exremly powerful and flexible.  Before we get into the sample, which  will  be an extension of the "Using the IDRS Scripting Object" tutorial, lets look into what's needed to use an embeded script inside an RML document.

Scripts can be embeded in any part of the <body>section of an RML document.   There are two steps needed to use a scripting language inside of an RML document.  In the web.xml file for the IDRS, you need to add a paramater called scriptClass which tells the IDRS to create the scripting context based on the class giiven when it creates the pool of IDRSReport objects.  This is done becuase creating the scripting context takes about as long as building a connection to a database.  It allows for faster page loading time and it puts less strain on the server.  The second step is to put the name of the class you want to use in the <scriptclass>tag that goes after <rml>inside of an RML document.  That statement resets the scripting context. 

All scripts are embeded inside of <$ $>and <$= $>tags.  <$  $>tag means that whatever is placed inside of the tag will be executed  using the specified scripting language.  This tag may be broken up over  multiple lines.  The <$= $>tag prints the epression inside the tag.   This tag can't be broken up over several lines.  Lets look at some examples  :

<$ out.pritnln("<h1>test</<h1>"); $>

This line would print the text "<h1>test</h1>" back to the browser.    This next example preforms the exact same task :

<$= "<h1>test</h1>" $>


If you are using JPython you have to be very careful about indentation.    Python basses what code is in procedures and functions based on indentation,   for more information on what this means, visit python's website where there   are plenty of tutorials on how to use python.  When using python as an embeded   language for the IDRS be aware that the indentation starts at the first   word inside the <$ $>tag, not where the <$ $>tag appears on   the page. 

There are two objects that can be used inside of an RML document by default.  The object "out" is the output stream used by the report.  The other object is "idrs" which is an implementation of the IDRSScript object. 

Now that we have the basics on scripting, lets build a sample page.  Our    sample page will simply do the same thing that the last tutorial "Using    The IDRS Scripting Scripting Object", but instead of using an external class    we will embed a script into the page.  For this example we will use BeanShell.     Make sure that you have the BeanShell classes installed and in your CLASSPATH.     You can get BeanShell from either their web site (www.beanshell.org)    or from the idrs download page.  Here is the RML for the sample saved as echo_inputembed.rml :

<rml>
  <ishtml>true</ishtml>
  <scriptClass>net.sourceforge.idrs.script.embedable.IDRSBeanShell</scriptClass>
  <head>
   <db id="input">
     <direction>input</direction>
     <dbname>jdbc:postgresql:samples</dbname>
     <sql>
       <src>
         INSERT INTO contacts VALUES(?,?,?,?,?)
       </src>
       <vartype>int</vartype>
       <vartype>string</vartype>
       <vartype>string</vartype>
       <vartype>string</vartype>
       <vartype>string</vartype>
     </sql>
   </db>
   </head>
   <body>
     <center>
       <h1>Added Record To Phone Dirrectory</h1><p>
       <inputresults>input</inputresults>rows effected
       <table>
         <tr><td>ID</td><td><$= idrs.getRequest().getParameter("input_id") $></td></tr>
         <tr><td>First Name</td><td><$= idrs.getRequest().getParameter("input_first") $></td></tr>
         <tr><td>Last Name</td><td><$= idrs.getRequest().getParameter("input_last") $></td></tr>
         <tr><td>Phone</td><td><$= idrs.getRequest().getParameter("input_phone") $></td></tr>
         <tr><td>Email</td><td><$= idrs.getRequest().getParameter("input_email") $></td></tr>
       </table>
     </center>
  </body>
</rml>



As you can see the report is very simple.  After the <rml>and <ishtml>tags is the <scriptClass>tag.  This tag does two things.  If the class being used is already loaded because it was specified in the web.xml file, then it simply resets the scripting context.  If the class was not specified in the web.xml file, then it loads it and builds a new scripting context.  As you can see from the sample, embeding a script is very easy.  If you look at the <head>tag you will ntice that there is no longer an <object>or <varlist>tag.  These tags have been removed because we've eliminated the need to use an external class in this case, simplifying the RML page. 

Please remember that an embedded script should NOT BE USED TO PREFORM BUSINESS LOGIC!  Scripts should be used to simplify presentation logic by taking it out of external java classes.  It should not be used to preform any business logic though. 

Another point to remeber is that you can not use en embeded script to loop over html.  For example, in JSP this works :


<% for (int i =0; i <= 100; i++) { %>
   <h1>test</h1>
<%}%>

This code does not work in the IDRS.  It is deficult to read and debug, and so I have decided not to allow this capability.  This style makes it too easy to write poor code and dificult to maintian pages.

Now that our RML page is built, lets build the calling form's html.  It's  the exact same that it was for the "Using the IDRS Scripting Object" tutorial  saved as embedvar.html :

ID : <input type="text" name="input_id"><br>
First Name : <input type="text" name="input_first"><br>
Last Name : <input type="text" name="input_last"><br>
Phone : <input type="text" name="input_phone"><br>
Email : <input type="text" name="input_email"><br>
<input type="submit"><input type="reset">


Now lets look at the deployment settings :

ID 6
Name test_embed
Source File  echo_inputembed.rml
Groups 1,2,
Var Source embedvar.html
Is File false
Parameters input_id:input_first:input_last:input_phone:input_email:input_id:input_first:input_last:input_phone:input_email:
Connections  jdbc:postgresql:samples,

Now that we have all of the pieces, lets deploy it :

java InsertToIDRS -isngvfpc org.postgresql.Driver jdbc:postgresql:idrs webAdmin asdf123 6 test_embed echo)inputembed.rml 1,2, embedvar.html false input_id:input_first:input_last:input_phone:input_email: jdbc:postgresql:samples,



Thats it! Now you know how to embed scripts into an RML document.  Make sure to read the Documentation section of the website specificly the IDRSScript interface.  Also make sure to read up on the scripting lanuage you choose.