throws RemoteException {
final TransactionManager transact = pool.getTransactionManager();
final Txn transaction = transact.beginTransaction();
DBBroker broker = null;
final Session session = getSession(sessionId);
Collection collection = null;
Collection destination = null;
DocumentImpl doc = null;
try {
broker = pool.get(session.getUser());
final XmldbURI collectionUri = docPath.removeLastSegment();
final XmldbURI docUri = docPath.lastSegment();
if (collectionUri==null || docUri==null) {
transact.abort(transaction);
throw new EXistException("Illegal document path");
}
collection = broker.openCollection(collectionUri, move ? Lock.WRITE_LOCK : Lock.READ_LOCK);
if (collection == null) {
transact.abort(transaction);
throw new RemoteException("Collection " + collectionUri
+ " not found");
}
doc = collection.getDocumentWithLock(broker, docUri, Lock.WRITE_LOCK);
if(doc == null) {
transact.abort(transaction);
throw new RemoteException("Document " + docUri + " not found");
}
// get destination collection
destination = broker.openCollection(destinationPath, Lock.WRITE_LOCK);
if(destination == null) {
transact.abort(transaction);
throw new RemoteException("Destination collection " + destinationPath + " not found");
}
if(move)
// TODO check XML/Binary resource
// broker.moveResource(transaction, doc, destination, newName);
{broker.moveResource(transaction, doc, destination, newName);}
else
// TODO check XML/Binary resource
// broker.copyResource(transaction, doc, destination, newName);
{broker.copyResource(transaction, doc, destination, newName);}
transact.commit(transaction);
// documentCache.clear();
return;
} catch (final LockException e) {
transact.abort(transaction);
throw new RemoteException("Could not acquire lock on document " + docPath);
} catch (final PermissionDeniedException e) {
transact.abort(transaction);
throw new RemoteException("Could not move/copy document " + docPath);
} catch (final IOException e) {
transact.abort(transaction);
throw new RemoteException(e.getMessage());
} catch (final TransactionException e) {
throw new RemoteException("Error commiting transaction " + e.getMessage());
} catch (final EXistException e) {
throw new RemoteException(e.getMessage());
} catch (final TriggerException e) {
transact.abort(transaction);
throw new RemoteException(e.getMessage());
} finally {
transact.close(transaction);
if(destination != null)
{destination.release(Lock.WRITE_LOCK);}
if(doc != null)
{doc.getUpdateLock().release(Lock.WRITE_LOCK);}
if(collection != null)
{collection.release(move ? Lock.WRITE_LOCK : Lock.READ_LOCK);}
pool.release(broker);