package DBC;

import java.io.*;
import java.awt.*;
import java.rmi.*;
import java.rmi.server.*;

public class
DBCImpl extends UnicastRemoteObject
        implements DBC {

    private myDatabase database;
    private String objName;
    private String name;
    private String age;
    private String occ;

    public DBCImpl(String s) throws java.rmi.RemoteException {
        super();
        objName = s;
        database = new myDatabase();
    }

    public void search(String a) throws java.rmi.RemoteException {

        StringBuffer asb = new StringBuffer();
        StringBuffer bsb = new StringBuffer();
        StringBuffer csb = new StringBuffer();

        database.find(a, asb, bsb, csb);
        name = asb.toString();
        age = bsb.toString();
        occ = csb.toString();
        System.out.println("got back from db: ");
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Occupation: " + occ);
    }

    public String getName() throws java.rmi.RemoteException {
        return name;
    }

    public String getAge() throws java.rmi.RemoteException {
        return age;
    }

    public String getOcc() throws java.rmi.RemoteException {
       return occ;
    }

    public void open() throws java.rmi.RemoteException {
        database.open();
        System.out.println("OPEN DATABASE"); 
    }

    public void close() throws java.rmi.RemoteException {
        database.close();
        System.out.println("CLOSE DATABASE");
    }

  public static void main(String[] args) {

    // Create and install the security manager
    System.setSecurityManager(new RMISecurityManager());

    try {
            DBCImpl obj = new DBCImpl("DBCServer");
            Naming.rebind("//osprey7.npac.syr.edu:4527/DBCServer", obj);
            System.out.println("DBCImpl created and bound in the registry to the name DBCServer");
    }
    catch (Exception ex) {
      System.err.println("DB Server: Exception-> " + ex);
      ex.printStackTrace();
    }
  }
}