Refs #1744 [Desktop] Show progress window during file upload

This commit is contained in:
Yuriy Artamonov 2012-12-21 13:10:27 +00:00
parent 0c13c383af
commit 4bea93b0b6
9 changed files with 97 additions and 117 deletions

View File

@ -7,11 +7,8 @@
package com.haulmont.cuba.desktop.gui.components;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.ConfigProvider;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.FileUploadField;
import com.haulmont.cuba.gui.components.IFrame;
@ -37,10 +34,11 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
private static final int BYTES_IN_MEGABYTE = 1048576;
protected FileUploadingAPI fileUploading;
protected Messages messages;
protected volatile boolean isUploadingState = false;
protected boolean isUploadingState = false;
protected String fileName;
protected byte[] bytes;
protected String description;
@ -54,7 +52,7 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
final JFileChooser fileChooser = new JFileChooser();
String caption = MessageProvider.getMessage(getClass(), "export.selectFile");
String caption = messages.getMessage(getClass(), "export.selectFile");
impl = new JButton();
impl.setAction(new AbstractAction(caption) {
@Override
@ -67,11 +65,11 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
}
private void uploadFile(File file) {
final Integer maxUploadSizeMb = ConfigProvider.getConfig(ClientConfig.class).getMaxUploadSizeMb();
final Integer maxUploadSizeMb = AppBeans.get(Configuration.class).getConfig(ClientConfig.class).getMaxUploadSizeMb();
final long maxSize = maxUploadSizeMb * BYTES_IN_MEGABYTE;
if (file.length() > maxSize) {
String warningMsg = MessageProvider.getMessage(AppConfig.getMessagesPack(), "upload.fileTooBig.message");
String warningMsg = messages.getMessage(AppConfig.getMessagesPack(), "upload.fileTooBig.message");
getFrame().showNotification(warningMsg, IFrame.NotificationType.WARNING);
} else {
boolean succcess = true;
@ -135,11 +133,6 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
}
}
@Override
public String getFilePath() {
return fileName;
}
@Override
public String getFileName() {
String[] strings = fileName.split("[/\\\\]");
@ -147,24 +140,26 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
}
@Override
public boolean isUploading() {
return isUploadingState;
public FileDescriptor getFileDescriptor() {
if (fileId != null)
return fileUploading.getFileDescriptor(fileId, fileName);
else
return null;
}
@Override
public byte[] getBytes() {
if (bytes == null) {
try {
if (fileId != null) {
File file = fileUploading.getFile(fileId);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
IOUtils.copy(fileInputStream, byteOutput);
bytes = byteOutput.toByteArray();
}
} catch (Exception e) {
throw new RuntimeException(e);
byte[] bytes = null;
try {
if (fileId != null) {
File file = fileUploading.getFile(fileId);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
IOUtils.copy(fileInputStream, byteOutput);
bytes = byteOutput.toByteArray();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return bytes;
@ -175,16 +170,6 @@ public class DesktopFileUploadField extends DesktopAbstractComponent<JButton> im
return fileId;
}
@Override
public long getBytesRead() {
return 0;
}
@Override
public void release() {
bytes = null;
}
@Override
public void addListener(Listener listener) {
if (!listeners.contains(listener)) listeners.add(listener);

View File

@ -6,7 +6,8 @@
package com.haulmont.cuba.desktop.gui.executors.impl;
import com.haulmont.cuba.core.global.UserSessionProvider;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.executors.*;
import com.haulmont.cuba.gui.executors.impl.TaskExecutor;
import com.haulmont.cuba.gui.executors.impl.TaskHandlerImpl;
@ -69,7 +70,7 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
private DesktopTaskExecutor(BackgroundTask<T, V> runnableTask) {
this.runnableTask = runnableTask;
userId = UserSessionProvider.getUserSession().getId();
userId = AppBeans.get(UserSessionSource.class).getUserSession().getId();
//noinspection unchecked
this.params = runnableTask.getParams();

View File

@ -39,9 +39,7 @@ import java.util.Map;
* @author ovchinnikov
* @version $Id$
*/
@SuppressWarnings("unused")
public class BackgroundWorkProgressWindow<T extends Number, V> extends AbstractWindow {
private static final long serialVersionUID = -3073224246530486376L;
@Inject
private Label text;

View File

@ -9,6 +9,8 @@
*/
package com.haulmont.cuba.gui.components;
import com.haulmont.cuba.core.entity.FileDescriptor;
import java.util.UUID;
public interface FileUploadField
@ -60,10 +62,13 @@ public interface FileUploadField
}
}
String getFilePath();
/**
* Get id for uploaded file in {@link com.haulmont.cuba.gui.upload.FileUploading}
* @return File Id
*/
UUID getFileId();
String getFileName();
boolean isUploading();
FileDescriptor getFileDescriptor();
/**
* Get content bytes for uploaded file
@ -72,15 +77,6 @@ public interface FileUploadField
*/
byte[] getBytes();
/**
* Get id for uploaded file in {@link com.haulmont.cuba.gui.upload.FileUploading}
* @return File Id
*/
UUID getFileId();
long getBytesRead();
void release();
void addListener(Listener listener);
void removeListener(Listener listener);
}

View File

@ -6,8 +6,9 @@
package com.haulmont.cuba.gui.executors.impl;
import com.haulmont.cuba.core.global.TimeProvider;
import com.haulmont.cuba.core.global.UserSessionProvider;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.TimeSource;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.executors.BackgroundTask;
@ -44,7 +45,7 @@ public class TaskHandlerImpl<T, V> implements BackgroundTaskHandler<V> {
public TaskHandlerImpl(TaskExecutor<T, V> taskExecutor, WatchDog watchDog) {
this.taskExecutor = taskExecutor;
this.watchDog = watchDog;
this.userSession = UserSessionProvider.getUserSession();
this.userSession = AppBeans.get(UserSessionSource.class).getUserSession();
BackgroundTask<T, V> task = taskExecutor.getTask();
if (task.getOwnerWindow() != null) {
@ -70,7 +71,7 @@ public class TaskHandlerImpl<T, V> implements BackgroundTaskHandler<V> {
UUID userId = getUserSession().getId();
Window ownerWindow = getTask().getOwnerWindow();
String windowClass = ownerWindow.getClass().getCanonicalName();
log.debug("Window closed. User: " + userId + " Window: " + windowClass);
log.trace("Window closed. User: " + userId + " Window: " + windowClass);
taskExecutor.cancelExecution();
}
@ -82,12 +83,12 @@ public class TaskHandlerImpl<T, V> implements BackgroundTaskHandler<V> {
this.started = true;
this.startTimeStamp = TimeProvider.currentTimestamp().getTime();
this.startTimeStamp = AppBeans.get(TimeSource.class).currentTimestamp().getTime();
this.watchDog.manageTask(this);
UUID userId = getUserSession().getId();
log.debug("Run task. User: " + userId);
log.trace("Run task. User: " + userId);
taskExecutor.startExecution();
}
@ -149,9 +150,9 @@ public class TaskHandlerImpl<T, V> implements BackgroundTaskHandler<V> {
if (ownerWindow != null) {
String windowClass = ownerWindow.getClass().getCanonicalName();
log.debug("Task killed. User: " + userId + " Window: " + windowClass);
log.trace("Task killed. User: " + userId + " Window: " + windowClass);
} else
log.debug("Task killed. User: " + userId);
log.trace("Task killed. User: " + userId);
}
taskExecutor.cancelExecution();

View File

@ -17,14 +17,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
* @author artamonov
* @version $Id$
*/
public class FileStorageEntity extends FileEntity {
public class FileStorageProgressEntity extends FileEntity {
private final long size;
private final UUID fileId;
private final FileUploadingAPI.UploadToStorageProgressListener listener;
public FileStorageEntity(File file, String contentType, UUID fileId,
FileUploadingAPI.UploadToStorageProgressListener listener) {
public FileStorageProgressEntity(File file, String contentType, UUID fileId,
FileUploadingAPI.UploadToStorageProgressListener listener) {
super(file, contentType);
this.listener = listener;
@ -47,6 +47,9 @@ public class FileStorageEntity extends FileEntity {
byte[] tmp = new byte[4096];
int readedBytes;
while ((readedBytes = instream.read(tmp)) != -1) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedIOException();
outstream.write(tmp, 0, readedBytes);
transferredBytes += readedBytes;

View File

@ -214,14 +214,14 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
if (file.exists()) {
boolean res = file.delete();
if (!res)
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, file.getAbsolutePath());
log.warn("Could not delete temp file " + file.getAbsolutePath());
}
}
}
@Override
public void deleteFileLink(String fileName) {
Map<UUID, File> clonedFileMap = new ConcurrentHashMap<>(tempFiles);
Map<UUID, File> clonedFileMap = new HashMap<>(tempFiles);
Iterator<Map.Entry<UUID, File>> iterator = clonedFileMap.entrySet().iterator();
UUID forDelete = null;
while ((iterator.hasNext()) && (forDelete == null)) {
@ -236,13 +236,18 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
@Override
public void putFileIntoStorage(UUID fileId, FileDescriptor fileDescr) throws FileStorageException {
uploadFileIntoStorage(fileId, fileDescr, null);
try {
uploadFileIntoStorage(fileId, fileDescr, null);
} catch (InterruptedIOException e) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fileDescr.getFileName());
}
deleteFile(fileId);
}
private void uploadFileIntoStorage(UUID fileId, FileDescriptor fileDescr,
@Nullable UploadToStorageProgressListener listener) throws FileStorageException {
@Nullable UploadToStorageProgressListener listener)
throws FileStorageException, InterruptedIOException {
File file = getFile(fileId);
for (Iterator<String> iterator = clusterInvocationSupport.getUrlList().iterator(); iterator.hasNext(); ) {
@ -254,7 +259,7 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
HttpPost method = new HttpPost(url);
FileEntity entity;
if (listener != null)
entity = new FileStorageEntity(file, "application/octet-stream", fileId, listener);
entity = new FileStorageProgressEntity(file, "application/octet-stream", fileId, listener);
else
entity = new FileEntity(file, "application/octet-stream");
@ -272,6 +277,9 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
else
throw new FileStorageException(FileStorageException.Type.fromHttpStatus(statusCode), fileDescr.getName());
}
} catch (InterruptedIOException e) {
log.trace("Uploading has been interrupted");
throw e;
} catch (IOException e) {
log.debug("Unable to upload file to " + url + "\n" + e);
if (iterator.hasNext())
@ -285,7 +293,9 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
}
@Override
public FileDescriptor putFileIntoStorage(final TaskLifeCycle<Long> taskLifeCycle) throws FileStorageException {
public FileDescriptor putFileIntoStorage(final TaskLifeCycle<Long> taskLifeCycle)
throws FileStorageException, InterruptedIOException {
checkNotNull(taskLifeCycle);
UUID fileId = (UUID) taskLifeCycle.getParams().get("fileId");
@ -337,10 +347,8 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
@Override
public String showTempFiles() {
StringBuilder builder = new StringBuilder();
Map<UUID, File> clonedFileMap = new ConcurrentHashMap<>(tempFiles);
Iterator<Map.Entry<UUID, File>> iterator = clonedFileMap.entrySet().iterator();
while ((iterator.hasNext())) {
Map.Entry<UUID, File> fileEntry = iterator.next();
Map<UUID, File> clonedFileMap = new HashMap<>(tempFiles);
for (Map.Entry<UUID, File> fileEntry : clonedFileMap.entrySet()) {
builder.append(fileEntry.getKey().toString()).append(" | ");
Date lastModified = new Date(fileEntry.getValue().lastModified());
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

View File

@ -12,6 +12,7 @@ import com.haulmont.cuba.gui.executors.TaskLifeCycle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.util.UUID;
/**
@ -68,5 +69,6 @@ public interface FileUploadingAPI {
* @return file descriptor
* @throws FileStorageException
*/
FileDescriptor putFileIntoStorage(TaskLifeCycle<Long> taskLifeCycle) throws FileStorageException;
FileDescriptor putFileIntoStorage(TaskLifeCycle<Long> taskLifeCycle)
throws FileStorageException, InterruptedIOException;
}

View File

@ -6,10 +6,11 @@
package com.haulmont.cuba.web.gui.components;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.ConfigProvider;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.FileUploadField;
import com.haulmont.cuba.gui.components.IFrame;
@ -34,9 +35,9 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
private static final int BYTES_IN_MEGABYTE = 1048576;
protected FileUploadingAPI fileUploading;
protected Messages messages;
protected String fileName;
protected byte[] bytes;
protected UUID fileId;
@ -49,7 +50,7 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
public WebFileUploadField() {
fileUploading = AppBeans.get(FileUploadingAPI.NAME);
String caption = MessageProvider.getMessage(AppConfig.getMessagesPack(), "Upload");
String caption = messages.getMessage(AppConfig.getMessagesPack(), "Upload");
component = new Upload(
/* Fixes caption rendering.
* If caption == "", the VerticalLayout reserves an empty space */
@ -75,14 +76,13 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
component.addListener(new Upload.StartedListener() {
@Override
public void uploadStarted(Upload.StartedEvent event) {
final Integer maxUploadSizeMb = ConfigProvider.getConfig(ClientConfig.class).getMaxUploadSizeMb();
final Integer maxUploadSizeMb = AppBeans.get(Configuration.class).getConfig(ClientConfig.class).getMaxUploadSizeMb();
final long maxSize = maxUploadSizeMb * BYTES_IN_MEGABYTE;
if (event.getContentLength() > maxSize) {
component.interruptUpload();
String warningMsg = MessageProvider.getMessage(AppConfig.getMessagesPack(), "upload.fileTooBig.message");
String warningMsg = messages.getMessage(AppConfig.getMessagesPack(), "upload.fileTooBig.message");
getFrame().showNotification(warningMsg, IFrame.NotificationType.WARNING);
} else {
bytes = null;
final Listener.Event e = new Listener.Event(event.getFilename());
for (Listener listener : listeners) {
listener.uploadStarted(e);
@ -144,12 +144,7 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
}
}
});
component.setButtonCaption(MessageProvider.getMessage(AppConfig.getMessagesPack(), "upload.submit"));
}
@Override
public String getFilePath() {
return fileName;
component.setButtonCaption(messages.getMessage(AppConfig.getMessagesPack(), "upload.submit"));
}
@Override
@ -158,22 +153,6 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
return strings[strings.length - 1];
}
@Override
public boolean isUploading() {
return component.isUploading();
}
@Override
public long getBytesRead() {
return component.getBytesRead();
}
@Override
public void release() {
outputStream = null;
bytes = null;
}
@Override
public void addListener(Listener listener) {
if (!listeners.contains(listener)) listeners.add(listener);
@ -192,18 +171,17 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
*/
@Deprecated
public byte[] getBytes() {
if (bytes == null) {
try {
if (fileId != null) {
File file = fileUploading.getFile(fileId);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
IOUtils.copy(fileInputStream, byteOutput);
bytes = byteOutput.toByteArray();
}
} catch (Exception e) {
throw new RuntimeException(e);
byte[] bytes = null;
try {
if (fileId != null) {
File file = fileUploading.getFile(fileId);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
IOUtils.copy(fileInputStream, byteOutput);
bytes = byteOutput.toByteArray();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return bytes;
@ -244,4 +222,12 @@ public class WebFileUploadField extends WebAbstractComponent<Upload> implements
public UUID getFileId() {
return fileId;
}
}
@Override
public FileDescriptor getFileDescriptor() {
if (fileId != null)
return fileUploading.getFileDescriptor(fileId, fileName);
else
return null;
}
}