DBBroker broker = null;
Collection srcCollection = null;
Collection destCollection = null;
TransactionManager txnManager = brokerPool.getTransactionManager();
Txn txn = txnManager.beginTransaction();
try {
broker = brokerPool.get(subject);
// This class contains already the URI of the resource that shall be moved/copied
XmldbURI srcCollectionUri = xmldbUri;
// Open collection if possible, else abort
srcCollection = broker.openCollection(srcCollectionUri, Lock.WRITE_LOCK);
if (srcCollection == null) {
txnManager.abort(txn);
return; // TODO throw
}
// 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; // TODO throw?
}
// Perform actial move/copy
if (mode == Mode.COPY) {
broker.copyCollection(txn, srcCollection, destCollection, newNameUri);
} else {
broker.moveCollection(txn, srcCollection, destCollection, newNameUri);
}
// Commit change
txnManager.commit(txn);
if(LOG.isDebugEnabled())
LOG.debug(String.format("Collection %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 {
if (destCollection != null) {
destCollection.release(Lock.WRITE_LOCK);
}
if (srcCollection != null) {
srcCollection.release(Lock.WRITE_LOCK);
}
txnManager.close(txn);
brokerPool.release(broker);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Finished %s", mode));
}