Crystal Reports for Eclipse Developer Guide

To create a class that listens to data source events

Before you can complete the steps in this example, you must have a plug-in project that includes the com.businessobjects.crystalreports.designer.sdk library and defines the DataSourceEvents extension point. This example assumes that you have created a report from a POJO object
This example shows how to create a class that implements the IDataSourceChangedListener interface. You can connect this class to the DataSourceEvents extension point, and populate the report with data when the database is refreshed or validated.
Note: To populate the data source when the report editor is opened, extend the editorOpened event.
  1. Click File > New > Class
    The" New Java Class" dialog box appears.
  2. Type the name of the class in the <Name> field.
    For example, type MyDataSourceListener.
  3. Click Add.
    The "Implemented Interfaces Selection" dialog box appears.
  4. Type com.businessobjects.crystalreports.designer.sdk.IDataSourceChangedListener and click OK.
  5. Click Finish.
    A class is created that implements the IDataSourceChangedListener interface.
  6. Include the import statements used in this example.
    import java.util.ArrayList;
    import java.util.List;
    
    import com.businessobjects.crystalreports.designer.sdk.IDataSourceChangedListener;
    import com.crystaldecisions.sdk.occa.report.application.IReportClientDocument;
    import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
    import com.crystaldecisions.sdk.occa.report.data.ITable;
    import com.crystaldecisions.sdk.occa.report.data.Tables;
    import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
  7. Create a method to populate a data source.

    Note: In this example we use a POJO data source. You can populate the data source using any supported method.
      a. Create the populateDataSource method.
      public void populateDataSource(IReportClientDocument rcd, String srcEvent) throws ReportSDKException
      {
      b. Find the database table to modify.
      This example assumes that the report contains a table named BasicPOJO.
        String tableName = "BasicPOJO";
      
        boolean tableFound = false;
        Tables allTables = rcd.getDatabaseController().getDatabase().getTables();
        String tableAlias = null;
      
        for (ITable table : allTables)
        {
          if (tableName.equals(table.getName()))
          {
            tableFound = true;
            tableAlias = table.getAlias();
          }
        }
        if (!tableFound)
           return;
      c. Create a list of POJO objects that represent the new datasource.
        List <BasicPOJO> allData = new ArrayList<BasicPOJO>();
        for (int i = 0; i < 10; i ++)
        {
           allData.add (new BasicPOJO (i, srcEvent));
        }
      d. Set the data in to the database controller.
        rcd.getDatabaseController().setDataSource(allData, BasicPOJO.class, tableAlias, tableAlias);
      }
  8. Implement the dataSourceChanged method to udpate the data source of the main report and suberports.
    The dataSourceChanged method is called when the report is refreshed or the database is verified.
      a. Get the ReportClientDocument object from the main report.
      public void dataSourceChanged(IReportClientDocument rcd)
      {
        ReportClientDocument doc = rcd.getMainReport();
      b. Populate the data source of the main report.
        populateDataSource(doc, "dataSourceChanged");
      c. Populate the subreport datasources.
        for (String subreportName : doc.getSubreportController().getSubreportNames())
        {
          IReportClientDocument srcd = doc.getSubreportController().getSubreport(subreportName);
          populateDataSource(srcd, "dataSourceChanged");
        }
      }
      d. Catch ReportSDKException exceptions.
      try
      { 
         ...  
      }
      catch (ReportSDKException e)
      {
        e.printStackTrace();
      } 
      
Example: Populating report data sources
import java.util.ArrayList;
import java.util.List;

import com.businessobjects.crystalreports.designer.sdk.IDataSourceChangedListener;
import com.crystaldecisions.sdk.occa.report.application.IReportClientDocument;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.ITable;
import com.crystaldecisions.sdk.occa.report.data.Tables;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;

public class MyDataSourceListener implements IDataSourceChangedListener
{ 
  private void populateDataSource(IReportClientDocument rcd, String srcEvent) throws ReportSDKException
  {
    String tableName = "BasicPOJO";

    boolean tableFound = false;
    Tables allTables = rcd.getDatabaseController().getDatabase().getTables();
    
    String tableAlias = null;
    for (ITable table : allTables)
    {
      if (tableName.equals(table.getName()))
      {
        tableFound = true;
        tableAlias = table.getAlias();
      }
    }
    if (!tableFound)
      return;
   
    List <BasicPOJO> allData = new ArrayList<BasicPOJO>();
    for (int i = 0; i < 10; i ++)
    {
      allData.add (new BasicPOJO (i, srcEvent));
    }
    rcd.getDatabaseController().setDataSource(allData, BasicPOJO.class, tableAlias, tableAlias);
  }

  public void dataSourceChanged(IReportClientDocument rcd)
  {
    try
    {
      ReportClientDocument doc = rcd.getMainReport();
      populateDataSource(doc, "dataSourceChanged");
   
      for (String subreportName : doc.getSubreportController().getSubreportNames())
      {
        IReportClientDocument srcd = doc.getSubreportController().getSubreport(subreportName);
        populateDataSource(srcd, "dataSourceChanged");
      }
    }
    catch (ReportSDKException e)
    {
      e.printStackTrace();
    } 
  }
}



SAP BusinessObjects
http://www.sap.com/sapbusinessobjects/
Support services
http://service.sap.com/bosap-support/
Product Documentation on the Web
http://help.sap.com/