No, I’m not smoking anything and I’m not trying to do a Greg Brady impersonation. By “groovy” module…I mean a Groovy module. đ
Groovy has the potential to bring the fun and rapid prototyping benefits of languages like Ruby to the Java scene. But a big advantage of Groovy is that it’s built in Java and designed to both work with Java and give Java programmers a fairly easy introduction to the fun of higher level scripting.
So, this Groovy module is a just testing the waters. Groovy is incredibly easy to embed into any Java application (they make it ridiculously simple…a single JAR library is all it takes). So, I made a simple little module that embeds Groovy into OpenMRS and gives a single web page with a textarea where you can play with Groovy scripting. The module adds a “Groovy Scripting” privilege to OpenMRS that you could assign to users…but I wouldn’t recommend it. Currently, this is for testing only and shouldn’t be used in production until we have some more experience with it.
So, fire up a copy of OpenMRS, install the new Groovy module and let’s take it for a spin.
Once the Groovy module is installed, you should see a link in the OpenMRS administration page under a new “Groovy Module” section. Clicking on that link should bring up a simple textarea dialog like the one shown above. Sorry…but that’s about as fancy as it gets for now.
There are several special variables automatically bound to the context for you: cohort, concept, encounter, form, locale, logic, obs, patient, person, and user. Most of these are bound to the corresponding service; locale is the locale of the current context, which is handy for some API calls.
So, type the following into the textarea and click the GO button:
p = patient.getPatient(2)
println p.givenName
That should display the given name of patient #2 from your database. Here are a few more examples of Groovy scripts you can copy and paste into the textarea:
import groovy.xml.*
writer = new StringWriter()
b = new MarkupBuilder(writer)
b.table(border: 1) {
for (i in 1..100) {
c = concept.getConcept(i)
tr { td(c.name, style:"font-weight:bold"); td(c.name.description) }
}
}
println writer.toString()
p = patient.getPatient(2)
println "<h2>${p.givenName} ${p.familyName} Encounters</h2>"
for (e in encounter.getEncounters(p)) {
println "${String.format('%tF', e.encounterDatetime)} - ${e.creator}<br />"
println "<ul>"
for (o in e.getObs()) {
println "<li>${o.concept.name}</li>"
}
println "</ul>"
}
p = patient.getPatient(2)
println "<h2>${p.givenName} ${p.familyName}</h2>"
for (o in obs.getObservations(p, false)) {
println "${o.concept.name} → ${o.getValueAsString(locale)}<br />"
}
s = "nga"
for (p in patient.getPatientsByName(s)) {
n = "${p.givenName} ${p.familyName}".replaceAll("(?i)($s)", "<b>\$1</b>")
println "${n}<br />"
}
import org.openmrs.module.*
// Uncomment the next line to start the printing module
// ModuleFactory.startModule(ModuleFactory.getModuleById("printing"))
// list out your running modules
for (m in ModuleFactory.startedModules) {
println "${m.name} : ${m.started}<br />"
// uncomment the next line to stop the printing module
// if (m.name == "Printing") ModuleFactory.stopModule(m)
}
As you may gather from trying the examples above, I’m redirecting the stdout from the script into a <div> on the page as HTML, so you can play with formatted output. If you want to simply print some text within Groovy and preserve whitespace, then just put println “<pre>” and println “</pre>” around your println statements. Or use <textarea></textarea> instead.
Learning Groovy (especially for Java developers) is pretty easy. See the Groovy documentation for more about the language. Remember that you don’t need get and set — e.g., person.givenName
will execute person.getGivenName()
for you. And you can enjoy a break from the semicolons. đ
The real fun with Groovy is that you can throw in good ol’ import statements and directly reference java classes whenever you want.
While the ability to execute some scripts on the fly can be fun…maybe even handy for testing out code during development, my real hope is to evolve toward a Grails module that could facilitate rapid prototyping of web forms or quick & dirty report pages. The ultimate would be a Grails module that provides a quick and easy starting point, let’s you edit and extend it without any re-compile steps, and then provides a button to download a snapshot of the current state of the module as a new omod file to facilitate rapid prototyping of OpenMRS modules to target quick & dirty development needs and/or prototype new functionality.
Now that would be groovy!