package COM.odi.demo.newpeople2;

// Import the COM.odi package, which contains the API:
import COM.odi.*;

public
class PersonManager {

  // Main:
  public static void main(String argv[]) {
    try {
      String dbName = argv[0];

      // The following line starts a nonglobal session and joins
      // this thread to the new session.  This allows the thread
      // to use PSE Pro.
      Session.create(null, null).join();
      Database db = createDatabase(dbName);
      readDatabase(db);
      db.close(); 
    }
    finally {
        Session.getCurrent().terminate();
    }
  }
    
  static Database createDatabase(String dbName) {

    // Attempt to open and destroy the database specified on the
    // command line.  This ensures that that program creates a
    // new database each time the application is called.
    try {
      Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
    } catch (DatabaseNotFoundException e) {
    }

    // Create a new database.
    Database db = Database.create(dbName,
			 ObjectStore.ALL_READ | ObjectStore.ALL_WRITE);

    // Start an update transaction.
    Transaction tr = Transaction.begin(ObjectStore.UPDATE);

    // Create instances of Person2.
    Person2 sophie = new Person2("Sophie", 5, null);
    Person2 joseph = new Person2("Joseph", 1, null);
    Person2 children[] = { sophie, joseph };
    Person2 sally = new Person2("Sally", 39, children);

    // Create a database root and associate it with 
    // sally, which is a persistent-capable object.
    // ObjectStore Java uses a database root as an entry
    // point into a database. 
    db.createRoot("Sally", sally);

    // End the transaction. 
    // This stores the three Person2 objects, along with
    // the String objects representing their names, and the array of 
    // children, into the database.   
    tr.commit();

    return db;
  }

  static void readDatabase(Database db) {

    // Start a read-only transaction
    Transaction tr = Transaction.begin(ObjectStore.READONLY);

    // Use the "Sally" database root to access objects in the database.
    // Because sally references sophie and joseph, obtaining the "Sally"
    // database root allows the program to also reach sophie and joseph.
    // In each transaction, an application must obtain a database root 
    // and use it to navigate to persistent objects. Be sure to write
    // your application so that it does not hold on to references
    // to persistent objects between transactions.
    Person2 sally = (Person2)db.getRoot("Sally");
    Person2 children[] = sally.getChildren();
    System.out.print("Sally is " + sally.getAge() + " and has " +
      children.length + " children: ");
    for(int i=0; i<children.length; i++) {
      System.out.print(children[i].getName() + " ");
    }

    System.out.println(" ");

    // End the transaction. This ends the accessibility of the 
    // persistent objects and abandons the transient objects.
    tr.commit();
  }
}
