/** * Deletes the folder with the given reference. * * @param folderReference the reference to the folder to delete */ private void deleteFolder(DocumentReference folderReference) { if (fileSystem.canDelete(folderReference)) { Folder folder = fileSystem.getFolder(folderReference); if (folder == null) { return; } List<DocumentReference> childFolderReferences = folder.getChildFolderReferences(); List<DocumentReference> childFileReferences = folder.getChildFileReferences(); notifyPushLevelProgress(childFolderReferences.size() + childFileReferences.size() + 1); try { for (DocumentReference childFolderReference : childFolderReferences) { deleteFolder(childFolderReference); notifyStepPropress(); } for (DocumentReference childFileReference : childFileReferences) { deleteFile(childFileReference, folderReference); notifyStepPropress(); } // Delete the folder if it's empty. if (folder.getChildFolderReferences().isEmpty() && folder.getChildFileReferences().isEmpty()) { fileSystem.delete(folderReference); } notifyStepPropress(); } finally { notifyPopLevelProgress(); } } else { this.logger.error("You are not allowed to delete the folder [{}].", folderReference); } }
/** * Deletes a file from one of its parent folders. If the given parent folder reference is {@code * null} then the file is deleted from all of its parent folders. * * @param fileReference the file to delete * @param parentReference the folder the file should be deleted from, {@code null} if the file * should be delete from all parents */ private void deleteFile(DocumentReference fileReference, DocumentReference parentReference) { File file = fileSystem.getFile(fileReference); if (file != null) { Collection<DocumentReference> parentReferences = file.getParentReferences(); boolean save = parentReferences.remove(parentReference); if (parentReferences.isEmpty() || parentReference == null) { if (fileSystem.canDelete(fileReference)) { fileSystem.delete(fileReference); } else { this.logger.error("You are not allowed to delete the file [{}].", fileReference); } } else if (save) { if (fileSystem.canEdit(fileReference)) { fileSystem.save(file); } else { this.logger.error("You are not allowed to edit the file [{}].", fileReference); } } } }