Lock lock = collectionsDb.getLock();
    public void moveXMLResource(Txn transaction, DocumentImpl doc, Collection destination, XmldbURI newName)    throws PermissionDeniedException, LockException {        if (readOnly)            throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);                //TODO : somewhat inconsistent (READ is enough for original doc whereas WRITE is mandatory for destination) -pb        Collection collection = doc.getCollection();                        if(!collection.getPermissions().validate(user, Permission.WRITE))            throw new PermissionDeniedException("Insufficient privileges to move resource " +                    doc.getFileURI());        if(!doc.getPermissions().validate(user, Permission.WRITE))            throw new PermissionDeniedException("Insufficient privileges to move resource " +                    doc.getFileURI());              User docUser = doc.getUserLock();        if (docUser != null) {           if(!(user.getName()).equals(docUser.getName()))                throw new PermissionDeniedException("Cannot move '" + doc.getFileURI() +                         " because is locked by user '" + docUser.getName() + "'");        }                if(newName==null) {        	newName = doc.getFileURI();        }        Lock lock = collectionsDb.getLock();        try {            lock.acquire(Lock.WRITE_LOCK);            // check if the move would overwrite a collection            if(getCollection(destination.getURI().append(newName)) != null)                throw new PermissionDeniedException("A resource can not replace an existing collection");            DocumentImpl oldDoc = destination.getDocument(this, newName);            if(oldDoc != null) {                if(doc.getDocId() == oldDoc.getDocId())                    throw new PermissionDeniedException("Cannot move resource to itself");                if(!destination.getPermissions().validate(user, Permission.UPDATE))                    throw new PermissionDeniedException("Resource with same name exists in target " +                            "collection and update is denied");                if(!oldDoc.getPermissions().validate(user, Permission.UPDATE))                    throw new PermissionDeniedException("Resource with same name exists in target " +                            "collection and update is denied");                if (oldDoc.getResourceType() == DocumentImpl.BINARY_FILE)                    destination.removeBinaryResource(transaction, this, oldDoc);                else                    destination.removeXMLResource(transaction, this, oldDoc.getFileURI());            } else                if(!destination.getPermissions().validate(user, Permission.WRITE))                    throw new PermissionDeniedException("Insufficient privileges on target collection " +                            destination.getURI());                            boolean renameOnly = collection.getId() == destination.getId();            collection.unlinkDocument(doc);            removeResourceMetadata(transaction, doc);                        doc.setFileURI(newName);            doc.setCollection(destination);            if (doc.getResourceType() == DocumentImpl.XML_FILE) {                if(!renameOnly) {                    notifyDropIndex(doc);                    saveCollection(transaction, collection);                }                destination.addDocument(transaction, this, doc);                    if(!renameOnly) {                    // reindexing                    reindexXMLResource(transaction, doc, true);                }            } else {                // binary resource                destination.addDocument(transaction, this, doc);            }            storeXMLResource(transaction, doc);            saveCollection(transaction, destination);        } catch (TriggerException e) {            throw new PermissionDeniedException(e.getMessage());        } catch (ReadOnlyException e) {            throw new PermissionDeniedException(e.getMessage());        } finally {            lock.release();        }    }