LOG.error(ex);
throw new EXistException(ex.getMessage());
}
DBBroker broker = null;
Collection srcCollection = null;
DocumentImpl srcDocument = null;
Collection destCollection = null;
TransactionManager txnManager = brokerPool.getTransactionManager();
Txn txn = txnManager.beginTransaction();
try {
broker = brokerPool.get(subject);
// Need to split path into collection and document name
XmldbURI srcCollectionUri = xmldbUri.removeLastSegment();
XmldbURI srdDocumentUri = xmldbUri.lastSegment();
// Open collection if possible, else abort
srcCollection = broker.openCollection(srcCollectionUri, Lock.WRITE_LOCK);
if (srcCollection == null) {
txnManager.abort(txn);
return; // TODO throw
}
// Open document if possible, else abort
srcDocument = srcCollection.getDocument(broker, srdDocumentUri);
if (srcDocument == null) {
LOG.debug(String.format("No resource found for path: %s", xmldbUri));
txnManager.abort(txn);
return;
}
// Open collection if possible, else abort
destCollection = broker.openCollection(destCollectionUri, Lock.WRITE_LOCK);
if (destCollection == null) {
LOG.debug(String.format("Destination collection %s does not exist.", xmldbUri));
txnManager.abort(txn);
return;
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyResource(txn, srcDocument, destCollection, newNameUri);
} else {
broker.moveResource(txn, srcDocument, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Document %sd sucessfully", mode));
}
} catch (LockException e) {
LOG.error("Resource is locked.", e);
txnManager.abort(txn);
throw new EXistException(e.getMessage());
} catch (EXistException e) {
LOG.error(e);
txnManager.abort(txn);
throw e;
} catch (IOException | PermissionDeniedException | TriggerException e) {
LOG.error(e);
txnManager.abort(txn);
throw new EXistException(e.getMessage());
} finally {
// TODO: check if can be done earlier
if (destCollection != null) {
destCollection.release(Lock.WRITE_LOCK);
}
if (srcCollection != null) {
srcCollection.release(Lock.WRITE_LOCK);
}