boolean debug = log.isDebugEnabled();
GlobalTransaction gtx = getGlobalTransaction(ctx);
TransactionWorkspace workspace = getTransactionWorkspace(ctx);
WorkspaceNode workspaceNode;
List<Fqn> nodesCreated = new ArrayList<Fqn>();
DataVersion version = null;
if (ctx.getOptionOverrides() != null && ctx.getOptionOverrides().getDataVersion() != null)
{
version = ctx.getOptionOverrides().getDataVersion();
workspace.setVersioningImplicit(false);
}
// start with the ROOT node and then work our way down to the node necessary, creating nodes along the way.
workspaceNode = workspace.getNode(Fqn.ROOT);
if (debug) log.debug("GlobalTransaction: " + gtx + "; Root: " + workspaceNode);
// we do not have the root in the workspace! Put it into thr workspace now.
if (workspaceNode == null)
{
NodeSPI node = dataContainer.getRoot();
workspaceNode = lockAndCreateWorkspaceNode(nodeFactory, node, workspace, gtx, lockAcquisitionTimeout);
workspace.addNode(workspaceNode);
log.debug("Created root node in workspace.");
}
else
{
log.debug("Found root node in workspace.");
}
// iterate through the target Fqn's elements.
int targetFqnSize = targetFqn.size(), currentDepth = 1;
for (Object childName : targetFqn.peekElements())
{
boolean isTargetFqn = (currentDepth == targetFqnSize);
currentDepth++;
// current workspace node canot be null.
// try and get the child of current node
if (debug) log.debug("Attempting to get child " + childName);
NodeSPI currentNode = workspaceNode.getNode().getChildDirect(childName);
if (currentNode == null)
{
// first test that it exists in the workspace and has been created in thix tx!
WorkspaceNode peekInWorkspace = workspace.getNode(Fqn.fromRelativeElements(workspaceNode.getFqn(), childName));
if (peekInWorkspace != null && peekInWorkspace.isCreated())
{
// exists in workspace and has just been created.
currentNode = peekInWorkspace.getNode();
if (peekInWorkspace.isDeleted())
{
peekInWorkspace.markAsDeleted(false);
// add in parent again
workspaceNode.addChild(peekInWorkspace);
}
}
}