/*
  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 report data 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. "DataWithExternalResultSet"
 * - set this class "samples.jdbc.DataWithExternalResultSet" 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=DataWithExternalResultSet...
 */

package samples.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.inet.report.Engine;

/**
 * An example of a Database class which can be used as a Datasource for your report. This gives you greater
 * flexibility over the data for your report.
 * 
 * See {@link DatabaseClassUsableInDesigner} for instructions on how to add this class as a datasource from
 * within i-net Designer.
 */
public class DataWithExternalResultSet extends com.inet.report.Database {

    private ResultSet rs;
    
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean useJdbcDriver(){
        return false;
    }
    
    /**
     * example for creating a simple result set (the URL would need to be your own JDBC connection URL)
     */
    private void fetchResultSet() {
        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
            Statement 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 with "AS".
            rs = st.executeQuery("Select ... ");
        } catch (Exception e) {
            e.printStackTrace();
            //...
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void getReportData( Engine engine, String configs ) {
        
        /* Methods to see which report or subreport is working with this engine
        engine.getReportTitle();
        engine.getPrompts();
        ...
        */
       
        fetchResultSet();
        
        try {
            engine.setData( rs );
        } catch (Exception e) {
            e.printStackTrace();
            //...
        }
    }   
}
