*
* @param sourceNode source node state
* @throws ItemStateException if the copy operation fails
*/
private void copy(NodeState sourceNode) throws ItemStateException {
ChangeLog changes = new ChangeLog();
// Copy the node state
NodeState targetNode = target.createNew(sourceNode.getNodeId());
targetNode.setParentId(sourceNode.getParentId());
targetNode.setDefinitionId(sourceNode.getDefinitionId());
targetNode.setNodeTypeName(sourceNode.getNodeTypeName());
targetNode.setMixinTypeNames(sourceNode.getMixinTypeNames());
targetNode.setPropertyNames(sourceNode.getPropertyNames());
targetNode.setChildNodeEntries(sourceNode.getChildNodeEntries());
if (target.exists(targetNode.getNodeId())) {
changes.modified(targetNode);
} else {
changes.added(targetNode);
}
// Copy all associated property states
for (Name name : sourceNode.getPropertyNames()) {
PropertyId id = new PropertyId(sourceNode.getNodeId(), name);
PropertyState sourceState = source.load(id);
PropertyState targetState = target.createNew(id);
targetState.setDefinitionId(sourceState.getDefinitionId());
targetState.setType(sourceState.getType());
targetState.setMultiValued(sourceState.isMultiValued());
// TODO: Copy binaries?
targetState.setValues(sourceState.getValues());
if (target.exists(targetState.getPropertyId())) {
changes.modified(targetState);
} else {
changes.added(targetState);
}
}
// Copy all node references
NodeReferencesId refsId = new NodeReferencesId(sourceNode.getNodeId());
if (source.exists(refsId)) {
changes.modified(source.load(refsId));
} else if (target.exists(refsId)) {
NodeReferences references = target.load(refsId);
references.clearAllReferences();
changes.modified(references);
}
// Persist the copied states
target.store(changes);
}