/*
  i-net software provides programming examples for illustration only, without warranty
  either expressed or implied, including, but not limited to, the implied warranties
  of merchantability and/or fitness for a particular purpose. This programming example
  assumes that you are familiar with the programming language being demonstrated and
  the tools used to create and debug procedures. i-net software support professionals
  can help explain the functionality of a particular procedure, but they will not modify
  these examples to provide added functionality or construct procedures to meet your
  specific needs.
  © i-net software 1998-2010
*/

/**
 * With this database class it is possible to set the data for main and sub-report with an external 
 * Result Set instead of using a connection to a database created by i-net Clear Reports.
 *
 * To do this you have to 
 * 1) create a Data Source Configuration with a user-defined driver for this database class
 * 2) set the name of the Data Source Configuration in the parameter datasource at runtime
 * 
 * Create a Data Source Configuration using the Data Source Manager:
 * - select "user-defined driver"
 * - set a name for the Data Source Configuration, e.g. "MainAndSubDataWithExternalResultSet"
 * - set this class "samples.jdbc.Main_and_Subreport_Data_WithExternalResultSet" as Database Class for the Data Source Configuration
 * 
 * In the report url you have to set the name of the Data Source Configuration in the datasource parameter:
 * ...datasource=MainAndSubDataWithExternalResultSet...
 */

package samples.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.inet.report.Engine;


/**
 * shows how you can distinguish between main and sub report and provide different data for each
 * with your own Database class.
 * See {@link DatabaseClassUsableInDesigner} for instructions on how to add this class as a datasource from
 * within i-net Designer.
 */
public class MainAndSubreportDataWithExternalResultSet extends com.inet.report.Database {

    // No database connection
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean useJdbcDriver(){
        return false;
    }
    
    // Sample for creating the resultset by executing a SQL Query with a JDBC driver from i-net software.
    // This method will be called once for the main report and once for each sub report.
    /**
     * {@inheritDoc}
     */
    @Override
    public void getReportData( Engine engine, String configs ) {
        ResultSet rs = null;
        Statement st = null;
        
        /* Methods to see which report or subreport is working with this engine
        engine.getReportTitle();
        engine.getPrompts();
        ...
        */
       
        try {
            Class.forName("com.inet.tds.TdsDriver").newInstance(); // your JDBC driver
            String url = "jdbc:inetdae:<serverName>?database=<dbName>"; // your JDBC connection URL
            Connection con = DriverManager.getConnection(url, "<userName>", "<pwd>");   // your connection info
            st = con.createStatement();
            // The column name(s) need to be the same as in the report (rpt file)
            // If this result set have different column names as the report then you can use alias names.
            Engine parentEngine = engine.getParent();
            if ( parentEngine == null ) {
                // data for the main report 
                System.out.println("Setting data for the main report");
                rs = st.executeQuery("Select * From ...");
            } else {
                // data for the sub report
                // here you can use the engine API to determine the subreport, e.g. Engine.getReportTitle()
                System.out.println("Setting data for the sub report");
                rs = st.executeQuery("Select * From ...");
            }
            engine.setData( rs );
        } catch (Exception e) {
            e.printStackTrace();
            //...
        } finally {
            try {
                st.close();
                st = null;
            } catch (Exception e) {
                e.printStackTrace();
                //...
            }
            try {
                rs.close();
                rs = null;
            } catch (Exception e) {
                e.printStackTrace();
                //...
            }
        }
    }   
}
