import java.util.*;
import java.io.*;
import com.gestalt.scribe.*; 
public class Test4
{
/*
**  Demo of the app using Scribe API to produce a report and save it in an HTML data packet.
*/
    public static void main(String args[])
    {
        new Test4();
    }
    public Test4()
    {
/*
**      Connect to Scribe Server
*/
        System.out.println("Connecting...");
        Connector conn = null;
        try
        {
            conn = new Connector("localhost", 6541, "heidi", "hello");
        }
        catch (ServerConnectionException e)
        {
            System.out.println(e);
            System.exit(1);
        }
        System.out.println("DONE");
/*
**      Run report
*/
        String reportName = "Query/Customer";
        try
        {
/*
**          The report "Query/Customer" takes one parameter - customer ID.
**          We run this report for the customer "VINET". The parameter is
**          passed to the server as a Parameter object (part of the API library)
**          stored in a Vector. Any images that the report may create, should
**          be stored in C:\temp directory; inside the HTML files they will be
**          referenced as <IMG SRC="temp\imgN.jpeg">, relative to the
**          location of C:\Customer.html file.
*/
            Vector parameterList = conn.getReportParameters(reportName);
            Parameter packet = (Parameter) parameterList.elementAt(0);
            packet.setValue("VINET");
            HTMLData data = conn.getReportAsHTML(reportName, parameterList, "temp");
/*
**          getReportAsHTML() method of Connector returns a HTMLData object
**          with the content of HTML-formatted report that is saved in a file.
*/
            try
            {
                String fileName = "C:\\Customer.html";
                FileWriter f = new FileWriter(fileName);
                f.write(data.getHTMLText());
                f.flush();
                f.close();
/*
**              Any images created for the report are saved in JPEG files.
**              C:\temp directory will be created if it does nor exist.
*/
                File file = new File("C:\\temp");
                file.mkdir();
                Hashtable images = data.getImages();
                Set imgNames = images.keySet();
                Iterator iterator = imgNames.iterator();
                while (iterator.hasNext())
                {
                    String name = (String) iterator.next();
                    fileName = "C:\\temp\\" + name + ".jpeg";
                    FileOutputStream fos = new FileOutputStream(fileName);
                    fos.write((byte[]) images.get(name));
                    fos.flush();
                    fos.close(); 
                } 
            }
            catch (IOException e) { System.out.println(e); }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        conn.disconnect();
        System.exit(0);
    }
}