allow IDs to be gotten from transient nodes. If getID is called on

a transient node, an ID is generated from the node manager. if
the node is later persisted, the same id will be used.
This commit is contained in:
hns 2001-08-25 17:46:00 +00:00
parent 43012cfe1d
commit fac98332c9
2 changed files with 18 additions and 13 deletions

View file

@ -199,8 +199,9 @@ public final class Node implements INode, Serializable {
this.nmgr = nmgr; this.nmgr = nmgr;
this.prototype = prototype; this.prototype = prototype;
dbmap = nmgr.getDbMapping (prototype); dbmap = nmgr.getDbMapping (prototype);
// the id is only generated when the node is actually checked into db. // the id is only generated when the node is actually checked into db,
// id = nmgr.generateID (dbmap); // or when it's explicitly requested.
id = null;
this.name = n == null ? "" : n; this.name = n == null ? "" : n;
created = lastmodified = System.currentTimeMillis (); created = lastmodified = System.currentTimeMillis ();
adoptName = true; adoptName = true;
@ -401,22 +402,24 @@ public final class Node implements INode, Serializable {
*/ */
public String getID () { public String getID () {
if (state == TRANSIENT) { // if we are transient, we generate an id on demand. It's possible that we'll never need
Thread.dumpStack (); // it, but if we do it's important to keep the one we have.
throw new RuntimeException ("getID called on transient Node: "+this); if (state == TRANSIENT && id == null)
} id = nmgr.generateID (dbmap);
return id; return id;
} }
protected void setID (String id) { /**
this.id = id; * Returns true if this node is accessed by id from its aprent, false if it
((Transactor) Thread.currentThread()).visitCleanNode (this); * is accessed by name
} */
public boolean isAnonymous () { public boolean isAnonymous () {
return anonymous; return anonymous;
} }
/**
* Return this node' name, which may or may not have some meaning
*/
public String getName () { public String getName () {
return name; return name;
} }
@ -1660,7 +1663,9 @@ public final class Node implements INode, Serializable {
protected void makePersistentCapable () { protected void makePersistentCapable () {
if (state == TRANSIENT) { if (state == TRANSIENT) {
state = NEW; state = NEW;
id = nmgr.generateID (dbmap); // generate a real, persistent ID for this object, if it doesn't have one already.
if (id == null)
id = nmgr.generateID (dbmap);
getHandle ().becomePersistent (); getHandle ().becomePersistent ();
Transactor current = (Transactor) Thread.currentThread (); Transactor current = (Transactor) Thread.currentThread ();
current.visitNode (this); current.visitNode (this);

View file

@ -77,7 +77,7 @@ public final class NodeHandle implements INodeState, Serializable {
*/ */
public String getID () { public String getID () {
if (key == null) if (key == null)
throw new RuntimeException ("getID called on transient Node"); return node.getID ();
return key.getID (); return key.getID ();
} }