package data_sources

import net.sf.jasperreports.engine.JRDataSource
import scala.xml.Elem
import java.io.ByteArrayInputStream
import net.sf.jasperreports.engine.data.JRXmlDataSource

case class XmlDataSource(val source:Elem) extends ReportDataSource {
  val claimElement = source \\ "DWPCATransaction" \\ "DWPCAClaim"
  val claimJasperTemplate = "reportNewClaim"
  val circsElement = source \\ "DWPCATransaction" \\ "DWPCAChangeOfCircumstances"
  val circsJasperTemplate = "reportNewCircs"


  def convertToJRDataSource(): JRDataSource = {
    new JRXmlDataSource(new ByteArrayInputStream(source.toString().getBytes("UTF-8")))
  }

  def jasperReportFilenameMatcher(): String = {
    circsElement.isEmpty match {
      case true => {
        claimElement.isEmpty match {
          case true => throw new scala.RuntimeException("XmlDataSource did not contain an XML element that matches a jasper template")
          case false => claimJasperTemplate
        }
      }
      case false => circsJasperTemplate
    }
  }
}
package data_sources

import net.sf.jasperreports.engine.JRDataSource
import scala.xml.Elem
import java.io.ByteArrayInputStream
import net.sf.jasperreports.engine.data.JRXmlDataSource

case class XmlDataSource(val source:Elem) extends ReportDataSource {

  def convertToJRDataSource(): JRDataSource = {
    new JRXmlDataSource(new ByteArrayInputStream(source.toString().getBytes("UTF-8")))
  }

  def jasperReportFilenameMatcher(): String = {
    (source \\ "DWPCATransaction" \\ "DWPCAChangeOfCircumstances").length match {
      case 0 => {
        (source \\ "DWPCATransaction" \\ "DWPCAClaim").length match {
          case 0 => throw new scala.RuntimeException("XmlDataSource could not match a report template")
          case _ => "reportNewClaim"
        }
      }
      case _ => "reportNewCircs"
    }
  }
}
--- 
+++ 
@@ -6,20 +6,25 @@
 import net.sf.jasperreports.engine.data.JRXmlDataSource
 
 case class XmlDataSource(val source:Elem) extends ReportDataSource {
+  val claimElement = source \\ "DWPCATransaction" \\ "DWPCAClaim"
+  val claimJasperTemplate = "reportNewClaim"
+  val circsElement = source \\ "DWPCATransaction" \\ "DWPCAChangeOfCircumstances"
+  val circsJasperTemplate = "reportNewCircs"
+
 
   def convertToJRDataSource(): JRDataSource = {
     new JRXmlDataSource(new ByteArrayInputStream(source.toString().getBytes("UTF-8")))
   }
 
   def jasperReportFilenameMatcher(): String = {
-    (source \\ "DWPCATransaction" \\ "DWPCAChangeOfCircumstances").length match {
-      case 0 => {
-        (source \\ "DWPCATransaction" \\ "DWPCAClaim").length match {
-          case 0 => throw new scala.RuntimeException("XmlDataSource could not match a report template")
-          case _ => "reportNewClaim"
+    circsElement.isEmpty match {
+      case true => {
+        claimElement.isEmpty match {
+          case true => throw new scala.RuntimeException("XmlDataSource did not contain an XML element that matches a jasper template")
+          case false => claimJasperTemplate
         }
       }
-      case _ => "reportNewCircs"
+      case false => circsJasperTemplate
     }
   }
 }
Refactor to move strings and xpaths  into vals
