// check precondition
checkInEditMode();
// 1. check paths & retrieve state
NodeState srcState = getNodeState(srcStateMgr, srcHierMgr, srcPath);
Path.Element destName = destPath.getNameElement();
Path destParentPath = destPath.getAncestor(1);
NodeState destParentState = getNodeState(destParentPath);
int ind = destName.getIndex();
if (ind > 0) {
// subscript in name element
String msg =
"invalid copy destination path: " + safeGetJCRPath(destPath)
+ " (subscript in name element is not allowed)";
log.debug(msg);
throw new RepositoryException(msg);
}
// 2. check access rights, lock status, node type constraints, etc.
// JCR-2269: store target node state in changelog early as a
// precautionary measure in order to isolate it from concurrent
// underlying changes while checking preconditions
stateMgr.store(destParentState);
checkAddNode(destParentState, destName.getName(),
srcState.getNodeTypeName(), CHECK_ACCESS | CHECK_LOCK
| CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
// check read access right on source node using source access manager
try {
if (!srcAccessMgr.isGranted(srcPath, Permission.READ)) {
throw new PathNotFoundException(safeGetJCRPath(srcPath));
}
} catch (ItemNotFoundException infe) {
String msg =
"internal error: failed to check access rights for "
+ safeGetJCRPath(srcPath);
log.debug(msg);
throw new RepositoryException(msg, infe);
}
// 3. do copy operation (modify and store affected states)
ReferenceChangeTracker refTracker = new ReferenceChangeTracker();
// create deep copy of source node state
NodeState newState = copyNodeState(srcState, srcPath, srcStateMgr, srcAccessMgr,
destParentState.getNodeId(), flag, refTracker);
// add to new parent
destParentState.addChildNodeEntry(destName.getName(), newState.getNodeId());
// adjust references that refer to uuid's which have been mapped to
// newly generated uuid's on copy/clone
Iterator<Object> iter = refTracker.getProcessedReferences();
while (iter.hasNext()) {
PropertyState prop = (PropertyState) iter.next();
// being paranoid...
if (prop.getType() != PropertyType.REFERENCE
&& prop.getType() != PropertyType.WEAKREFERENCE) {
continue;
}
boolean modified = false;
InternalValue[] values = prop.getValues();
InternalValue[] newVals = new InternalValue[values.length];
for (int i = 0; i < values.length; i++) {
NodeId adjusted = refTracker.getMappedId(values[i].getNodeId());
if (adjusted != null) {
newVals[i] = InternalValue.create(adjusted);
modified = true;
} else {
// reference doesn't need adjusting, just copy old value
newVals[i] = values[i];
}
}
if (modified) {
prop.setValues(newVals);
stateMgr.store(prop);
}
}
refTracker.clear();
// store states
stateMgr.store(newState);
stateMgr.store(destParentState);
return newState.getNodeId();
}