0
votes

I'm trying to implement JasperReport generation in Scala. I need to take the information from memory and merge it with the report.

This is what the documentation says, in Java:

HashMap[] reportDataSource = new HashMap[4];
// fill reportDataSource with information
JRMapArrayDataSource dataSource = new JRMapArrayDataSource(reportDataSource);
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream,
                                       new HashMap(), dataSource);

This is my attempt in Scala, where I get a compilation error in the line noted below:

val row1 = HashMap[String,String]("code" -> "B1", "name" -> "Bank 1")
val row2 = HashMap[String,String]("code" -> "B2", "name" -> "Bank 2")
val map = Array[HashMap[String,String]](row1,row2)  

val reportDataSource = new JRMapArrayDataSource(map)  // <-- this line does not compile

val baos = new ByteArrayOutputStream
JasperRunManager.runReportToPdfStream(blob.getBinaryStream, baos, params, reportDataSource)
Ok(baos.toByteArray).as("application/pdf")

The error (cannot convert from HashMap to Array[Object]):

type mismatch; found : Array[scala.collection.mutable.HashMap[String,String]] required: Array[Object] Note: scala.collection.mutable.HashMap[String,String] <: Object, but class Array is invariant in type T. You may wish to investigate a wildcard type such as _ <: Object. (SLS 3.2.10)>

How to do the conversion and fix the problem?

1
One thing that might work is to change the type of the array variable. As in val map = Array[Object](row1,row2)dada67

1 Answers

1
votes

You can very easily fix your error: val map = Array[Object](row1,row2)

The reason why your code did not compile in the first place, is that the type of map value is Array[HashMap[String, String]], while method only accepts Array[Object] (I recommend you to read about variance, but it is not important in the case of this problem).

On the other hand i assume that your code will still fail in runtime, because you are using scala HashMaps, while method you use probably requires java hash-maps