{previous = getLastNode(previous);}
final NodePath oldPath = oldNode.getPath();
StreamListener listener = null;
//May help getReindexRoot() to make some useful things
Node newNode = null;
DBBroker broker = null;
try {
broker = ownerDocument.getBrokerPool().get(null);
broker.getIndexController().setDocument(ownerDocument);
final StoredNode reindexRoot = broker.getIndexController().getReindexRoot(oldNode, oldPath, false);
broker.getIndexController().setMode(StreamListener.REMOVE_SOME_NODES);
if (reindexRoot == null) {
listener = broker.getIndexController().getStreamListener();
} else {
broker.getIndexController().reindex(transaction, reindexRoot,
StreamListener.REMOVE_SOME_NODES);
}
broker.removeAllNodes(transaction, oldNode, oldPath, listener);
broker.endRemove(transaction);
broker.flush();
broker.getIndexController().setMode(StreamListener.STORE);
listener = broker.getIndexController().getStreamListener();
newNode = appendChild(transaction, oldNode.nodeId, new NodeImplRef(previous),
getPath(), newChild, listener);
//Reindex if required
final DocumentImpl owner = (DocumentImpl)getOwnerDocument();
broker.storeXMLResource(transaction, owner);
broker.updateNode(transaction, this, false);
broker.getIndexController().reindex(transaction, reindexRoot, StreamListener.STORE);
broker.flush();
} catch (final EXistException e) {
LOG.warn("Exception while inserting node: " + e.getMessage(), e);
} finally {
if (broker != null)
broker.release();
}
//return oldChild; // method is spec'd to return the old child, even though that's probably useless in this case
return newNode; //returning the newNode is more sensible than returning the oldNode
}