public RevTree build(ObjectDatabase origin, ObjectDatabase target) {
final ObjectId nodeId = node.getObjectId();
final RevTree tree = origin.getTree(nodeId);
RevTreeBuilder builder = tree.builder(target).clearSubtrees();
for (MutableTree childTree : this.childTrees.values()) {
String name;
ObjectId newObjectId;
ObjectId metadataId;
Envelope bounds;
{
RevTree newChild = childTree.build(origin, target);
target.put(newChild);
Node oldNode = childTree.getNode();
name = oldNode.getName();
newObjectId = newChild.getId();
metadataId = oldNode.getMetadataId().or(ObjectId.NULL);
bounds = SpatialOps.boundsOf(newChild);
}
Node newNode = Node.create(name, newObjectId, metadataId, TYPE.TREE, bounds);
builder.put(newNode);
}
RevTree newTree = builder.build();
if (!this.node.getObjectId().equals(newTree.getId())) {
target.put(newTree);
Envelope bounds = SpatialOps.boundsOf(newTree);
this.node = Node.create(node.getName(), newTree.getId(),
node.getMetadataId().or(ObjectId.NULL), TYPE.TREE, bounds);