Hi All,
There is a feature in our app where users can upload a .csv file. Then it will be processed, modified and downloaded to the user’s pc. Then the app removes it from the fileStorage. Now I’ve found it a little bit tricky, because the file removal overtakes the file download. Solved it with a new Thread and sleep it until the file download finishes but come on…
Here is the original method (without thread sleep), invoked by a button:
public void uploadAndImportCsv() {
FileUploadDialog dialog = (FileUploadDialog) screens.create("fileUploadDialog", OpenMode.DIALOG);
dialog.addAfterCloseListener(event -> {
UUID fileId = dialog.getFileId();
String fileName = dialog.getFileName();
try {
dataImportService.asd(fileUploadingAPI.getFile(fileId));
FileDescriptor fileDescriptor = fileUploadingAPI.getFileDescriptor(fileId, fileName);
fileDescriptor.setName(OPPORTUNITY_ERRORS_CSV);
fileUploadingAPI.putFileIntoStorage(fileId, fileDescriptor);
dataManager.commit(fileDescriptor);
exportDisplay.show(fileDescriptor);
fileStorageService.removeFile(fileDescriptor);
} catch (FileStorageException e) {
log.error(e.getMessage());
}
});
screens.show(dialog);
}
Any idea how can I make it work without thread sleep and any black magic?
Thanks,
Balázs
Pinyazhin
(Roman Pinyazhin)
November 2, 2020, 1:36pm
#3
Hello,
Take a look at background tasks (see documentation ). Background tasks mechanism performs tasks asynchronously without blocking the user interface. It also can show the progress dialog of the task.
For instance,
Background task class
private class DownloadFileTask extends BackgroundTask<Void, FileDescriptor> {
private FileDescriptor fd;
public DownloadFileTask(FileDescriptor fd) {
super(5, TimeUnit.SECONDS, TestScreen.this);
this.fd = fd;
}
@Override
public FileDescriptor run(TaskLifeCycle<Void> taskLifeCycle) throws Exception {
return fd;
}
@Override
public void done(FileDescriptor result) {
exportDisplay.show(result);
}
}
Task progress listener
private class DownloadFileTaskProgressListener extends BackgroundTask.ProgressListenerAdapter<Void, FileDescriptor> {
@Override
public void onDone(FileDescriptor result) {
try {
fileStorageService.removeFile(result);
dataManager.remove(result);
} catch (FileStorageException e) {
log.error("Cannot remove file", e);
}
}
}
FileUploadDialog code
public void uploadAndImportCsv() {
FileUploadDialog dialog = (FileUploadDialog) screens.create("fileUploadDialog", OpenMode.DIALOG);
dialog.addAfterCloseListener((event) -> {
if (event.closedWith(StandardOutcome.COMMIT)) {
UUID fileId = dialog.getFileId();
String fileName = dialog.getFileName();
try {
dataImportService.asd(fileUploadingAPI.getFile(fileId));
FileDescriptor fileDescriptor = fileUploadingAPI.getFileDescriptor(fileId, fileName);
fileDescriptor.setName(OPPORTUNITY_ERRORS_CSV);
fileUploadingAPI.putFileIntoStorage(fileId, fileDescriptor);
dataManager.commit(fileDescriptor);
DownloadFileTask task = new DownloadFileTask(fileDescriptor);
task.addProgressListener(new DownloadFileTaskProgressListener());
BackgroundWorkWindow.show(task);
} catch (FileStorageException e) {
log.error("Cannot save file", e);
}
} else if (event.closedWith(StandardOutcome.CLOSE)) {
// do something
}
});
screens.show(dialog);
}