Reporting module using TextTemplateRenderer Groovy

(Samuel Mbugua) #1

Using this guide https://wiki.openmrs.org/display/docs/Text+Template+Renderer+Groovy I would want to display age in years and months. I am able to display age using $data.person.age (where person is my parameter) but I cannot use fullYears or fullMonthsSinceLastBirthday. It fails with

Caused by: groovy.lang.MissingPropertyException: No such property: fullYears for class: java.lang.Integer at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

(Mike Seaton) #2

@sthaiya if you look at that guide you linked to, it specifies that to use the “fullYears” or “fullMonthsSinceLastBirthday” properties, you have to be operating on an “Age” object. This would be if you put a column of type Age into your dataset and referenced it like $data.age. But if you are referencing the age property of the Person object (which you are doing when you say $data.person.age), this is an Integer.

Mike

(Samuel Mbugua) #3

Thanks Mike for the prompt reply.

I have tried this with no success - what am i doing wrong? These are my steps:

  1. Created a Data Definition of type Age and named it “age”
  2. Created a Dataset Definition named “Salama” of type “Row-Per-Patient Dataset” and added age as a column.
  3. The Dataset definition has a parameter of type “Person” named and labeled person
  4. Created a Report a definition and added “salama” as a dataset and added the a parameter choosing the person parameter
  5. In my groovy script when a print $data I get Pastie Link
  6. $data.age prints null.
(Mike Seaton) #4

Try $Salama.age

(Samuel Mbugua) #5

Fails with Caused by: groovy.lang.MissingPropertyException: No such property: Salama for class: SimpleTemplateScript228

(Sanjula Madurapperuma) #6

I think this error is because you have not defined a value for age yet? Did you check on that? Then try $data.age.fullYears and $data.age.fullMonthsSinceLastBirthday

This error usually comes when you misspell a variable name or else try to call variables you haven’t defined.

(Sanjula Madurapperuma) #7

Yeah given the information you have provided, it looks to me that the problem lies in $data.age being null. Did you try printing $data.person? Does it include the age? Is it null?

(Samuel Mbugua) #8

@sanjula I have defined a parameter called person and mapped it.

  • $data.person prints “Patient#3” where 3 is person id.
  • $data.person.age prints 44 - which is the int age of the patient
  • $data.age.fullYears throws TemplateEvaluationException: No such property: fullYears for class: java.lang.Integer
  • $data.age prints null

If I run the Dataset definition alone I get Age as expected

(Mike Seaton) #9

@sthaiya, it takes a lot of trial and error to get right unfortunately, and it’s certainly possible that we need to improve our groovy handling. I created a simple report on the demo server that I was able to get working with some less elegant syntax. I’m sure it could be improved. See:

https://demo.openmrs.org/openmrs/module/reporting/reports/reportHistoryOpen.form?uuid=dc239d9f-88e4-4cd6-a370-f2d4688b9c07

In case this demo is overridden, and for historical value, here is generally what I did:

  • Created an AgeDataDefinition called “age”
  • Created a PatientDataSetDefinition with a single column, using the “age” data definition, named “patientAge” in the data set.
  • Created a ReportDefinition with that DSD, naming the DSD “myData” in the report.

I then added a TextTemplateRenderer with Groovy as the type, and the following contents:

<% reportData.dataSets.get("myData").rows.each { dsRow -> 
    def patAge = dsRow.getColumnValue("patientAge");
%>
        Patient Age:  <% if (patAge != null) { %> $patAge.fullYears years and $patAge.fullMonthsSinceLastBirthday months <% } %> 
        -----------------------------
<% } %>

This produces a text file with each patient’s age outputted with years and months.

Mike

1 Like
(Samuel Mbugua) #10

Thanks a million Mike, this demo guide really help and solves my problem.

1 Like
(Sanjula Madurapperuma) #11

Glad to know the issue was fixed Samuel!!! And thanks a lot Mike, for taking the effort.