Desktop version

Home arrow Computer Science arrow Building Applications with Scala

Source

Reports folder

We need to move the reports template (JRXML files) from the source folder to the public folder. Why do we need to do this? Because when we generate the standalone deploy, they won't be included in the application jars. What is inside the public folder will be packed and deployed into a proper JAR file. That's why we need to make this change.

Create a folder called reports at ReactiveWebStore/public/. Then move all the JRXML files there.

Changing report builder

As our templates will be inside a JAR file, we need to change the loading logic in order to get the templates properly. Under ReactiveWebStore/app/report/, we need to change ReportBuilder.scala, which should look something like this after editing:

package reports object ReportBuilder {

private var reportCache:scala.collection.Map[String,Boolean] = new scala.collection.mutable.HashMap[String,Boolean].empty def generateCompileFileName(jrxml:String): String =

"/tmp/report_" + jrxml + "_.jasper" def compile(jrxml:String){

if(reportCache.get(jrxml).getOrElse(true)){ val design:JasperDesign = JRXmlLoader.load(

Play.resourceAsStream("/public/reports/" + jrxml) (Play.current).get )

JasperCompileManager.compileReportToFile(design,

generateCompileFileName(jrxml))

reportCache += (jrxml -> false)

}

}

def toPdf(jrxml:String):ByteArrayInputStream = { try {

val os:OutputStream = new ByteArrayOutputStream() val reportParams:java.util.Map[String,Object] = new java.util.HashMap() val con:Connection =

DriverManager.getConnection("jdbc:mysql://localhost/RWS_DB?

user=root&password=&useUnicode=

true&useJDBCCompliantTimezoneShift=

true&useLegacyDatetimeCode=false&serverTimezone=UTC") compile(jrxml) val jrprint:JasperPrint =

JasperFillManager.fillReport(generateCompileFileName(jrxml), reportParams, con)

val exporter:JRPdfExporter = new JRPdfExporter() exporter.setExporterInput(new SimpleExporterInput(jrprint)); exporter.setExporterOutput(

new SimpleOutputStreamExporterOutput(os));

exporter.exportReport()

new ByteArrayInputStream

((os.asInstanceOf[ByteArrayOutputStream]).toByteArray())

}catch {

case e:Exception => throw new RuntimeException(e) null

}

}

}

The main changes that we made were around the compile function. Now, we are using the Jasper JRXmlLoader in order to load the Jasper template from an InputStream method. Passing the InputStream method provided by the Play.resourceAsStream function. As you can see, we are passing the new path, /public/reports/, in order to get the templates. The rest of the code is pretty much the same as the one we executed earlier.

Now we are ready to deploy. In order to do so, we will need to run a command on activator, which is as follows:

$ activator universal:packageZipTarball

You will see the following result:

As soon as the task is finished, our app will be packed into the

ReactiveWebStore/target/universal/ directory, and you will see a reactivewebstore-1.0-SNAPSHOT.tgz file.

Then you need to extract the file and you shall have the following directory:

  • • reactivewebstore-1.0-SNAPSHOT
  • • bin: Scripts to run the app • conf: All config files: routes, logging, messages • bib: All JARs including third-party dependencies • share: All documentation about the app
 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >

Related topics