/*
  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
*/

package samples.jdbc;

import java.sql.*;

import com.inet.report.*;

/**
 * 
 * This sample shows how to define a Database class that can be used for report design and report execution.
 * This class defines one table with three string columns, that will be shown in i-net Designer - Database Wizard.
 * This sample also demonstrates the usage of the public class com.inet.report.DatabaseMetaDataFactory that is helpful 
 * to create meta data Result Sets expected from i-net Designer. 
 * 
 * To make this class usable by i-net Designer you have to add it to class path and 
 * you have to create a Data Source Configuration with a user-defined driver for this database class.
 * 
 * Adding to class path: 
 * To add this class file to the class path, you can create a jar file containing this class 
 * (with command "jar cf custom-database-class.jar DatabaseClassUsableInDesigner.class")
 * and copying the created jar file into the directory "lib/driver" that is included in the installation directory 
 * of i-net Clear Reports or i-net Designer.
 * Also, have a look at http://www.inetsoftware.de/documentation/crystal-clear/online-help/howto/library.html.
 * 
 * Create a Data Source Configuration using the Data Source Manager:
 * - select "user-defined driver"
 * - set a name for the Data Source Configuration, e.g. "DesignerDatabaseClass"
 * - set this class "samples.jdbc.DatabaseClassUsableInDesigner" as Database Class for the Data Source Configuration
 * 
 * When you design your report in i-net Designer you have to choose the data source "DesignerDatabaseClass" in the Configuration Manager  
 * and you'll be able to design a report based on this Database class. In the Database Wizard you can add the table "customers" to the report.
 */
public class DatabaseClassUsableInDesigner extends Database {

    private final static String     TABLE_NAME =         "customers";
    private final static String[]   TABLE_COLUMN_NAMES =  new String[]  {"customerid",   "companyname",  "address"}; 
    private final static int[]      TABLE_COLUMN_TYPES =  new int[]     {Types.VARCHAR,  Types.VARCHAR,  Types.VARCHAR}; 
    private final static Object[][] TABLE_DATA =         new Object[][]{{"ALFKI","Alfreds Futterkiste",                "Obere Str. 57"},
                                                                       {"ANATR","Ana Trujillo Emparedados y helados", "Avda. de la Constitución 2222"}};    
    
    // This effects that the table "customers" will shown in Database Wizard of i-net Designer
    /**
     * {@inheritDoc}
     */
    @Override
    public ResultSet getTables(Datasource con, String catalog)throws SQLException {
        return DatabaseMetaDataFactory.getTables(new String[]{TABLE_NAME},catalog); 
    }
    
    // This effects that the used table also has columns
    /**
     * {@inheritDoc}
     */
    @Override
    public ResultSet getColumns(Datasource con, String cat, String own, String tab) throws SQLException {
        if(tab.equalsIgnoreCase(TABLE_NAME)) {
            return DatabaseMetaDataFactory.getColumns(TABLE_COLUMN_NAMES,TABLE_COLUMN_TYPES);
        } else {
            throw new SQLException("unknown table "+tab);
        }
    }
    
    // Sets the data for report at runtime
    /**
     * {@inheritDoc}
     */
    @Override
    public void getReportData(Engine engine, String configs)throws ReportException {
        String[] columns = new String[TABLE_COLUMN_NAMES.length];
        for (int i = 0; i < columns.length; i++) {
                columns[i]=TABLE_NAME+"."+TABLE_COLUMN_NAMES[i];//set full qualified column name
        }
        engine.setData(columns,TABLE_DATA);
    }

    // No JDBC Connection is required
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean useJdbcDriver() {
        return false;
    }
}
