}
// restore the frozen nodes
InternalFreeze[] frozenNodes = freeze.getFrozenChildNodes();
for (int i = 0; i < frozenNodes.length; i++) {
InternalFreeze child = frozenNodes[i];
NodeImpl restoredChild = null;
if (child instanceof InternalFrozenNode) {
InternalFrozenNode f = (InternalFrozenNode) child;
// check for existing
if (f.getFrozenUUID() != null) {
try {
NodeImpl existing = (NodeImpl) session.getNodeByUUID(f.getFrozenUUID());
// check if one of this restoretrees node
if (removeExisting) {
existing.remove();
} else if (existing.isShareable()) {
// if existing node is shareable, then clone it
restoredChild = clone(existing, f.getName());
} else {
// since we delete the OPV=Copy children beforehand, all
// found nodes must be outside of this tree
throw new ItemExistsException(
"Unable to restore node, item already"
+ " exists outside of restored tree: "
+ existing);
}
} catch (ItemNotFoundException e) {
// ignore, item with uuid does not exist
}
}
if (restoredChild == null) {
restoredChild = addNode(f.getName(), f);
restoredChild.restoreFrozenState(f, vsel, restored, removeExisting);
}
} else if (child instanceof InternalFrozenVersionHistory) {
InternalFrozenVersionHistory f = (InternalFrozenVersionHistory) child;
VersionHistory history = (VersionHistory) session.getNodeById(f.getVersionHistoryId());
NodeId nodeId = NodeId.valueOf(history.getVersionableUUID());
String oldVersion = "jcr:dummy";
// check if representing versionable already exists somewhere
if (itemMgr.itemExists(nodeId)) {
NodeImpl n = session.getNodeById(nodeId);
if (removeExisting) {
String dstPath = getPath() + "/" + n.getName();
if (!n.getPath().equals(dstPath)) {
session.move(n.getPath(), dstPath);
}
oldVersion = n.getBaseVersion().getName();
} else if (n.getParent().isSame(this)) {
n.internalRemove(true);
} else {
// since we delete the OPV=Copy children beforehand, all
// found nodes must be outside of this tree
throw new ItemExistsException(
"Unable to restore node, item already exists"
+ " outside of restored tree: " + n);
}
}
// get desired version from version selector
VersionImpl v = (VersionImpl) vsel.select(history);
// check existing version of item exists
if (!itemMgr.itemExists(nodeId)) {
if (v == null) {
// if version selector was unable to select version,
// choose the initial one
Version[] vs = history.getRootVersion().getSuccessors();
if (vs.length == 0) {
String msg = "Unable to select appropariate version for "
+ child.getName() + " using " + vsel;
log.error(msg);
throw new VersionException(msg);
}
v = (VersionImpl) vs[0];
}
restoredChild = addNode(child.getName(), v.getInternalFrozenNode());
} else {
restoredChild = session.getNodeById(nodeId);
if (v == null || oldVersion == null || v.getName().equals(oldVersion)) {
v = null;
}