DocumentImpl newDoc = new DocumentImpl(this, child.getFileName(), destCollection); newDoc.copyOf(child); newDoc.setDocId(getNextDocumentId(transaction, destination)); copyResource(transaction, child, newDoc); storeDocument(transaction, newDoc); destCollection.addDocument(transaction, this, newDoc);
if (child.getResourceType() == DocumentImpl.XML_FILE) { DocumentImpl newDoc = new DocumentImpl(this, child.getFileName(), destCollection); newDoc.copyOf(child); newDoc.setDocId(getNextDocumentId(transaction, destination)); copyResource(transaction, child, newDoc); storeDocument(transaction, newDoc); destCollection.addDocument(transaction, this, newDoc); } else { BinaryDocument newDoc = new BinaryDocument(this, child.getFileName(), destCollection); newDoc.copyOf(child); newDoc.setDocId(getNextDocumentId(transaction, destination)); byte[] data = getBinaryResourceData((BinaryDocument) child); storeBinaryResource(transaction, newDoc, data); storeDocument(transaction, newDoc); destCollection.addDocument(transaction, this, newDoc); }
	public void copyCollection(Txn transaction, Collection collection, Collection destination, String newName)	throws PermissionDeniedException, LockException {		if (readOnly)			throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);		if(!collection.getPermissions().validate(user, Permission.READ))			throw new PermissionDeniedException("Read permission denied on collection " +					collection.getName());		if(collection.getId() == destination.getId())	    	throw new PermissionDeniedException("Cannot move collection to itself");		if(!destination.getPermissions().validate(user, Permission.WRITE))	        throw new PermissionDeniedException("Insufficient privileges on target collection " +	                destination.getName());		if(newName == null) {            ///TODO : use dedicated function in XmldbURI            int p = collection.getName().lastIndexOf("/");            newName = collection.getName().substring(p + 1);        }        ///TODO : use dedicated function in XmldbURI	    if(newName.indexOf("/") != Constants.STRING_NOT_FOUND)	        throw new PermissionDeniedException("New collection name is illegal (may not contain a '/')");	    //	check if another collection with the same name exists at the destination	    //TODO : use dedicated function in XmldbURI        Collection old = openCollection(destination.getName() + "/" + newName, Lock.WRITE_LOCK);	    if(old != null) {	    	LOG.debug("removing old collection: " + newName);	    	try {	    		removeCollection(transaction, old);	    	} finally {	    		old.release();	    	}	    }	    Collection destCollection = null;	    Lock lock = null;	    try {	        lock = collectionsDb.getLock();	        lock.acquire(Lock.WRITE_LOCK);	        ///TODO : use dedicated function in XmldbURI	        newName = destination.getName() + "/" + newName;	        LOG.debug("Copying collection to '" + newName + "'");		    destCollection = getOrCreateCollection(transaction, newName);		    for(Iterator i = collection.iterator(this); i.hasNext(); ) {		    	DocumentImpl child = (DocumentImpl) i.next();		    	LOG.debug("Copying resource: '" + child.getName() + "'");		    	DocumentImpl newDoc = new DocumentImpl(this, child.getFileName(), destCollection);		        newDoc.copyOf(child);                newDoc.setDocId(getNextDocumentId(transaction, destination));		        copyResource(transaction, child, newDoc);                storeDocument(transaction, newDoc);		        destCollection.addDocument(transaction, this, newDoc);		    }		    saveCollection(transaction, destCollection);	    } finally {	        lock.release();	    }	    String name = collection.getName();	    for(Iterator i = collection.collectionIterator(); i.hasNext(); ) {	    	String childName = (String)i.next();            ///TODO : use dedicated function in XmldbURI	        Collection child = openCollection(name + "/" + childName, Lock.WRITE_LOCK);	        if(child == null)	            LOG.warn("Child collection '" + childName + "' not found");	        else {	            try {	            	copyCollection(transaction, child, destCollection, childName);	            } finally {	            	child.release();	            }	        }	    }	    saveCollection(transaction, destCollection);	    saveCollection(transaction, destination);	}