package COM.odi.demo.newpeople;

// Import the COM.odi package, which contains the API:
import COM.odi.*;

public
class Person {

  // Fields in the Person class:
  String name;
  int age;
  Person children[];

  // 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 Person.
    Person sophie = new Person("Sophie", 5, null);
    Person joseph = new Person("Joseph", 1, null);
    Person children[] = { sophie, joseph };
    Person sally = new Person("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 person objects, 
    // along with the String objects representing their names, and 
    // the array of children, into the database.  After this method 
    // call all Persistent object references are considered STALE.
    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.
    Person sally = (Person)db.getRoot("Sally");
    Person 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 read-only transaction. This ends the
    // accessibility of the persistent objects since the commit()
    // is of a RETAIN_STALE type.
    tr.commit();
  }

  // Constructor
  public Person(String nameValue, int ageValue, 
    Person theChildren[]) {
    name = nameValue; 
    age = ageValue; 
    children = theChildren;
  }

  public String getName() { 
    return name; 
  }

  public void setName(String nameValue) { 
    name = nameValue; 
  }

  public int getAge() { 
    return age; 
  }

  public void setAge(int ageValue) { 
    age = ageValue; 
  }

  public Person[] getChildren() { 
    return children; 
  }

  public void setChildren(Person theChildren[]) {
    children = theChildren;
  }
   
  // This class is never used as a persistent hash key.
  // So using the Object's hashCode is ok.
  public int hashCode() {
    return super.hashCode();
  }
}

