Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Practically Groovy: Go Server-Side Up, with Groovy

By Andrew Glover
2005-05-05


Refactor me this ...

You can learn a lot from refactoring older code as your experience with a programming language or platform grows. I'd like to revisit the simple reporting application from January's column, when you were just learning learning about GroovySql.

As you'll recall, I built a quick-and-dirty reporting application that could have multiple uses within an organization. As it turns out, the application has since become quite popular for studying activity on the company database. Now, nontechnical personnel want to have access to this stupendous report, but they don't want the overhead of having to install Groovy on their machines to run it.

I kind of predicted this would happen, and the solution seems practically obvious: I'll Web-enable the reporting application. Lucky for me, Groovlets and GSPs will make the refactoring a snap.

Refactoring the reporting application
First, I'll tackle the guts of the simple application from Listing 12 of the GroovySql article. Refactoring this is easy: I simply replace all the printlns with logic that places an instance variable in the HttpRequest object using the setAttribute() method.

My next step is to forward the request, using a RequestDispatcher, to a GSP that will handle the view component of the reporting application. The new report Groovlet is defined in Listing 8:

Listing 8. The refactored database reporting application

import groovy.sql.Sql

/**
* forwards to passed in page
*/
def forward(page, req, res){
dis = req.getRequestDispatcher(page);
dis.forward(req, res);
}
sql = Sql.newInstance("jdbc:mysql://yourserver.anywhere/tiger", "scott",
"tiger", "org.gjt.mm.mysql.Driver")

uptime = null
questions = null
insertnum = null
selectnum = null
updatenum = null
sql.eachRow("show status"){ status |
if(status.variable_name == "Uptime"){
uptime = status[1]
request.setAttribute("uptime", uptime)
}else if (status.variable_name == "Questions"){
questions = status[1]
request.setAttribute("questions", questions)
}
}
request.setAttribute("qpm", Integer.valueOf(questions) /
Integer.valueOf(uptime) )
sql.eachRow("show status like 'Com_%'"){ status |
if(status.variable_name == "Com_insert"){
insertnum = Integer.valueOf(status[1])
}else if (status.variable_name == "Com_select"){
selectnum = Integer.valueOf(status[1])
}else if (status.variable_name == "Com_update"){
updatenum = Integer.valueOf(status[1])
}
}
request.setAttribute("qinsert", 100 * (insertnum / Integer.valueOf(uptime)))
request.setAttribute("qselect", 100 * (selectnum / Integer.valueOf(uptime)))
request.setAttribute("qupdate", 100 * (updatenum / Integer.valueOf(uptime)))
forward("mysqlreport.gsp", request, response)
The code in Listing 8 should be quite familiar. I've simply replaced all the printlns from the previous application and added a forward function to handle the view portion of the report.

Adding the view component
My next step is to create the GSP to handle the reporting application's view. Because I'm an engineer and not an artist, my view is rather simple -- a tad of HTML with a table, as shown in Listing 9:

Listing 9. The view component of the report

<html><head>

<title>MySql Health Report</title>
</head>
<body>
<table>
<tr>
<td>Database Uptime:</td><td><% println
"${request.getAttribute("uptime")}" %></td>
</tr>
<tr>
<td>Number of Queries:</td><td><% println
"${request.getAttribute("questions")}" %></td>
</tr>
<tr>
<td>Queries per Minute =</td><td><% println
"${request.getAttribute("qpm")}" %></td>
</tr>
<tr>
<td>% Queries Inserts =</td><td><% println
"${request.getAttribute("qinsert")}" %></td>
</tr>
<tr>
<td>% Queries Selects =</td><td><% println
"${request.getAttribute("qselect")}" %></td>
</tr>
<tr>
<td>% Queries Updates =</td><td><% println
"${request.getAttribute("qupdate")}" %></td>
</tr>
</table>
</body>
</html>
Running the new report should result in the output found in Listing 3. Of course, numbers will vary.

Figure 3. Output from the refactored reporting application


Tutorial Pages:
» On-the-fly Server-Side Programming with Groovlets and GSPs
» Defining Functions in Scripts
» Groovlets and GSPs
» The Groovlet, Please
» A Diagnostic Groovlet
» What About Those GSPs?
» Refactor me this ...
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources