destination.addBinaryResource(transaction, this, newName, data, doc.getMimeType());
destination.addBinaryResource(transaction, this, newName, data, doc.getMetadata().getMimeType());
	public void copyResource(Txn transaction, DocumentImpl doc, Collection destination, String newName) 	throws PermissionDeniedException, LockException {		if (readOnly)			throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);	    Collection collection = doc.getCollection();	    if(!collection.getPermissions().validate(user, Permission.READ))	        throw new PermissionDeniedException("Insufficient privileges to copy resource " +	                doc.getFileName());	    if(!doc.getPermissions().validate(user, Permission.READ))	    	throw new PermissionDeniedException("Insufficient privileges to copy resource " +	                doc.getFileName());	    if(newName == null) {            ///TODO : use dedicated function in XmldbURI            int p = doc.getFileName().lastIndexOf("/");            newName = doc.getFileName().substring(p + 1);        }	    Lock lock = null;	    try {	        lock = collectionsDb.getLock();	        lock.acquire(Lock.WRITE_LOCK);	        // check if the move would overwrite a collection            ///TODO : use dedicated function in XmldbURI	        if(getCollection(destination.getName() + "/" + 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 copy 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.removeDocument(transaction, this, oldDoc.getFileName());	        } else {	        	if(!destination.getPermissions().validate(user, Permission.WRITE))	    	        throw new PermissionDeniedException("Insufficient privileges on target collection " +	    	                destination.getName());	        }            if (doc.getResourceType() == DocumentImpl.BINARY_FILE)  {                byte[] data = getBinaryResourceData((BinaryDocument) doc);                 destination.addBinaryResource(transaction, this, newName, data, doc.getMimeType());            } else {                DocumentImpl newDoc = new DocumentImpl(this, newName, destination);                newDoc.copyOf(doc);                newDoc.setDocId(getNextDocumentId(transaction, destination));                newDoc.setPermissions(doc.getPermissions());                 copyResource(transaction, doc, newDoc);                destination.addDocument(transaction, this, newDoc);                storeDocument(transaction, newDoc);            }//	        saveCollection(destination);        } catch (EXistException e) {            LOG.warn("An error occurred while copying resource", e);		} catch (TriggerException e) {			throw new PermissionDeniedException(e.getMessage());		} finally {	    	lock.release();	    }	}