Article
· Nov 12, 2023 6m read

Part II: Jasperreports open source drag and drop reports for IRIS

The part II you will learn how to run any Jasper report file (jrxml file) designed on Jasper Studio from InterSystems IRIS. We will create a REST API to return a processed report on PDF format.

The jrxml file

On part I, we designed this report:

1. First of all, rename the report from Blank_A4.jrxml to sample.jrxml (Project Explorer tab > click right the file > Rename):

See renamed file:

2. Click right the sample.jrxml file > Properties and see the file Location:

3. Copy the file from this location to the docker instance (docker instance created on part I) on folder /home/irisowner/dev:

docker cp C:\Users\yurim\JaspersoftWorkspace\MyReports\sample.jrxml jirisreport-api-sample-iris-1:/home/irisowner/dev

Install the JIRISReport to be able to run Jasper from IRIS

1. Open the source code on VSCode (install ObjectScript and InterSystems extensions if you don't have it yet) and select on bottom docker:iris52773[USER] to open the options:

2. Select the option Open Terminal in Docker:

3. See the terminal:

4. Type zpm to begin zpm shell:

5. Type, execute and wait for the installation (take some time):

zpm:USER>install jirisreport -verbose

6. See the installation results:




The Report credentials

The JIRISReport connects to the database using the user and password configured on interoperability > credentials. To set the credentials do:

1. Open the Management Portal (http://localhost:52773/csp/sys/UtilHome.csp?$NAMESPACE=USER):

2. Go to Interoperability > Configure > Credentials:

3. Create the ReportCreds credential:

  • ID: ReportCreds
  • User Name: _SYSTEM
  • Password: SYS

The REST API

1. Go to src\dc\Sample\PersonREST.cls, edit the file to include a REST method on routes:

<!-- All persons Report -->
<Route Url="/report" Method="GET" Call="GetReport" Cors="true"/>

2. Right the method implementation:

/// Get all persons report
ClassMethod GetReport() As %Status
{
    Set Parameters = {}

    Set result = ##class(dc.jirisreport.JIRISReport).GenerateReportAsFile(
        "jdbc:IRIS://localhost:1972/USER", 
        "/home/irisowner/dev/sample.jrxml",
        "/tmp/sample.pdf",
        Parameters 
    )
        
    Set %response.ContentType = "application/pdf"
    Do %response.SetHeader("Content-Disposition","attachment;filename=""sample.pdf""")
    Set %response.NoCharSetConvert=1
    Set %response.Headers("Access-Control-Allow-Origin")="*"
    Set stream=##class(%Stream.FileBinary).%New()
    Set sc=stream.LinkToFile("/tmp/sample.pdf")
    Do stream.OutputToDevice() 
    
    Set tSC=$$$OK
}
  • The class dc.jirisreport.JIRISReport is a utility class to generate reports using JasperReports library.
  • The GenerateReportAsFile is a method that get the JDBC URL, report file path and PDF out path and generate the report on out path.

3. Save the PersonREST.cls file to compile it on server (VSCode must be connected to send the new version to the server). See the final version of the file:

Class dc.Sample.PersonREST Extends Sample.REST.Base
{

Parameter Version = "1.0.6";
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<!-- All persons Report -->
<Route Url="/report" Method="GET" Call="GetReport" Cors="true"/>
<!-- Server Info -->
<Route Url="/" Method="GET" Call="GetInfo" Cors="true"/>
<!-- Get all records of Person class -->
<Route Url="/persons/all" Method="GET" Call="GetAllPersons"/>
<!-- Swagger specs -->
<Route Url="/_spec" Method="GET" Call="SwaggerSpec" />
<!-- GET method to return JSON for a given person id-->
<Route Url="/persons/:id" Method="GET" Call="GetPerson"/>
<!-- Update a person with id-->
<Route Url="/persons/:id" Method="PUT" Call="UpdatePerson"/>
<!-- Delete a person with id-->
<Route Url="/persons/:id" Method="DELETE" Call="DeletePerson"/>
<!-- Create a person-->
<Route Url="/persons/" Method="POST" Call="CreatePerson"/>
<!-- Create random persons-->
<Route Url="/persons/gen/:amount" Method="POST" Call="CreateRandomPersons"/>


</Routes>
}

/// Get all persons report
ClassMethod GetReport() As %Status
{
    Set Parameters = {}

    Set result = ##class(dc.jirisreport.JIRISReport).GenerateReportAsFile(
        "jdbc:IRIS://localhost:1972/USER", 
        "/home/irisowner/dev/sample.jrxml",
        "/tmp/sample.pdf",
        Parameters 
    ) 
        
    Set %response.ContentType = "application/pdf"
    Do %response.SetHeader("Content-Disposition","attachment;filename=""sample.pdf""")
    Set %response.NoCharSetConvert=1
    Set %response.Headers("Access-Control-Allow-Origin")="*"
    Set stream=##class(%Stream.FileBinary).%New()
    Set sc=stream.LinkToFile("/tmp/sample.pdf")
    Do stream.OutputToDevice() 
    
    Set tSC=$$$OK
}

/// PersonsREST general information
ClassMethod GetInfo() As %Status
{
  SET version = ..#Version
  SET info = {
    "version": (version)
  }
  RETURN ..%ProcessResult($$$OK, info)
}

/// Retreive all the records of dc.Sample.Person
ClassMethod GetAllPersons() As %Status
{

    #dim tSC As %Status = $$$OK
    Set rset = ##class(dc.Sample.Person).ExtentFunc()

    Set %response.ContentType = ..#CONTENTTYPEJSON
    Write "["
    if rset.%Next() {
        Set person = ##class(dc.Sample.Person).%OpenId(rset.ID)    
        Do person.%JSONExport()
    }
    While rset.%Next() {   
        Write ","
        Set person = ##class(dc.Sample.Person).%OpenId(rset.ID)    
        Do person.%JSONExport()
    }
    Write "]"
    Quit tSC
}

/// Return one record fo dc.Sample.Person
ClassMethod GetPerson(id As %Integer) As %Status
{
	#dim tSC As %Status = $$$OK
    #dim e As %Exception.AbstractException
    #; Set the response header to plain text
    Set %response.ContentType = ..#CONTENTTYPEJSON

    Set person = ##class(dc.Sample.Person).%OpenId(id)

    If '$IsObject(person) Quit ..Http404()

    Do person.%JSONExport()

    Quit tSC
}

/// Creates a new dc.Sample.Person record
ClassMethod CreatePerson() As %Status
{
	#dim tSC As %Status = $$$OK
    #dim e As %Exception.AbstractException
    Set person = ##class(dc.Sample.Person).%New()
    Set data=%request.Content
    $$$TOE(tSC,person.%JSONImport(data))
    $$$TOE(tSC,person.%Save())

    Set %response.Status = 204
    Set %response.ContentType = ..#CONTENTTYPEJSON
    //d data.%ToJSON()
    Do person.%JSONExport()

    Quit tSC
}

/// Update a record in dc.Sample.Person with id
ClassMethod UpdatePerson(id As %Integer) As %Status
{
	#dim tSC As %Status = $$$OK
    #dim e As %Exception.AbstractException
    Set person = ##class(dc.Sample.Person).%OpenId(id)
    If '$IsObject(person) Return ..Http404()
    Set data=%request.Content
    $$$TOE(tSC,person.%JSONImport(data))
    $$$TOE(tSC,person.%Save())

    Set %response.Status = 200
    Set %response.ContentType = ..#CONTENTTYPEJSON
    Do person.%JSONExport()

    Quit tSC
}

/// Delete a record with id in dc.Sample.Person
ClassMethod DeletePerson(id As %Integer) As %Status
{
	#dim tSC As %Status = $$$OK
    #dim e As %Exception.AbstractException
    Set person = ##class(dc.Sample.Person).%OpenId(id)
    If '$IsObject(person) Return ..Http404()

    $$$TOE(tSC,person.%DeleteId(id))

    Set %response.Status = 200
    Set %response.ContentType = ..#CONTENTTYPEJSON

    Quit tSC
}

ClassMethod CreateRandomPersons(amount As %Integer) As %Status
{
#dim tSC As %Status = $$$OK
    Set %response.ContentType = ..#CONTENTTYPEJSON

    Try {
        set status = ##class(dc.Sample.Person).AddTestData(amount)
        $$$ThrowOnError(status)
        Set message = "Created "_amount_" random persons in Sample.Person data"
    } Catch(ex) {
        Set tSC = ex.Code
    }
    Return tSC
}

ClassMethod SwaggerSpec() As %Status
{
  Set tSC = ##class(%REST.API).GetWebRESTApplication($NAMESPACE, %request.Application, .swagger)
  Do swagger.info.%Remove("x-ISC_Namespace")
  Set swagger.basePath = "/crud"
  Set swagger.info.title = "InterSystems IRIS REST CRUD demo"
  Set swagger.info.version = "0.1"
  Set swagger.host = "localhost:52773"
  Return ..%ProcessResult($$$OK, swagger)
}

}

Creating data and get report from Swagger UI

1. Go to the the Swagger UI page (http://localhost:52773/swagger-ui/index.html):

2. Go to /persons/gen/{amount} method, expand it, set the amount with 10 and the content with {}, and execute it (Execute button):

3. Now, go to /report method and execute it:

4. Click the Download file link:

5. See the results:

Ready! You have an API with report generation. In the next article we will design a report with parameters.

Discussion (0)1
Log in or sign up to continue