// check constraints
// get applicable definition of target node at new location
NodeTypeImpl nt = (NodeTypeImpl) targetNode.getPrimaryNodeType();
NodeDefinitionImpl newTargetDef;
try {
newTargetDef = destParentNode.getApplicableChildNodeDefinition(destName.getName(), nt.getQName());
} catch (RepositoryException re) {
String msg = destAbsPath + ": no definition found in parent node's node type for new node";
log.debug(msg);
throw new ConstraintViolationException(msg, re);
}
// if there's already a node with that name also check same-name sibling
// setting of new node; just checking same-name sibling setting on
// existing node is not sufficient since same-name sibling nodes don't
// necessarily have identical definitions
if (existing != null && !newTargetDef.allowsSameNameSiblings()) {
throw new ItemExistsException(existing.safeGetJCRPath());
}
// check protected flag of old & new parent
if (destParentNode.getDefinition().isProtected()) {
String msg = destAbsPath + ": cannot add a child node to a protected node";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
if (srcParentNode.getDefinition().isProtected()) {
String msg = srcAbsPath + ": cannot remove a child node from a protected node";
log.debug(msg);
throw new ConstraintViolationException(msg);
}
// check lock status
srcParentNode.checkLock();
destParentNode.checkLock();
NodeId targetId = targetNode.getNodeId();
int index = srcName.getIndex();
if (index == 0) {
index = 1;
}
if (srcParentNode.isSame(destParentNode)) {
// do rename
destParentNode.renameChildNode(srcName.getName(), index, targetId, destName.getName());
} else {
// do move:
// 1. remove child node entry from old parent
NodeState srcParentState =
(NodeState) srcParentNode.getOrCreateTransientItemState();
srcParentState.removeChildNodeEntry(srcName.getName(), index);
// 2. re-parent target node
NodeState targetState =
(NodeState) targetNode.getOrCreateTransientItemState();
targetState.setParentId(destParentNode.getNodeId());
// 3. add child node entry to new parent
NodeState destParentState =
(NodeState) destParentNode.getOrCreateTransientItemState();
destParentState.addChildNodeEntry(destName.getName(), targetId);
}
// change definition of target
targetNode.onRedefine(newTargetDef.unwrap().getId());
}