Jump to content
  • Using a custom DataSourceProvider in iReport Designer with custom datasources


    gdmoreno

    Introduction

    In this article, we take a look at how to use a custom datasource in iReport Designer. For this example article, we will use the example custom datasource we developed in another article, the one about pulling data from the Yahoo Finance website.

    One easy way to use the custom datasource is to first implement a custom DataSourceProvider class, and have that class manage the instantiation of the custom datasource. By packaging up these two classes (and any dependent classes) into a JAR, and then adding that JAR file to the iReport Designer class, we can create a datasource in iReport Designer of the "JRDataSourceProvider" type, and then design a report as we normally would.

    This article uses the Yahoo Finance custom datasource as an example to illustrate that process.

    The JRDataSourceProvider interface

    The JRDataSourceProvider interface is a simple interface to implement; in our example we only care about implementing the "create" method (it instantiates the custom datasource) and the "getFields" method, which iReport Designer calls to figure out the fields that the report will have for the custom datasource.

    package net.sf.jasperreports.engine; 

    public interface JRDataSourceProvider

    {

      public boolean supportsGetFieldsOperation(); 

      public JRField getFields(JasperReport report)

      throws JRException, UnsupportedOperationException; 

      public JRDataSource create(JasperReport report) throws JRException; 

      public void dispose(JRDataSource dataSource) throws JRException;

    Implementing the YahooFinanceDataSourceProvider class

    Development requirements

    To develop this example class, I created an Eclipse project and added the JAR files from JasperReports Server's WEB-INF/lib directory, as well as the JAR files from Tomcat's lib directory. There is no need to implement a constructor in this example, or to maintain object variables. For the "dispose" method, I gave it an empty implementation.

    Implementing the "supportsGetFieldsOperation" method

    This method is straightforward, all you need to do is return true if the data source provider indicates that it is able to introspect the data source and discover the available fields, and false otherwise.

    public boolean supportsGetFieldsOperation() {

      // TODO Auto-generated method stub

      return false;

    }

    In my example I'm returning false, because instead of doing an introspection of the data, I'm defining the datasource fields elsewhere. But that's just a development choice I made for the sake of this example.

    Implementing the "create" method

    Implementing this method is straightforward too, all it does is create an instance of the custom datasource object. Of course, you can add more complexity to how you create the custom datasource, but the essence of the problem is that you create the custom datasource in this method.

    public JRDataSource create(JasperReport report) throws JRException {

      // TODO Auto-generated method stub

      return new YahooFinanceDataSource();

    Implementing the "getFields" method

    This method returns an array of JRField objects that represent the datasource's available fields for designing reports. In this method, I call a static method on the custom datasource class (the YahooFinanceDataSource.fieldNames() call) that returns the list of field names as a String array, which I then convert to JRField objects, returning an array of JRFields objects in the method's last line.

    public JRField[] getFields(JasperReport report) throws JRException, UnsupportedOperationException {

      ArrayList fields = new ArrayList();

      String [] fieldNames = YahooFinanceDataSource.fieldNames();

      for (String s : fieldNames) {

        JRDesignField field = new JRDesignField();

        field.setName(s);

        field.setValueClassName("java.lang.String");

        fields.add(field);

      }

      return (JRField[]) fields.toArray(new JRField[fields.size()]);

    }

    Process for deploying to iReport Designer

    The process is straightforward, but it does involve several steps.

    Package up a JAR file

    This JAR file needs to contain the custom DataSourceProvider and datasource classes you developed, as well as any dependent classes. You can do it in Eclipse by right-clicking on the project name and choosing the export function; you'll need to go through several steps, and in the end, Eclipse will create a JAR file for you on your file system.

    Add the JAR file to the iReport Designer classpath

    After launching iReport Designer, go to "Tools" then "Options", and then a new window should appear in the foreground. Choose the "Classpath" tab, and then hit the "Add JAR" button. Browse to the location where you saved your JAR from the previous step and hit "OK" to finish the process.

    Add the custom datasource in iReport Designer as a "report datasource"

    Click on the "report datasources" button (the fourth icon from the top left), and you should get a "Connections / Datasources" pop-up like the one below:

    Datasourceprovider1.png.6f06a71aa569dc8e7ae29d5573e42552.png

    1. Click on "New"
    2. Select the "JRDataSourceProvider" option and hit "Next."
    3. On the next step, give it a name (it doesn't matter what it is), and then fill in the fully qualified class name of the custom DataSourceProvider class you developed earlier.
    4. Hit the "Test" button to confirm success
    5. Save the connection

    Create the report query

    Now we will create the report query, which is in fact just referencing the fields that are available in the custom datasource. Click on the report query button, click on the "DataSource Provider" tab, and hit the "Get fields from datasource" button; that should generate the fields that are available in the custom datasource.

    If you've successfully gotten to this point, you can hit the "Refresh Preview Data" button, and it will go ahead and display the data that the custom datasource contains, such as in the image below:

    Datasourceprovider2.png.f6ac0304a2fe8ec10fffa4b88aae6287.png

    Now you can go ahead and design a report as you normally would, such as in the screenshot below:

    Datasourceprovider3.png.ec72fa22144f9a1664cdf7d0ff5fd858.png

    And when clicking the "Preview" button, you can achieve the usual result you get when creating reports, such as what you see below:Datasourceprovider4.png.e77b053d3e99832d39d430a6e112d9a5.png

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...