// for restore operation
List<String> existedIdentifiers = new ArrayList<String>(); // InWorkspace
List<VersionImpl> notExistedVersions = new ArrayList<VersionImpl>();
TransactionableDataManager dataManager = session.getTransientNodesManager().getTransactManager();
SessionChangesLog changesLog = new SessionChangesLog(session);
for (Version v : versions)
{
String versionableIdentifier = v.getContainingHistory().getVersionableUUID();
NodeData node = (NodeData)dataManager.getItemData(versionableIdentifier);
if (node != null)
{
existedIdentifiers.add(versionableIdentifier);
// restore version at existed parent
try
{
NodeData destParent = (NodeData)dataManager.getItemData(node.getParentIdentifier());
NodeData vh = (NodeData)dataManager.getItemData(((VersionImpl)v).getParentIdentifier()); // version parent
// it's a VH
VersionHistoryDataHelper historyHelper = new VersionHistoryDataHelper(vh, dataManager, nodeTypeManager);
changesLog.addAll(((VersionImpl)v).restoreLog(destParent, node.getQPath().getName(), historyHelper,
session, removeExisting, changesLog).getAllStates());
}
catch (ItemExistsException e)
{
throw new ItemExistsException("Workspace restore. Can't restore a node. "
+ v.getContainingHistory().getVersionableUUID() + ". " + e.getMessage(), e);
}
catch (RepositoryException e)
{
throw new RepositoryException("Workspace restore. Can't restore a node. "
+ v.getContainingHistory().getVersionableUUID() + ". Repository error: " + e.getMessage(), e);
}
}
else
{
// not found, looking for parent
// SPEC: For every version V in S that corresponds to a missing node in
// the workspace, there must also be a parent of V in S
// =========================================
// Trying search for corresponding node,
// If we have a corr node and her parent in the existed list - all ok,
// otherwise exception will be thrown.
NodeData corrNode = null;
String versionableParentIdentifier = null;
if (!v.getSession().getWorkspace().getName().equals(session.getWorkspace().getName()))
{
TransactionableDataManager vDataManager =
((SessionImpl)v.getSession()).getTransientNodesManager().getTransactManager();
corrNode = (NodeData)vDataManager.getItemData(versionableIdentifier);
if (corrNode != null)
{
versionableParentIdentifier = corrNode.getParentIdentifier();
}
else
{
log.warn("Workspace.restore(). Correspondent node is not found " + versionableIdentifier);
}
}
if (versionableParentIdentifier != null && existedIdentifiers.contains(versionableParentIdentifier))
{
notExistedVersions.add((VersionImpl)v);
continue;
}
if (versionableParentIdentifier == null)
{
//check in changes log
ItemState itemState = changesLog.getItemState(versionableIdentifier);
if (itemState != null && !itemState.isDeleted())
{
notExistedVersions.add((VersionImpl)v);
continue;
}
}
throw new VersionException(
"No such node (for version, from the array of versions, "
+ "that corresponds to a missing node in the workspace, there must also be a parent in the array). UUID: "
+ versionableIdentifier);
}
}
for (VersionImpl v : notExistedVersions)
{
String versionableIdentifier = v.getContainingHistory().getVersionableUUID();
try
{
NodeData node = null;
for (ItemState change : changesLog.getAllStates())
{
if (change.isNode() && change.isAdded()
&& ((NodeData)change.getData()).getIdentifier().equals(versionableIdentifier))
{
node = (NodeData)change.getData();
break;
}
}
if (node != null)
{
NodeData destParent = (NodeData)dataManager.getItemData(node.getParentIdentifier());
// version parent it's a VH
NodeData vh = (NodeData)dataManager.getItemData(v.getParentIdentifier());
VersionHistoryDataHelper historyHelper = new VersionHistoryDataHelper(vh, dataManager, nodeTypeManager);
changesLog.addAll(v.restoreLog(destParent, node.getQPath().getName(), historyHelper, session,
removeExisting, changesLog).getAllStates());
}
else
{
throw new VersionException("No such node restored before (for version, from the array of versions, "