final SessionInfoImpl sInfo = getSessionInfoImpl(sessionInfo);
executeWithLocalEvents(new Callable() {
public Object run() throws RepositoryException {
Version v = (Version) getNode(versionId, sInfo);
if (hasNode(sessionInfo, nodeId)) {
Node n = getNode(nodeId, sInfo);
n.restore(v, removeExisting);
} else {
// restore with rel-Path part
Node n = null;
Path relPath = null;
Path path = nodeId.getPath();
if (nodeId.getUniqueID() != null) {
n = getNode(idFactory.createNodeId(nodeId.getUniqueID()), sInfo);
relPath = (path.isAbsolute()) ? getPathFactory().getRootPath().computeRelativePath(nodeId.getPath()) : path;
} else {
int degree = 0;
while (degree < path.getLength()) {
Path ancestorPath = path.getAncestor(degree);
NodeId parentId = idFactory.createNodeId(nodeId.getUniqueID(), ancestorPath);
if (hasNode(sessionInfo, parentId)) {
n = getNode(parentId, sInfo);
relPath = ancestorPath.computeRelativePath(path);
}
degree++;
}
}
if (n == null) {
throw new PathNotFoundException("Path not found " + nodeId);
} else {
n.restore(v, sInfo.getNamePathResolver().getJCRPath(relPath), removeExisting);
}
}
return null;
}
}, sInfo);