Groovy Module

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.

For a quick overview of some of the cool groovy features, see this spring source training video

Setup

Download and install the Groovy module omod file from the module repository.
(You can download the code from github: https://github.com/openmrs/openmrs-module-groovy)

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):

  • admin

  • cohort

  • concept

  • encounter

  • form

  • locale

  • logic

  • obs

  • patient

  • person

  • user

  • fn (as of 2.2)

Using fn

The special fn variable provides access to convenience functions:

  • fn.sql("select count() from users")
    Runs the given SQL and returns the result set (only allows reading from database)

  • fn.sql("update users set name='Super-duper User' where user_id = 1", false)
    Runs SQL that can write to the database

Scheduling Groovy Scripts

As of version 2.2, you can schedule Groovy scripts to run automatically using the scheduler.

  1. Create and save a groovy script

  2. Create a task (Scheduler within administration page) using the Schedulable Class: org.openmrs.module.groovy.GroovyTask

  3. Add a property as follows:

    1. name: "script name" (the literal text, 'script name' without quotes... not the name of your script)

    2. value: the name of your script

Accessing Other Module Services

When trying to access the service of another module within a Groovy script, you might see a an error like:

java.lang.NoClassDefFoundError: $Proxy109

If you do, try loading the service like this:

def mymodule = Context.getService( Context.loadClass( "org.openmrs.module.mymoduleid.MyModuleService" ) );

Groovy script examples

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

Display some encounters?

Display some observations

Search for patients by name

Create providers for all users

Create providers for users with specified roles

Execute SQL commands

Manage modules

Delete program

Add Locations

Using groovy only:

Using mysql:

Print user and patients

Delete duplicate patients

Modify observation to use coded answer

Delete observations

Delete concept and concept answers

Change program and workflow

Change person

Or this simple script

Change form id

This used when we duplicate an InfoPath form into htmlform, and want to change all the infopath forms to use htmlform view/edit capabilities.

Retire form

Delete forms (with no encounters)

Retire drug

Remove birthdates

This is useful when no birthdate is known and bogus ages are selected (ie. 106 years old)

Print concept sets and names 

Void concept names

Change ConceptSource

 

Change concept_map comment (for OpenMRS 1.9 upgrade)



Remove role

A more complex example

Trigger Sync for a Patient (may not be complete)

Make REST web service calls

Make sure you don't save your password in a script! You can enter your password into this script and run it, but make sure to remove your password before saving the script!

The following script sets up basic authentication, makes a call to the REST Web Services module, and then displays the result.

Copy the JSON-formatted result into an editor like Sublime Text 2 with a JSON formatter (in Sublime Text 2, Tools > Command Palette > Package Control : Install Package > Pretty JSON) to quickly format the JSON into a more legible form.



Look for HTML Forms that have the same concept repeated, without extra contextual information

Update a Person Attribute and insert an Obs given a value from another Obs

 

A slightly groovier way

Troubleshooting

java.lang.ClassFormatError: Invalid method Code length 82342 in class file Script1

Solution: Java only allows methods that are 64K long. Reduce the size of your groovy script.