Using The IDRS Scripting Object
The
IDRS scripting object allows for an RML page to control more aspects of
the web development process. It allows the IDRS to no longer be used as
just a reporting engine, but as a full web-development platform. Using
the IDRSScript object inside of a component called from inside an RML page
is easy. Declare one of the arguments as IDRSScript and then place a obj_Scripter
in the correct place inside of the record's DocParams field in the tblDoc
table for the report where obj is the name of the object, database or varlist
being used to call the method. In this tutorial we will extend the RML
page we built in "Data Input" by echoing the information that was put in
the database. This is what the RML page, saved as
echo_input.rml,
will look like:
<rml>
<ishtml>true</ishtml>
<head>
<object id="echo">
<class>EchoClass</class>
<constructor>
</constructor>
<method>
<name>echoInput</name>
<vartype>scripter</vartype>
</method>
</object>
<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>
<varlist id="varEcho">
<vartype>scripter</vartype>
</varlist>
</head>
<body>
<center>
<h1>Added Record To Phone Dirrectory</h1><p>
<inputresults>input</inputresults> rows effected
<usemethod varlist="varEcho" objid="echo">echoInput</usemethod>
</center>
</body>
</rml>
As
you can see, passing the IDRS Scripting Object to a method is extremely
simple. In the <method> tag the <vartype> was set to IDRSScript and
the same in the <varlist> tag. That's it! Now lets look at the java
class that will be used to echo the input, EchoClass.java
:
import net.sourceforge.idrs.script.IDRSScript;
//needed to have IDRS Scripting support
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class
EchoClass {
public EchoClass() {
}
public String echoInput(IDRSScript
idrs) throws Exception {
PrintWriter out = idrs.getOut();
//gets the output stream back to the client
HttpServletRequest req =
idrs.getRequest();
out.println("<table>");
out.println("<tr><td>ID</td><td>"+
req.getParameter("input_id")
+
"</td></tr>");
out.println("<tr><td>First
Name</td><td>" + req.getParameter("input_first")
+
"</td></tr>");
out.println("<tr><td>Last
Name</td><td>" + req.getParameter("input_last")
+
"</td></tr>");
out.println("<tr><td>Phone</td><td>"+
req.getParameter("input_phone")
+
"</td></tr>");
out.println("<tr><td>Email</td><td>"
+
req.getParameter("input_email")
+
"</td></tr>");
out.println("</table>");
return "";
//no
need for a return, everything was printed
}
}
This class is very simple. There is an
empty constructor and only one method. The method echoInput does all of
the work of generating an html table with all of the data that was passed
to the RML page. The PrintWriter is retrieved as well as the request object.
It is now as simple as printing out the information.
The HTML for the data entry, svaed as varecho_input.html,
is the same :
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 that we have our class, our data entry
source and our RML document built, lets deploy it. Here are the parameter
we will use in deploying the new report :
ID |
5 |
Name |
test_script |
Source File |
echo_input.rml |
Groups |
1,2, |
Var Source |
varecho_input.html |
Is File |
false |
Parameters |
input_id:input_first:input_last:input_phone:input_email:varEcho_Scripter: |
Connections |
jdbc:postgresql:samples, |
Everything is standard except for the parameters.
Even here everything is standard until the last parameter, varEcho_Scripter.
varEcho_Scripter tells the IDRS to pass the IDRS Scripting Object to the
varlist varEcho. The IDRS Scripting Object can also be passed to
the constructor of an object or to a method called from inside of a <db>.
Now we can deploy the page using the InsertToIDRS program :
java InsertToIDRS -insgvfpc org.postgresql.Driver
jdbc:postgresql:idrs webadmin asd123 5 test_script echo_input.rml 1,2,
varecho_input.html false input_id:input_first:input_last:input_phone:input_email:varEcho_Scripter:
jdbc:postgresql:samples,
While the example is simple, it would have been easier to echo the
input from inside the report using an embeded scripting language.
That topic is covered by the next tutorial, "Embeding Scripting into an
RML Document".