/*
  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.servlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import com.inet.report.*;

/**
This sample class extends the ReportServlet.
It demonstrates you how you can set the connection for one or for all reports.

To use this servlet instead of the ReportServlet you need to (sample for Tomcat):
  - copy this file in: "<TOMCAT-HOME>/webapps/<SERVLET-CONTEXT>/WEB-INF/classes/" 
    ( <SERVLET-CONTEXT> is the context that you have specified in the i-net Clear Reports setup )
  - compile this class
  - replace the servlet-class "com.inet.report.ReportServlet" in the file: "TOMCAT-HOME>/webapps/<SERVLET-CONTEXT>/WEB-INF/web.xml"
    with the name of your servlet class: <servlet-class>ReportServletSetConnectionInCheckProperties</servlet-class>
  - call this servlet with: 
    http://<serverName>:8080/<servlet-context>/sample.rpt
     or
    http://<serverName>:8080/<servlet-context>/<url-pattern>?report=file:c:/reports/myReport.rpt
    ( <url-pattern> is configured in the file: "TOMCAT-HOME>/webapps/<SERVLET-CONTEXT>/WEB-INF/web.xml", 
    default <url-pattern>: ReportServlet ):
    http://<serverName>:8080/<servlet-context>/ReportServlet?report=file:c:/reports/myReport.rpt
*/

public class ReportServletSetConnectionInCheckProperties extends ReportServlet {

    /**
     * Creates a database connection.
     * @return created database connection
     */
    private Connection createConnection() {
        try {
            // e.g. Connection to an Oracle database
            DriverManager.setLogStream(System.out);
            Class.forName("com.inet.ora.OraDriver").newInstance();
            String url = "jdbc:inetora:DBServerName:Port:ORCL";
            return DriverManager.getConnection(url, "usr", "pwd");
        } catch(Throwable e){
            e.printStackTrace();
            return null;
        }
    }

    /** 
     * This method from the ReportServlet you need to overwrite to read, set and/or modify the parameter values.
     * This method is empty in the class com.inet.report.ReportServlet.
     * 
     * @param engine  The initialized engine.
     * @param reportProperties The user properties.
	 * @param servletRequest  The HTTP servlet request
	 * 
	 * @throws ReportException If an error occurred during checking the 
     * supplied properties with the current PropertiesChecker. 
     * The ReportException will be send to the client and displayed in the Java viewer.
     */
    public void checkProperties(Engine engine, Properties reportProperties, Object servletRequest) throws ReportException {
        // set the database connection for a specific report
        if ((reportProperties.getProperty("report").toLowerCase()).endsWith("ReportName.rpt")) {  //<<--- Change the report name
            try {
                // Set the connection for the main report
                Datasource ds = engine.getDatabaseTables().getDatasource(0);
                ds.setConnection(createConnection());
                // Set the connection for the existing sub report(s)
                int subReportCount = engine.getSubReportCount();
                System.out.println("engine.getParent(): "+engine.getParent());
                System.out.println("subReportCount: "+subReportCount);
                for (int i=0;i<subReportCount;i++) {
                    Datasource dsSub = engine.getSubReport(i).getDatabaseTables().getDatasource(0);
                    dsSub.setConnection(createConnection());
                }
                System.out.println("Connections are ok.");
                // if a schema is saved in the rpt file that does not exist in the database to that this connection is connected.
                reportProperties.put("schema",""); 
            } catch (Throwable e) {e.printStackTrace();}
        }
        super.checkProperties(engine, reportProperties, servletRequest);
    }
}

