/*
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 java.io.FileOutputStream;

import com.inet.report.BorderProperties;
import com.inet.report.DatabaseField;
import com.inet.report.DatabaseTables;
import com.inet.report.Datasource;
import com.inet.report.Engine;
import com.inet.report.Field;
import com.inet.report.Paragraph;
import com.inet.report.RDC;
import com.inet.report.Section;
import com.inet.report.TableSource;
import com.inet.report.Text;
import com.inet.report.config.datasource.DataSourceConfiguration;
import com.inet.report.config.datasource.DataSourceConfigurationManager;

/**
 * This sample demonstrates how to use a bean data source for local execution.
 * Local means that the BeanDataSourceManager class is in classpath if this
 * code is executed. Please see {@link GenerateReportsServlet} for a sample using
 * i-net Clear Reports in an application server to execute reports.
 */
public class GenerateReportsDirect {
    
    /**
     * This will setup the connection to the bean data source. As this is a
     * local implementation we need the class name of the BeanDataSourceManager
     * implementation and user/password only.
     * @param ds the datasource that represents a connection to a database
     */
    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");
        ds.setDataSourceConfiguration(dsc);
    }
    
    /**
     * This will generate a report &quot;customers.pdf&quot; containing all
     * records provided by the bean data source for the bean
     * <i>samples.beans.Customer</i>.
     * @throws Exception if any Exception occurs.
     */
    public final void generateCustomers() throws Exception {
        //create a new report
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        TableSource ts = ds.createTableSource("samples.beans.Customer");
        ts.refresh();
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*2000,0, 1800, 200);
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*2000,0, 1800, 200);  
        }
        engine.execute();
        FileOutputStream out=new FileOutputStream("customers.pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }
    
    /**
     * This will generate a report
     * &quot;customers_sp_param_<i>paramValue</i>.pdf&quot;
     * containing all records provided by the bean data source for the bean
     * <i>samples.beans.Customer</i> where the filter <i>paramValue</i> applies.
     * @param paramValue The parameter to pass to the BeanDataSourceManager
     * implementation.
     * @throws Exception if any Exception occurs.
     */
    public final void generateCustomers(String paramValue) throws Exception {
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        TableSource ts = ds.createTableSource("SP_samples.beans.Customer");
        ts.setInputParameter(new String[] {"param"}, new int[] {Field.STRING});
        ts.refresh();
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*2000,0, 1800, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*2000,0, 1800, 200);  
        }
        engine.setPrompt("param", paramValue);
        engine.execute();
        if(paramValue.length()==0) {
            paramValue="all";
        }
        FileOutputStream out=new FileOutputStream("customers_sp_param_"+paramValue+".pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }
    
    

    /**
     * This will generate a report &quot;products.pdf&quot; containing all
     * records provided by the bean data source for the bean
     * <i>samples.beans.Product</i>.
     * @throws Exception if any Exception occurs.
     */
    public final void generateProducts() throws Exception {
        //create a new report
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        TableSource ts = ds.createTableSource("samples.beans.Product");
        ts.refresh();
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*2000, 0, 1800, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*2000, 0, 1800, 200);
        }
        engine.execute();
        FileOutputStream out=new FileOutputStream("products.pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }

    /**
     * This will generate a report
     * &quot;products_sp_param_<i>paramValue</i>.pdf&quot;
     * containing all records provided by the bean data source for the bean
     * <i>samples.beans.Product</i> where the filter <i>paramValue</i> applies.
     * @param paramValue The parameter to pass to the BeanDataSourceManager
     * implementation.
     * @throws Exception if any Exception occurs.
     */
    public final void generateProducts(String paramValue) throws Exception {
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        TableSource ts = ds.createTableSource("SP_samples.beans.Product");
        ts.setInputParameter(new String[] {"param"}, new int[] {Field.STRING});
        ts.refresh();
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*2000,0, 1800, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*2000,0, 1800, 200);  
        }
        engine.setPrompt("param", paramValue);
        engine.execute();
        if(paramValue.length()==0) {
            paramValue="all";
        }
        FileOutputStream out=new FileOutputStream("products_sp_param_"+paramValue+".pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }
    
    /**
     * This will generate a report &quot;productsAndCustomers.pdf&quot; containing the
     * records of both tables provided by the bean data source for the beans
     * <i>samples.beans.Product</i> and <i>samples.beans.Customer</i>.
     * @throws Exception if any Exception occurs.
     */
    public final void generateProductsAndCustomers() throws Exception {
        //create a new report
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        //create two tables
        TableSource ts1 = ds.createTableSource("samples.beans.Product");
        ts1.refresh();
        TableSource ts2 = ds.createTableSource("samples.beans.Customer");
        ts2.refresh();
        DatabaseField fromColumn = ts1.getDatabaseField( "id" );
        DatabaseField toColumn = ts2.getDatabaseField( "id" );
        //connect tables via inner join
        engine.getDatabaseTables().addJoin( ts1, fromColumn, ts2, toColumn, DatabaseTables.JOINTYPE_INNER, DatabaseTables.JOINLINK_EQUALS );
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts1.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*1200, 0, 1100, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*1200,0, 1100, 200);
        }
        int offset = fields.length;
        fields=ts2.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText((offset + i) * 1200, 0, 1100, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], (offset + i) * 1200, 0, 1100, 200);
        }
        engine.execute();
        FileOutputStream out=new FileOutputStream("productsAndCustomers.pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }
    
    /**
     * This will generate a report &quot;productsAndShipment.pdf&quot; containing all
     * records provided by the bean data source for the bean
     * <i>samples.beans.Product</i> and data of sub object <i>samples.beans.Shipment</i>.
     * @throws Exception if any Exception occurs.
     */
    public final void generateProductsAndShipment() throws Exception {
        //create a new report
        Engine engine=RDC.createEmptyEngine(Engine.EXPORT_PDF);
        Datasource ds=engine.getDatabaseTables().getDatasource(0);
        setupDataSourceConfiguration(ds);
        //increase the depth of data fetching in the bean and their sub objects
        ds.getDataSourceConfiguration().addProperty( "countOfSubBeans", "1" );
        TableSource ts = ds.createTableSource("samples.beans.Product");
        ts.refresh();
        Section header=engine.getArea(1).getSection(0);
        Section detail=engine.getArea(2).getSection(0);
        DatabaseField[] fields=ts.getDatabaseFields();
        for(int i=0;i<fields.length;i++) {
            Text text = header.addText(i*1700,0, 1600, 200);  
            text.setBottomLineStyle( BorderProperties.LINE_STYLE_SINGLE );
            Paragraph paragraph = text.addParagraph();
            paragraph.addTextPart(fields[i].getName());
            detail.addFieldElement(fields[i], i*1700,0, 1600, 200);
        }
        engine.execute();
        FileOutputStream out=new FileOutputStream("productsAndShipment.pdf");
        for(int i=1;i<=engine.getPageCount();i++) {
            out.write(engine.getPageData(i));
        }
        out.close();
    }
    
    /**
     * Generates all reports.
     * 
     * @throws Exception if any Exception occurs.
     */
    public final void generateAll()throws Exception {
        generateProducts();             //generates products.pdf
        generateProducts("");           //generates products_sp_param_all.pdf
        generateProducts("cars");       //generates products_sp_param_cars.pdf
        generateProducts("bikes");      //generates products_sp_param_bikes.pdf
        generateCustomers();            //generates customers.pdf
        generateCustomers("");          //generates customers_sp_param_all.pdf
        generateCustomers("germany");   //generates customers_sp_param_germany.pdf
        generateCustomers("world");     //generates customers_sp_param_world.pdf
        generateProductsAndCustomers(); //generates productsAndCustomers.pdf
        generateProductsAndShipment();  //generates productsAndShipment.pdf
    }
    
    /**
     * The main method.
     * 
     * @param args the arguments
     */
    public static void main(String[] args) {
        GenerateReportsDirect local=new GenerateReportsDirect();
        try {
            local.generateAll();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

}

