Groovy Module
Contents |
Overview
This module was created as a proof of concept (for embedding Groovy into OpenMRS) and to serve as a base module for other modules that want to use Groovy scripting as well.
Setup
Download and install the Groovy module omod file from the module repository.
Usage
Find and click on the Groovy Scripting Form link within the Groovy Module section of OpenMRS' administration page. You should be taken to a simple web form with a single textarea followed by a "GO" button. Type any valid Groovy script into the textarea and click the button to execute the script.
Some variables are automatically provided (most of these represent the API service):
- cohort
- concept
- encounter
- form
- locale
- logic
- obs
- patient
- person
- user
List 100 concepts?
import groovy.xml.MarkupBuilder writer = new StringWriter() b = new MarkupBuilder(writer) b.table(border: 1) { 1.upto(100) { c = concept.getConcept(it) tr { td(c.name, style:"font-weight:bold"); td(c.name.description) } } } println writer.toString()
Display some encounters?
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>" }
Display some observations
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 />" }
Search for patients by name
s = "nga" for (p in patient.getPatientsByName(s)) { n = "${p.givenName} ${p.familyName}".replaceAll("(?i)($s)", "<b>\$1</b>") println "${n}<br />" }
Manage modules
import org.openmrs.module.ModuleFactory // 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) }
TODO
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 (see Groovy Forms Module). 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.
