Groovy Module
- 1 Overview
- 2 Setup
- 3 Usage
- 4 Groovy script examples
- 4.1 List 100 concepts?
- 4.2 Display some encounters?
- 4.3 Display some observations
- 4.4 Search for patients by name
- 4.5 Create providers for all users
- 4.6 Create providers for users with specified roles
- 4.7 Execute SQL commands
- 4.8 Manage modules
- 4.9 Delete program
- 4.10 Add Locations
- 4.11 Print user and patients
- 4.12 Delete duplicate patients
- 4.13 Modify observation to use coded answer
- 4.14 Delete observations
- 4.15 Delete concept and concept answers
- 4.16 Change program and workflow
- 4.17 Change person
- 4.18 Change form id
- 4.19 Retire form
- 4.20 Delete forms (with no encounters)
- 4.21 Retire drug
- 4.22 Remove birthdates
- 4.23 Print concept sets and names
- 4.24 Void concept names
- 4.25 Change ConceptSource
- 4.26 Change concept_map comment (for OpenMRS 1.9 upgrade)
- 4.27 Remove role
- 4.28 A more complex example
- 4.29 Trigger Sync for a Patient (may not be complete)
- 4.30 Make REST web service calls
- 4.31 Look for HTML Forms that have the same concept repeated, without extra contextual information
- 4.32 Update a Person Attribute and insert an Obs given a value from another Obs
- 5 Troubleshooting
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.
Create and save a groovy script
Create a task (Scheduler within administration page) using the Schedulable Class:
org.openmrs.module.groovy.GroovyTask
Add a property as follows:
name: "script name" (the literal text, 'script name' without quotes... not the name of your script)
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"
)
);
Reference: developer mailing list discussion
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.