Showing posts with label JasperReports. Show all posts
Showing posts with label JasperReports. Show all posts

Friday, June 2, 2006

EJBQL query support added in JasperReports



My patch to support EJBQL queries inside JasperReports templates is included in the latest JasperReports 1.2.3 release. You can use any Java Persistence API implementation (part of JSR-220) like GlassFish/Toplink Essentials or Hibernate Entity Manager to provide data to your reports.

An example of using EJBQL in JasperReports templates is provided in the JasperReports project distribution, located in \demo\samples\ejbql. The example uses the Toplink Essentials Java Persistence API implementation from GlassFish. All necessary libraries to run the example are included in the JasperReports project distribution.

Running the example
Open a Command box and and navigate to JR_HOME\demo\samples\ejbql. Then run ant javac to compile the Java sources, ant compile to compile the JasperReports template files, ant fill to fill the compiled report design with data, and ant view to launch the report viewer to preview the report.

C:\Java Libs>cd jasperreports-1.2.3\demo\samples\ejbql

C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql>ant javacBuildfile:

build.xml

javac:

[mkdir] Created dir: C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\build\classes

[javac] Compiling 5 source files to C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\build\classes

[javac] Note: C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\EjbqlApp.java uses unchecked or unsafe operations.

[javac] Note: Recompile with -Xlint:unchecked for details.

[copy] Copying 1 file to C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\build\classes

[copy] Copying 1 file to C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\build

BUILD SUCCESSFUL

Total time: 2 seconds

C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql>ant compileBuildfile:

build.xml

compile:

[jrc] Compiling 3 report design files.

[jrc] File : C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\JRMDbVariaSubreport.jrxml ... OK.

[jrc] File : C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\JRMDbCastSubreport.jrxml ... OK.

[jrc] File : C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql\JRMDbReport.jrxml ... OK.

BUILD SUCCESSFUL

Total time: 3 seconds

C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql>ant fillBuildfile:

build.xml

fill:

[java] [TopLink Info]: 2006.06.02 08:55:20.906--ServerSession(30533424)--TopLink, version: Oracle TopLink Essentials - 2006.4 (Build 060412)

[java] [TopLink Info]: 2006.06.02 08:55:21.281--ServerSession(30533424)--file:/C:/Java%20Libs/jasperreports-1.2.3/demo/samples/ejbql/build/classes-pu1 login successful

[java] Filling time : 2125

[java] [TopLink Info]: 2006.06.02 08:55:23.015--ServerSession(30533424)--file:/C:/Java%20Libs/jasperreports-1.2.3/demo/samples/ejbql/build/classes-pu1 logout successful

BUILD SUCCESSFUL

Total time: 4 seconds

C:\Java Libs\jasperreports-1.2.3\demo\samples\ejbql>ant viewBuildfile:

build.xml

view:

BUILD SUCCESSFULTotal time: 4 seconds


Understanding the code
The ant fill target does the filling of the report by quering the HSQDB using the Java Persistence API. In the JRMDbReport.jrxml report template the following queryString was defined:

<queryString language="ejbql">

<![CDATA[

SELECT m

FROM Movie m

WHERE m.releaseDate BETWEEN $P{DateFrom} AND $P{DateTo}

ORDER BY $P!{OrderClause}]]>

</queryString>



By setting the language="ejbql" attribute, the JasperReports engine will use the Java Persistence API to retrieve the data. When filling the report an EnitiyManager instance need to be provided to the JasperReports engine. This is done in the main \demo\samples\ejbql\EjbqlApp.java class:



EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1", new HashMap());

EntityManager em = emf.createEntityManager();

Map parameters = new HashMap();

parameters.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, em);

JasperFillManager.fillReportToFile(fileName, parameters);



Note that this is only the part of the code to provide the EntityManager. Take a look at the \demo\samples\ejbql\EjbqlApp.java class for the complete code of the example.

The actual connection information to connect to the database is stored in the \demo\samples\ejbql\META-INF\persistence.xml:



<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="pu1">

<!-- Provider class name is required in Java SE -->

<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

<!-- All persistence classes must be listed -->

<class>Person</class>

<class>Movie</class>

<class>Cast</class>

<class>Varia</class>

<properties>

<!-- Provider-specific connection properties -->

<property name="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver"/>

<property name="toplink.jdbc.url" value="jdbc:hsqldb:db"/>

<property name="toplink.jdbc.user" value="sa"/>

<property name="toplink.jdbc.password" value=""/>

<!-- Provider-specific settings -->

<property name="toplink.logging.level" value="DEBUG"/>

<property name="toplink.platform.class.name" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>

</properties> </persistence-unit></persistence>


To read more about the EJBQL support in JasperReports, for example how to specify Query Hints, take a look at the JasperReports API documentation of the JRJpaQueryExecuter class.

Saturday, April 8, 2006

Using other query languages in JasperReports



In version 1.2.0 of JasperReports - The most popular open source reporting engine in the world - support for other query languages, including HQL and XPath, is added.

This means you can use HQL directly in report templates! The report engine will create a HibernateSessionFactory which uses your Hibernate configuration and mappings to connect to the database, build the SQL statement and return the mapped Java objects. Example of HQL queryString in report template:

<queryString language="hql">
<![CDATA[
from Cat cat
where cat.weight > ($P{CatWeight})]]>
</queryString>

Notice the language="hql" which indicates the report engine to use HQL instead of plain JDBC SQL. JasperReports introduced the JRQueryExecuterFactory and JRQueryExecuter interfaces to incorporate new/additional query languages easily by - just - implementing them. Currently there are query language implementations for JDBC, Hibernate and Xpath.

As the Java Persistence API, part of EJB 3.0/JEE 5.0, is becoming the standard I decided to create a patch to add support for JPA/EJBQL in JasperReports. The code is already submitted to the JasperReports team and I hope it will be included soon in a future release.

When released I will add a step-by-step example on using EJBQL queries in report templates.

Thursday, April 6, 2006



I've just successfully deployed the demo webapp of JasperReports - The most popular open source reporting engine in the world - on the latest GlassFish build (b42).

  1. Download the latest GlassFish build from https://glassfish.dev.java.net/public/downloadsindex.html.
  2. See the Instructions to unbundle and configure GlassFish and the GlassFish Quick Start Guide to get GlassFish up and running.
  3. Make sure GlassFish is running by checking http://localhost:8080.
  4. Download JasperReports (jasperreports-1.2.0-project.zip) from http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579.
  5. Unzip the downloaded JasperReports archive.
  6. Navigate to the the JasperReports demo webapp source directory.
    cd jasperreports-1.2.0\demo\samples\webapp
  7. Run the ant command and specify the war target.
    ant war
    When the build is finished the jasper-webapp.war should be in the \webapp directory.
  8. Copy jasper-webapp.war to GlassFish install-dir/domains/domain1/autodeploy. The JasperReports demo webapp will be deployed automatically.
  9. Goto the JasperReports demo webapp running on http://localhost:8080/jasper-webapp/. If you get a 404 wait a couple of seconds and refresh; probably the application was not yet deployed. Eventually you should see the JasperReports Web Application Sample.
  10. In the leftside menu click the compile link and compile the report template either using the JSP or Servlet Example.
  11. Now the report template is compiled you can execute the report itself with the PDF and HTML output links in the leftside menu. Both PDF and HTML output should work!

In a next entry I will explain how to use Java Persistence API EJBQL queries directly in report templates. This by using the new JasperReports 1.2.0 extendible JRQueryExectureFactory feature.