/*
@copyright(disclaimer)

DISCLAIMER OF WARRANTIES. The following [IBM Content Manager Enterprise Edition.] code is sample code created by IBM Corporation. 
IBM grants you a nonexclusive copyright license to use this sample code example to generate similar function tailored to your own 
specific needs. This sample code is not part of any standard IBM product and is provided to you solely for the purpose of assisting 
you in the development of your applications. This example has not been thoroughly tested under all conditions. IBM, therefore cannot 
guarantee nor may you imply reliability, serviceability, or function of these programs. The code is provided "AS IS", without warranty 
of any kind. IBM shall not be liable for any damages arising out of your or any other parties use of the sample code, even if IBM has 
been advised of the possibility of such damages. If you do not agree with these terms, do not use the sample code.

Licensed Materials - Property of IBM
5724-B19 / 5697-H60
� Copyright IBM Corp. 1994, 2011 All Rights Reserved.
US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

@endCopyright
*/

import com.ibm.mm.beans.*;
import com.ibm.mm.beans.util.*;

import java.beans.*;

public class DemoDriver2WithUI implements CMBConnectionReplyListener, TemplateChangeListener, CMBResultListener {
    SampleGuiLogon connGui = null;
    SampleGuiSchema schemaGui = null;
    SampleGuiTemplate stGui = null;
    SampleGuiResults resGui = null;
    UtilSimpleUI ui = new UtilSimpleUI();

    CMBConnection connBean = null;
    CMBSchemaManagement schemaBean = null;
    CMBSearchResults resultBean = null;
    CMBDataManagement dataBean = null;
    CMBTraceLog traceBean = null;

    public void onCMBConnectionReply(CMBConnectionReplyEvent evt) {
        int status = evt.getStatus();
        if (status == CMBBaseConstant.CMB_STATUS_OK) {

            schemaBean.setConnection(connBean);
            dataBean.setConnection(connBean);
            resultBean.setConnection(connBean);

            connGui.hide();
            schemaGui = new SampleGuiSchema();
            schemaGui.addCMBSchemaRequestListener(schemaBean);
            schemaGui.addTemplateChangeListener(this);
            schemaBean.addCMBSchemaReplyListener(schemaGui);
            resGui = new SampleGuiResults(connBean);
            resultBean.addCMBResultListener(resGui);
            resultBean.addCMBResultListener(this);
        }
    }

    public void onTemplateChange(TemplateChangeEvent evt) {
        String stName = (String) evt.getData();
        if (evt.getID() == TemplateChangeEvent.SAMPLE_TEMPLATE_SELECTED) {
            try {
                CMBSearchTemplate stObj = schemaBean.getSearchTemplate(stName);
                stObj.addCMBSearchReplyListener(resultBean);
                if (stGui == null) {
                    stGui = new SampleGuiTemplate(stObj);
                } else {
                    stGui.setTemplate(stObj);
                }
                resGui.updateUI(stObj);
            } catch (Exception ex) {
            }
        }
    }

    public void onCMBResult(CMBResultEvent evt) {
        int id = evt.getID();
        switch (id) {
            case CMBResultEvent.CMB_RESULT_UPDATED :
                int nHits = resultBean.getCount();
                System.out.println("There is " + nHits + " from the search");

                for (int k = 0; k < nHits; k++) {
                    try {
                        CMBHitItem hit = resultBean.getHitItem(k);
                        System.out.println("Hit item #" + k + ", pidString=" + hit.getPidString());
                        String[] values = hit.getAttrValue();
                        for (int l = 0; l < values.length; l++)
                            System.out.println("\tattrValue: " + values[l]);
                    } catch (CMBException ex) {
                        ex.printStackTrace();
                    }
                }

                break;
            case CMBResultEvent.CMB_RESULT_CLEARED :
                break;
            default :
                break;
        }
    }

    DemoDriver2WithUI() {
        try {
            System.out.println("Creating CMBConnection bean using " + "ser/class/jar specified in the classpath.");
            connBean = (com.ibm.mm.beans.CMBConnection) Beans.instantiate(null, "com.ibm.mm.beans.CMBConnection");
            if (connBean.getServerName().length() == 0) {
                String srv = ui.getString("Server name is empty, " + "please enter the server name:");
                connBean.setServerName(srv);
            }

            System.out.println("Creating CMBTraceLog bean using " + "ser/class/jar specified in the classpath.");
            traceBean = (com.ibm.mm.beans.util.CMBTraceLog) Beans.instantiate(null, "com.ibm.mm.beans.util.CMBTraceLog");
            connBean.setTraceEnabled(true);
            connBean.addCMBTraceListener(traceBean);
            connBean.addCMBConnectionReplyListener(this);

            connGui = new SampleGuiLogon();
            connGui.addVetoableChangeListener(connBean);
            connGui.addCMBConnectionRequestListener(connBean);

            schemaBean = (com.ibm.mm.beans.CMBSchemaManagement) Beans.instantiate(null, "com.ibm.mm.beans.CMBSchemaManagement");
            dataBean = (com.ibm.mm.beans.CMBDataManagement) Beans.instantiate(null, "com.ibm.mm.beans.CMBDataManagement");
            resultBean = (com.ibm.mm.beans.CMBSearchResults) Beans.instantiate(null, "com.ibm.mm.beans.CMBSearchResults");
            schemaBean.setTraceEnabled(true);
            schemaBean.addCMBTraceListener(traceBean);
            resultBean.setTraceEnabled(true);
            resultBean.addCMBTraceListener(traceBean);
            dataBean.setTraceEnabled(true);
            dataBean.addCMBTraceListener(traceBean);
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }

    public static void main(String argv[]) {
        String demoName = "IBM CMB Demonstration program: DemoDriver2WithUI";
        String copyright = "C) Copyright IBM Corp. 1994,1999. All Rights Reserved.";

        System.out.println("");
        System.out.println(demoName);
        System.out.println(copyright);
        System.out.println("");
        System.out.println("");

        DemoDriver2WithUI demo = new DemoDriver2WithUI();
        boolean loopControl = true;

        while (loopControl) {
            if (demo.schemaGui != null && !demo.schemaGui.isVisible())
                loopControl = false;
        }

        try {
            demo.connBean.disconnect();
        } catch (Exception e) {
        }
        demo.connGui.finalize();
        demo.schemaGui.finalize();
        demo.stGui.finalize();
        demo.resGui.finalize();
        System.out.println("Bye.");
        System.exit(0);
    }

}