/*
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.property_checker;



import java.util.Properties;
import com.inet.report.Engine;
import com.inet.report.PropertiesChecker;
import com.inet.report.ReportException;

/**
 * Since i-net Crystal-Clear version 6.1, it is possible to provide a class called "PropertyChecker"
 * which, if it can be found in the class path, it is taken as the default PropertiesChecker
 * before the report is rendered. The easiest way to place this class in the classpath is
 * to jar it, and place the jar file in the i-net CrystalClear directory.
 * 
 * This sample PropertyChecker class has a simple functionality, which is to set the paper size
 * of a report using the properties "pwidth" and "pheight". These properties are checked shortly
 * before the report is rendered. If they exist, the engine method setPaperHeight and setPaperWidth
 * are used accordingly.
 * 
 * To give an example why this is so useful: using a PropertyChecker class such as this
 * enables you to utilize your own custom properties for your report and to change the report
 * at run-time, all while only accessing i-net Clear Reports through the Listener interface.
 * Only this class then is needed additionally to add functionality to the URL parameters set.
 * 
 * If this class is in the classpath, a URL such as:
 * 
 * http://myhost.com:9000?report=file:C:/test.rpt&pwidth=100000&pheight=20000
 * 
 * will cause test.rpt to be rendered with a page width of 100000 twips and a height of 20000.
 */
public class PropertyChecker implements PropertiesChecker {
    /**
     * This method is called right before the engine examines the report file. 
     * The properties object contains the values that the viewer has passed 
     * to the report server (e.g. in the report URL).
     * 
     * @param props The user properties.
     * @param req  The HTTP servlet request, either null or HttpServlet
     * 
     * @throws ReportException If you like to send an exception to the client then throw the ReportException.  
     * The ReportException will be send to the client and displayed in the Java viewer.
     * 
     * @see com.inet.report.PropertiesChecker#checkProperties(java.util.Properties, java.lang.Object)
     */
    public void checkProperties(Properties props, Object req) throws ReportException { }

    /**
     * This method is called just before the report is executed, and contains any properties
     * set for the report (e.g. in the report URL). 
     * In our case, we will set the paper width and height according to the pwidth and pheight parameters, 
     * if they were set.
     * Note these parameters are measured in twips = 1/20 of a point = 1/1440 of an inch
     * 
     * @param engine The initialized engine that have parsed the rpt file already.
     * @param props The user properties.
     * @param req The HTTP servlet request, either null or HttpServlet
     * 
     * @throws ReportException If you like to send an exception to the client then throw the ReportException.  
     * The ReportException will be send to the client and displayed in the Java viewer.
     * 
     * @see com.inet.report.PropertiesChecker#checkProperties(com.inet.report.Engine, java.util.Properties, java.lang.Object)
     */
    public void checkProperties(Engine engine, Properties props, Object req) throws ReportException {
        String width = props.getProperty("pwidth");
        if (width != null && width.length() > 0) {
            try {
                int widthInt = Integer.valueOf(width).intValue();
                engine.getReportProperties().setPaperWidth(widthInt);
            }
            catch (NumberFormatException e) {
                throw new ReportException(e.toString(),-500);
            }
        }
        String height = props.getProperty("pheight");
        if (height != null && height.length() > 0) {
            try {
                int heightInt = Integer.valueOf(height).intValue();
                engine.getReportProperties().setPaperHeight(heightInt);
            }
            catch (NumberFormatException e) {
                throw new ReportException(e.toString(),-500);
            }
        }
    }

    /**
     * This method is called before the report server creates and sends
     * back a HTML page with the applet tag for the viewer embedded. The properties
     * object contains the default properties for the HTML page.
     * 
     * @param props  The properties of the html page. Any change to this properties object itself has absolutely no effect.
     * 
     * @throws ReportException If an error occurred during checking the supplied properties
     * 
     * @see com.inet.report.PropertiesChecker#checkHtmlPageProperties(java.util.Properties)
     */
    public void checkHtmlPageProperties(Properties props) throws ReportException { }
}
