/**
   * This method takes all the children of the passed-in category (both {@link
   * arlut.csd.ganymede.server.DBObjectBase DBObjectBase} objects and contained {@link
   * arlut.csd.ganymede.server.DBBaseCategory DBBaseCategory} objects) and makes copies under this.
   */
  private void recurseDown(DBBaseCategory category, Hashtable baseHash, DBSchemaEdit editor)
      throws RemoteException {
    Vector<CategoryNode> children = category.getNodes();
    DBObjectBase oldBase, newBase;
    DBBaseCategory oldCategory, newCategory;

    /* -- */

    if (debug) {
      Ganymede.debug("** recurseDown");

      if (editor == null) {
        Ganymede.debug("**#?!?!!! DBBaseCategory.recurseDown(): editor == null!!!");
      }
    }

    for (CategoryNode node : children) {
      if (node instanceof DBObjectBase) {
        oldBase = (DBObjectBase) node;

        // a new copy, with the same objects under it

        newBase = new DBObjectBase(oldBase, editor);
        baseHash.put(newBase.getKey(), newBase);

        if (false) {
          Ganymede.debug(
              "Created newBase " + newBase.getName() + " in recursive category tree duplication");
        }

        // we want this base to be added to the current end of this category

        addNodeAfter(newBase, null);

        if (false) {
          Ganymede.debug("Added " + newBase.getName() + " to new category tree");
        }
      } else if (node instanceof DBBaseCategory) {
        oldCategory = (DBBaseCategory) node;
        newCategory = (DBBaseCategory) newSubCategory(oldCategory.getName());
        newCategory.editor = editor;

        if (false) {
          Ganymede.debug(
              "Created newCategory "
                  + newCategory.getName()
                  + " in recursive category tree duplication");
        }

        newCategory.recurseDown(oldCategory, baseHash, editor);
      }
    }
  }
  /** This DBObjectDeltaRec constructor is used to load a delta record from a Journal stream. */
  public DBObjectDeltaRec(DataInput in) throws IOException {
    short fieldcode;
    short typecode;
    boolean scalar;
    DBObjectBase baseDef;
    DBObjectBaseField fieldDef;
    String fieldName = null;
    String status = null;
    DBObject obj = null;
    /* -- */

    status = "Reading invid";

    boolean debug = true;

    try {
      invid = Invid.createInvid(in);
      baseDef = Ganymede.db.getObjectBase(invid.getType());
      obj = Ganymede.db.viewDBObject(invid);

      if (debug) {
        System.err.println(
            "\n>*> Reading delta rec for " + baseDef.getName() + " <" + invid.getNum() + ">");
      }

      status = "Reading field count";
      int fieldcount = in.readInt();

      if (debug) {
        System.err.println(
            ">> DBObjectDeltaRec(): " + fieldcount + " fields in on-disk delta rec.");
      }

      for (int i = 0; i < fieldcount; i++) {
        status = "\nReading field code for field " + i;
        if (debug) {
          System.err.println(status);
        }
        fieldcode = in.readShort();
        fieldDef = (DBObjectBaseField) baseDef.getField(fieldcode);
        typecode = fieldDef.getType();
        fieldName = fieldDef.getName();
        status = "Reading deletion boolean for field " + i;
        if (in.readBoolean()) {
          // we're deleting this field
          if (debug) {
            System.err.println(
                "Reading field deletion record field ("
                    + fieldName
                    + ":"
                    + fieldcode
                    + ") for field "
                    + i);
          }

          fieldRecs.addElement(new fieldDeltaRec(fieldcode, null));
          continue;
        }

        // okay, we've got a field redefinition.. check the type
        // code to make sure we don't have an incompatible journal
        // entry
        status = "Reading type code for field " + i;
        if (in.readShort() != typecode) {
          throw new RuntimeException("Error, field type mismatch in journal file");
        }

        // ok.. now, is it a total redefinition, or a vector delta record?
        status = "Reading scalar/vector delta boolean for field " + i;
        scalar = in.readBoolean();

        if (scalar) {
          fieldDeltaRec f_r = null;
          status = "Reading field (" + fieldName + ":" + fieldcode + ") for field " + i;
          if (debug) {
            System.err.println(status);
          }

          f_r = new fieldDeltaRec(fieldcode, DBField.readField(obj, in, fieldDef));
          fieldRecs.addElement(f_r);
          if (debug) {
            System.err.println("Value: " + f_r.toString());
          }
        } else {
          // ok, we have a vector delta chunk
          fieldDeltaRec fieldRec = new fieldDeltaRec(fieldcode);
          Object value = null;

          // read in the additions
          status = "Reading vector addition count for field " + fieldName + "(" + i + ")";
          if (debug) {
            System.err.println(status);
          }

          int size = in.readInt();

          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " additions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types
            switch (typecode) {
              case STRING:
                status = "Reading string addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading ip addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }

            fieldRec.addValue(value);
          }

          // read in the deletions
          status = "Reading vector deletion count for field " + i;
          if (debug) {
            System.err.println(status);
          }

          size = in.readInt();
          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " deletions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types

            switch (typecode) {
              case STRING:
                status = "Reading string deletion " + j + " for field " + i + ":" + fieldName;
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid deletion " + j + " for field " + i + ":" + fieldName;
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading IP deletion " + j + " for field " + i + ":" + fieldName;
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }
            fieldRec.delValue(value);
          }
          // and save this field
          fieldRecs.addElement(fieldRec);
        }
      }
    } catch (IOException ex) {
      System.err.println("DBObjectDeltaRec constructor: IOException in state " + status);
      Ganymede.logError(ex);
      throw ex;
    }
  }
  /**
   * This method is used to remove a Category Node from under us.
   *
   * @see arlut.csd.ganymede.rmi.Category
   */
  public synchronized void removeNode(CategoryNode node) throws RemoteException {
    int i, index = -1;

    /* -- */

    if (node == null) {
      throw new IllegalArgumentException("Can't remove a null node");
    }

    // find our deletion point

    if (debug) {
      try {
        Ganymede.debug("DBBaseCategory (" + getName() + ").removeNode(" + node.getPath() + ")");
      } catch (RemoteException ex) {
        Ganymede.logError(ex);
        throw new RuntimeException("rmi local failure?" + ex.getMessage());
      }
    }

    for (i = 0; i < contents.size(); i++) {
      if (debug) {
        try {
          Ganymede.debug(" examining: " + ((CategoryNode) contents.elementAt(i)).getPath());
        } catch (RemoteException ex) {
          Ganymede.logError(ex);
          throw new RuntimeException("rmi local failure?" + ex.getMessage());
        }
      }

      if (contents.elementAt(i).equals(node)) {
        index = i;
      }
    }

    if (index == -1) {
      throw new IllegalArgumentException("can't delete a node that's not in the category");
    }

    // remove our node from our content list

    contents.removeElementAt(index);

    if (false) {
      if (node instanceof DBObjectBase) {
        DBObjectBase base = (DBObjectBase) node;

        if (!base.isEditing()) {
          System.err.println(
              "DBBaseCategory.removeNode(): " + base.getName() + " has a null editor!");
        } else {
          System.err.println(
              "DBBaseCategory.removeNode(): " + base.getName() + " has a non-null editor!");
        }
      }
    }

    // Sorry, kid, yer on your own now!

    node.setCategory(null);
  }
  /**
   * This method is used to remove a Category Node from under us.
   *
   * @see arlut.csd.ganymede.rmi.Category
   */
  public synchronized void removeNode(String name) throws RemoteException {
    int i, index = -1;

    CategoryNode node = null;

    /* -- */

    if (name == null) {
      throw new IllegalArgumentException("Can't remove a null name");
    }

    // find our deletion point

    if (debug) {
      Ganymede.debug("DBBaseCategory (" + getName() + ").removeNode(" + name + ")");
    }

    for (i = 0; i < contents.size() && (index == -1); i++) {
      if (debug) {
        Ganymede.debug(" examining: " + contents.elementAt(i));
      }

      node = (CategoryNode) contents.elementAt(i);

      try {
        if (node.getName().equals(name)) {
          index = i;
        }
      } catch (RemoteException ex) {
        throw new RuntimeException("caught remote: " + ex);
      }
    }

    if (index == -1) {
      throw new IllegalArgumentException("can't delete a name that's not in the category");
    } else if (debug) {
      System.err.println("DBBaseCategory.removeNode(): found node " + node);

      if (node instanceof DBObjectBase) {
        System.err.println("DBBaseCategory.removeNode(): node is DBObjectBase");
      } else if (node instanceof Base) {
        System.err.println("DBBaseCategory.removeNode(): node is Base");
      } else if (node instanceof DBBaseCategory) {
        System.err.println("DBBaseCategory.removeNode(): node is DBBaseCategory");
      } else if (node instanceof Category) {
        System.err.println("DBBaseCategory.removeNode(): node is Category");
      } else {
        System.err.println("DBBaseCategory.removeNode(): node is <unrecognized>");
      }
    }

    // remove our node from our content list

    contents.removeElementAt(index);

    if (debug) {
      if (node instanceof DBObjectBase) {
        DBObjectBase base = (DBObjectBase) node;

        if (!base.isEditing()) {
          System.err.println(
              "DBBaseCategory.removeNode(2): " + base.getName() + " has a null editor!");
        } else {
          System.err.println(
              "DBBaseCategory.removeNode(2): " + base.getName() + " has a non-null editor!");
        }
      }
    }

    // Sorry, kid, yer on your own now!

    node.setCategory(null);
  }