* @param sourceNode source node state
* @throws RepositoryException if the copy operation fails
*/
private void copy(NodeState sourceNode) throws RepositoryException {
try {
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
Iterator iterator = sourceNode.getPropertyNames().iterator();
while (iterator.hasNext()) {
Name name = (Name) iterator.next();
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());
InternalValue[] values = sourceState.getValues();
if (sourceState.getType() == PropertyType.BINARY) {
for (int i = 0; i < values.length; i++) {
InputStream stream = values[i].getBLOBFileValue().getStream();
try {
values[i] = InternalValue.createTemporary(stream, store);
} finally {
stream.close();
}
}
}
targetState.setValues(values);
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);
} catch (IOException e) {