/*
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.beans;

import com.inet.report.Datasource;
import com.inet.report.config.datasource.DataSourceConfiguration;
import com.inet.report.config.datasource.DataSourceConfigurationManager;

/**
 * This sample demonstrates how to use a bean data source of a server 
 * installation of i-net Clear Reports. This means that i-net Clear Reports 
 * needs to be installed as a servlet application in a servlet environment 
 * (such as Tomcat) or in an application server (such as Websphere or Weblogic).
 * Therefore the classes BeanDataSourceManagerImpl, Customer and Product of this
 * package need to be deployed in a jar file into the lib sub directory of the
 * i-net Clear Reports servlet context. This sample will then request the data
 * from this i-net Clear Reports servlet installation. Please note that the
 * class BeanDataSourceManagerImpl should not be in classpath if this code
 * is executed.
 */
public class GenerateReportsServlet extends GenerateReportsDirect {

    /**
     * This is the URL specifying the path to the servlet context where i-net
     * Crystal-Clear ist installed and where the above mentioned sample classes
     * are installed. This is a sample for a tomcat server using port 8080 and
     * ClearReports as servlet context. Please change this value for your 
     * servlet environment or application server.
     */
    private static final String SERVLET_PATH="http://localhost:8080/ClearReports";
    
    /**
     * This will setup the connection to the bean data source. As this sample
     * should query the data from an i-net Clear Reports servlet installation
     * we need to specify the class name of the BeanDataSourceManager 
     * implementation, the servlet path to the TransferServlet used for 
     * communication between this sample and the servlet installation and the
     * user/password for user authentication.
     * @param ds the datasource that represents a connection to a database
     */
    @Override
    protected void setupDataSourceConfiguration(Datasource ds) {
        DataSourceConfiguration dsc;
        //delete any existing DataSourceConfiguration
        if(DataSourceConfigurationManager.exists("SampleBeanDataSourceLocal", DataSourceConfigurationManager.USER_PREFERENCES)) {
            DataSourceConfigurationManager.removeDatasourceConfiguration("SampleBeanDataSourceLocal", DataSourceConfigurationManager.USER_PREFERENCES);
        }
        //create the DataSourceConfiguration based on a bean data source
        dsc=DataSourceConfigurationManager.createDataSourceConfiguration("SampleBeanDataSourceLocal", DataSourceConfigurationManager.USER_PREFERENCES);
        //this will setup the configuration as bean data source
        dsc.setDatabaseClassname("com.inet.report.DatabaseBean");
        //as BeanDataSourceManagerImpl supports valid users only user/password
        //have to be set
        dsc.setUser("usera");
        dsc.setPassword("passworda");
        dsc.addProperty("beanDataSourceManager", "samples.beans.BeanDataSourceManagerImpl");
        dsc.addProperty("servletPath",SERVLET_PATH+"/TransferServlet");
        ds.setDataSourceConfiguration(dsc);
    }

    
    /**
     * The main method.
     * 
     * @param args the arguments
     */
    public static void main(String[] args) {
        //check if BeanDataSourceManagerImpl is not in classpath
        try {
            Class.forName("samples.beans.BeanDataSourceManagerImpl");
            System.err.println("To run this sample the class \"samples.beans.BeanDataSourceManagerImpl\" should not be in classpath");
            System.exit(1);
        } catch(ClassNotFoundException e1) {
            //we expected this exception and everything is alright
        }
        
        GenerateReportsServlet server=new GenerateReportsServlet();
        try {
            server.generateAll();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }    
}

