/*
  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.standalone;

import java.util.Properties;
import com.inet.report.*;

/**
 * This sample shows the usage of the method checkProperties.
 * It extends the com.inet.report.Listener and it shows you how you can read,
 * change or add parameter values of/to the report properties.
 * You only need to overwrite the method checkProperties(Properties reportProperties)
 * to read, change or add the value(s) of the report properties like datasource, prompt# etc.
 * 
 * Please refer to the i-net Clear Reports documentation for the list of all parameters 
 * that you can read, change or add in/to the report properties. You can install 
 * the i-net Clear Reports documentation using the setup tool or you can read it online:
 * http://www.inetsoftware.de/documentation/crystal-clear/online-help/features/report-url-parameters.html.
 * 
 * You need to call
 *     static Listener reportListener = new ListenerWithMyReportProperties(true);
 * instead of
 *     static Listener reportListener = new Listener(true);
 * in your Java application.
 */
public class ListenerWithMyReportProperties extends Listener {

    /**
     * Starts the listener.
     * @param standalone if true then this is a listener of a standalone application
     */
    public ListenerWithMyReportProperties(boolean standalone) {
        super( standalone, 0);
    }

    /**
     * Main method of this sample
     * @param args arguments not used
     */
    public static void main(String[] args) {
        try {
            new ListenerWithMyReportProperties(false);
        } catch(Throwable e){e.printStackTrace();}
    }

    /**
     * You need to override this method from the Listener to modify the parameter values.
     * This method is empty in the com.inet.report.Listener.
     * @param reportProperties The properties of the report that are specified in the report URL.
     * @param req The HTTP servlet request, either null or HttpServlet
     * 
     * @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(Properties reportProperties, Object req) throws ReportException{
        String currentReportName;
        currentReportName = ((String)reportProperties.get("report")).toLowerCase();
        // Check report name
        if (currentReportName.indexOf("report1.rpt") != -1) {
            // Change report parameter values
            // for Report1 we change the password and the RecordSelectionFormular
            reportProperties.put("password", "Password");
            reportProperties.put("sf",       "{List.UserID} = 404");
        } else if (currentReportName.indexOf("report2.rpt") != -1) {
            // Change report parameter values
            // here we set a date in a parameter field
            reportProperties.put("prompt0",  "Date(2000,05,18)");
        } else if (currentReportName.indexOf("report3.rpt") != -1) {
            // Change report parameter values
            // for this report we change the database
            // Example: Report3 was designed with the ODBC driver
            // Now we will use this report with the MS SQL Server
            reportProperties.put("datasource", "<Data Source Configuration Name>" ); // name of the Data Source Configuration
                                                                                     // that should be used for connection to SQL Server);         
            reportProperties.put("password",   "Password");
            reportProperties.put("prompt0",    "Date(2005,05,18)");  // a date in a parameter field
        } else if (currentReportName.indexOf("report4.rpt") != -1) {
            // Change report parameter values
            // for this report we change the database
            // Example: Report4 was designed with the ODBC driver
            // Now we will use this report with the Oracle Database
            reportProperties.put("datasource", "<Data Source Configuration Name>" ); // name of the Data Source Configuration 
                                                                                     // that should be used for connection to Oracle);
            reportProperties.put("password",   "Password");
            reportProperties.put("prompt0",    "Date(2008,01,15)");  // a date in a parameter field
            reportProperties.put("prompt1",    "2518");              // a number in the second parameter field
        }
        // report5.rpt should be exported to PDF
        else if (currentReportName.indexOf("report5.rpt") != -1) {
            reportProperties.put("init", "pdf");
        }
        super.checkProperties(reportProperties, req);
    }
}
