D:\DEV\PROJECTS\Harbor_dB_Demo_Ship\src\harbor_db_demo_ship\CD_Unit.java
/*
 * CD_Unit.java
 */

package harbor_db_demo_ship;
import harbor.dbDemo.app.ui.interfaces.*;
import harbor.dbDemo.app.srvr.interfaces.I_Visitor_Processor;
import kewlstuff.pojopersist.interfaces.I_FlexiBean;
import kewlstuff.harbor.client.Vessel; //Added
//import harbor.dbDemo.app.srvr.Visitor_Processor; //Removed

/**
 *
 * @author JK
 * As you know this is the CD Unit (SHIP) for your code in the harbor.
 * It is the only module that is specific to the Harbor Application server
 *  and it allows the application dropped into Harbor to run remotely.
 *
 * Just for the purposes of demonstration and to make one see just how much
 *  work harbor actually is... we have taken the original code from the application
 *  and we comment out what is not used and where Harbor specific API have been used
 * ie its a direct comparison to the standalone CD unit.
 * Approx 12 lines of code, to put a business application with its UI and dB pool online.
 * The application took 3 hours to make... its easy because debugging and testing
 * is easy in POJO. Its a stand-alone application so one cant even count that as Harbor
 * specific, but ok lets say we thought about the MVC (Coherent Disintegration) in
 * the app for an hour, also hard to count as Harbor specific because all it really does
 * is force good coding practice on the developer.
 * This took 5 mins.
 * Once you have done one, its fast, very fast, and the code is reusable anywhere.
 */
public class CD_Unit implements I_CD_Unit{
    private I_UI i_UIControl = null;
    private I_Visitor_Processor i_Visitor_Processor = null; 
    private Vessel vessel = null; //Added
    
    /** Creates a new instance of CDTestUnit */
    public CD_Unit() {
        //Tell this code where our application server is
        String harborUrl = "http://localhost:8080/harbor/service"; 
        
        //Allows one to change the url without re-compiling this example
        //Nice for testing on other machines... the getenv Java function has issues
        Vessel.loadUserConfig(); //loads <user location>/harbor/user/config.txt
        String harborTestUrl = Vessel.getUserProperty("HARBOR_SITE_URL");
        if(harborTestUrl != null){
                Vessel.setLocalAdminLogMessage("URL environment variable set to " + harborTestUrl);
                harborUrl = harborTestUrl;
                javax.swing.JOptionPane.showMessageDialog(null,harborUrl, "URL Setting in config file", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }       
        vessel = new Vessel(harborUrl); //get a vessel from the harbor 
        //vessel.verboseVessel(true); // for debugging
        vessel.enableProgressDisplay(true); //for internet apps
    }
    
    public void startViewForm(){
        //I_UI i_UI = new View_Form(); //Removed
        Class uiApp = vessel.getRemoteClass("harbor.dbDemo.app.ui.View_Form");//Added
        if(uiApp != null){
            I_UI i_UI = (I_UI)vessel.newInst(uiApp);//Added
            i_UI.start(this);
        }        
    }     
    
    public void startEntryForm(){
        //I_UI i_UI = new Entry_Form();//Removed
        Class uiApp = vessel.getRemoteClass("harbor.dbDemo.app.ui.Entry_Form");//Added
        if(uiApp != null){
            I_UI i_UI = (I_UI)vessel.newInst(uiApp);//Added
            i_UI.start(this);
        }        
    }
   
    
    public void startControlForm(){
        if(i_UIControl == null){
            //i_UIControl = new Control_Panel();//Removed
            Class uiApp = vessel.getRemoteClass("harbor.dbDemo.app.ui.Control_Panel");//Added
            if(uiApp != null) i_UIControl = (I_UI)vessel.newInst(uiApp);//Added
        }
        i_UIControl.start(this); //pass in callback so UI program can use CD_Unit
    }  
    
    public boolean addVisitorMessageToDb(String sName, String sMessage){
        if(i_Visitor_Processor == null) return false;
        return i_Visitor_Processor.addVisitorMessageToDb(sName,sMessage);
    }
    
    public I_FlexiBean[] getVisitorFromDb(long startPosition, int limitRecords, boolean forwardDirection){
        if(i_Visitor_Processor == null) return null;
        return i_Visitor_Processor.getVisitorFromDb(startPosition, limitRecords, forwardDirection);
    }   
    
    public I_FlexiBean[] getMessagesFromDb(long visitorID){
        if(i_Visitor_Processor == null) return null;        
        return i_Visitor_Processor.getMessagesFromDb(visitorID);
    }    
    
    public void start(){
         //i_Visitor_Processor = new Visitor_Processor();//Removed
         i_Visitor_Processor = (I_Visitor_Processor)vessel.loadRemoteSingleton(I_Visitor_Processor.class,"harbor.dbDemo.app.srvr.Visitor_Processor","VISITOR_DB_DEMO");//Added
         if(i_Visitor_Processor == null){ System.out.print("Class does not exist or connection problems\n"); return;}//Added         
         startControlForm();
    }    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new CD_Unit().start();
    }
    
}