Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Treating nil datasources using JREmptyDataSource

  • Loading branch information...
commit 034ac0ee4bb6715570a5ade3ac23c55b95731c21 1 parent f2cfb04
@msaraiva msaraiva authored
View
21 lib/jasper-rails.rb
@@ -48,6 +48,7 @@ module JasperRails
JasperFillManager = Rjb::import 'net.sf.jasperreports.engine.JasperFillManager'
JasperPrint = Rjb::import 'net.sf.jasperreports.engine.JasperPrint'
JRXmlUtils = Rjb::import 'net.sf.jasperreports.engine.util.JRXmlUtils'
+ JREmptyDataSource = Rjb::import 'net.sf.jasperreports.engine.JREmptyDataSource'
# This is here to avoid the "already initialized constant QUERY_EXECUTER_FACTORY_PREFIX" warnings.
JRXPathQueryExecuterFactory = silence_warnings{Rjb::import 'net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory'}
InputSource = Rjb::import 'org.xml.sax.InputSource'
@@ -78,16 +79,20 @@ def self.render_pdf(jasper_file, datasource, parameters, options)
end
# Fill the report
- input_source = InputSource.new
- input_source.setCharacterStream(StringReader.new(datasource.to_xml(options).to_s))
- data_document = silence_warnings do
- # This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
- JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
+ if datasource
+ input_source = InputSource.new
+ input_source.setCharacterStream(StringReader.new(datasource.to_xml(options).to_s))
+ data_document = silence_warnings do
+ # This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
+ JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
+ end
+
+ jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
+ jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
+ else
+ jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params, JREmptyDataSource.new)
end
- jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
- jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
-
# Export it!
JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
rescue Exception=>e
View
12 spec/controllers/people_controller_spec.rb
@@ -47,7 +47,7 @@
response.should contain("I'm a parameter. I was defined in the controller")
end
-
+
end
describe "GET compile_time_error_report" do
@@ -66,4 +66,14 @@
end
+ describe "GET nil_datasource" do
+
+ it "should treat nil datasources" do
+ get :nil_datasource, :format => :pdf
+
+ response.should contain("I'm a parameter. I was defined in the controller")
+ end
+
+ end
+
end
View
5 spec/internal/app/controllers/people_controller.rb
@@ -17,4 +17,9 @@ def runtime_error_report
respond_with []
end
+ def nil_datasource
+ @parameter_defined_in_the_controller = "I'm a parameter. I was defined in the controller"
+ respond_with nil
+ end
+
end
View
118 spec/internal/app/views/people/nil_datasource.jrxml
@@ -0,0 +1,118 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="index" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="311ea5a2-cc66-43bf-9af3-b2e0a552b1de">
+ <property name="ireport.zoom" value="1.0"/>
+ <property name="ireport.x" value="0"/>
+ <property name="ireport.y" value="0"/>
+ <style name="Title" forecolor="#FFFFFF" fontName="Arial" fontSize="26" isBold="true" pdfFontName="Helvetica-Bold"/>
+ <style name="SubTitle" forecolor="#666666" fontName="Arial" fontSize="18"/>
+ <style name="Column header" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
+ <style name="Detail" fontName="Arial" fontSize="12"/>
+ <parameter name="parameter_defined_in_the_controller" class="java.lang.String"/>
+ <queryString language="xPath">
+ <![CDATA[/people/person]]>
+ </queryString>
+ <field name="name" class="java.lang.String">
+ <fieldDescription><![CDATA[name]]></fieldDescription>
+ </field>
+ <field name="email" class="java.lang.String">
+ <fieldDescription><![CDATA[email]]></fieldDescription>
+ </field>
+ <background>
+ <band splitType="Stretch"/>
+ </background>
+ <title>
+ <band height="70" splitType="Stretch">
+ <image>
+ <reportElement uuid="0b15b501-e65d-4f8b-b890-759a6d279efe" x="-20" y="0" width="595" height="64"/>
+ <imageExpression><![CDATA["wood.jpg"]]></imageExpression>
+ </image>
+ <staticText>
+ <reportElement uuid="dfeee47e-d32e-4525-a708-7cc3d6ec3f91" style="Title" x="0" y="5" width="263" height="33"/>
+ <textElement verticalAlignment="Middle"/>
+ <text><![CDATA[jasper-rails]]></text>
+ </staticText>
+ <textField>
+ <reportElement uuid="91699025-171b-40b4-ae45-eb9cdca69b2b" x="97" y="38" width="358" height="20" forecolor="#FFFFFF"/>
+ <textElement>
+ <font size="15"/>
+ </textElement>
+ <textFieldExpression><![CDATA[$P{parameter_defined_in_the_controller}]]></textFieldExpression>
+ </textField>
+ </band>
+ </title>
+ <pageHeader>
+ <band splitType="Stretch"/>
+ </pageHeader>
+ <columnHeader>
+ <band height="36" splitType="Stretch">
+ <line>
+ <reportElement uuid="6a63817d-d338-4966-9432-96e7baffc7f8" positionType="FixRelativeToBottom" x="0" y="35" width="555" height="1"/>
+ <graphicElement>
+ <pen lineWidth="0.5" lineColor="#999999"/>
+ </graphicElement>
+ </line>
+ <staticText>
+ <reportElement uuid="89d037e3-230c-4845-b0aa-a43920c60dc2" x="48" y="15" width="100" height="20"/>
+ <textElement/>
+ <text><![CDATA[name]]></text>
+ </staticText>
+ <staticText>
+ <reportElement uuid="6920786b-3719-4243-bee2-9e4073274cb1" x="245" y="15" width="100" height="20"/>
+ <textElement/>
+ <text><![CDATA[email]]></text>
+ </staticText>
+ </band>
+ </columnHeader>
+ <detail>
+ <band height="21" splitType="Stretch">
+ <textField>
+ <reportElement uuid="63d9b6a9-caa5-40d8-8bb0-41822dd43364" x="48" y="0" width="185" height="20"/>
+ <textElement/>
+ <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
+ </textField>
+ <textField>
+ <reportElement uuid="83b41533-c6ed-4de6-8c48-5545c9c78e43" x="245" y="0" width="290" height="20"/>
+ <textElement/>
+ <textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
+ </textField>
+ </band>
+ </detail>
+ <columnFooter>
+ <band height="45" splitType="Stretch">
+ <line>
+ <reportElement uuid="9642b713-530f-4d6e-a4dd-b6e7b4d07e2b" positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
+ <graphicElement>
+ <pen lineWidth="0.5" lineColor="#999999"/>
+ </graphicElement>
+ </line>
+ </band>
+ </columnFooter>
+ <pageFooter>
+ <band height="20" splitType="Stretch">
+ <textField>
+ <reportElement uuid="06b23f1b-7ac0-4c70-bb2e-2aeb039f67a3" style="Column header" x="433" y="0" width="80" height="20"/>
+ <textElement textAlignment="Right">
+ <font size="10" isBold="false"/>
+ </textElement>
+ <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
+ </textField>
+ <textField evaluationTime="Report">
+ <reportElement uuid="b8b7868e-fe9c-4e1b-8a24-75b97450ef73" style="Column header" x="513" y="0" width="40" height="20"/>
+ <textElement>
+ <font size="10" isBold="false"/>
+ </textElement>
+ <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
+ </textField>
+ <textField pattern="EEEEE dd MMMMM yyyy">
+ <reportElement uuid="f924a532-c0df-4366-baad-4126ffb1462f" style="Column header" x="0" y="0" width="197" height="20"/>
+ <textElement>
+ <font size="10" isBold="false"/>
+ </textElement>
+ <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
+ </textField>
+ </band>
+ </pageFooter>
+ <summary>
+ <band splitType="Stretch"/>
+ </summary>
+</jasperReport>
View
1  spec/internal/config/routes.rb
@@ -3,6 +3,7 @@
collection do
get 'compile_time_error_report'
get 'runtime_error_report'
+ get 'nil_datasource'
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.