270 lines
8.6 KiB
Java
270 lines
8.6 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.widgets.workspaceuploader.client.uploader;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.ClosableDialog;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploadNotification.WorskpaceUploadNotificationListener;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderServiceAsync;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.events.CancelUploadEvent;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.events.CancelUploadEventHandler;
|
|
import org.gcube.portlets.widgets.workspaceuploader.client.resource.WorkspaceUploaderResources;
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.HandlerResultMessage;
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem;
|
|
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem.UPLOAD_STATUS;
|
|
|
|
import com.google.gwt.core.shared.GWT;
|
|
import com.google.gwt.event.shared.HandlerManager;
|
|
import com.google.gwt.http.client.Request;
|
|
import com.google.gwt.http.client.RequestBuilder;
|
|
import com.google.gwt.http.client.RequestCallback;
|
|
import com.google.gwt.http.client.RequestException;
|
|
import com.google.gwt.http.client.Response;
|
|
import com.google.gwt.user.client.Timer;
|
|
import com.google.gwt.user.client.Window;
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
|
import com.google.gwt.user.client.ui.HTML;
|
|
import com.google.gwt.user.client.ui.HorizontalPanel;
|
|
|
|
/**
|
|
* The Class UploaderMonitor.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Sep 7, 2015
|
|
*/
|
|
public class UploaderMonitor {
|
|
|
|
private static UploaderMonitor instance;
|
|
private final static HandlerManager eventBus = new HandlerManager(null);
|
|
private MonitorPanel monitorPanel = new MonitorPanel(eventBus);
|
|
private static Map<String, Timer> mapTimer = new HashMap<String, Timer>();
|
|
private ClosableDialog dialogUploadMonitor = new ClosableDialog(monitorPanel, false, ConstantsWorkspaceUploader.MY_UPLOADS);
|
|
private static List<WorskpaceUploadNotificationListener> listenersUpload = new ArrayList<WorskpaceUploadNotificationListener>();
|
|
/**
|
|
* Gets the single instance of UploaderMonitor.
|
|
*
|
|
* @return single instance of UploaderMonitor
|
|
*/
|
|
public static synchronized UploaderMonitor getInstance() {
|
|
if (instance == null){
|
|
instance = new UploaderMonitor();
|
|
bindEvents();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* Bind events.
|
|
*/
|
|
private static void bindEvents() {
|
|
|
|
eventBus.addHandler(CancelUploadEvent.TYPE, new CancelUploadEventHandler() {
|
|
|
|
@Override
|
|
public void onCancelUpload(final CancelUploadEvent deleteTimerEvent) {
|
|
|
|
if(deleteTimerEvent.getUploader()==null)
|
|
return;
|
|
|
|
String clientKey = deleteTimerEvent.getUploader().getClientUploadKey();
|
|
Timer timer = mapTimer.get(clientKey);
|
|
GWT.log("Delete timer "+timer);
|
|
if(timer!=null && timer.isRunning()){
|
|
timer.cancel();
|
|
mapTimer.put(clientKey, null);
|
|
}
|
|
|
|
RequestBuilder request = new RequestBuilder(RequestBuilder.GET,
|
|
ConstantsWorkspaceUploader.WORKSPACE_UPLOADER_SERVLET_STREAM+"?"+ConstantsWorkspaceUploader.CLIENT_UPLOAD_KEYS+"="+clientKey+"&"+ConstantsWorkspaceUploader.CANCEL_UPLOAD+"=true");
|
|
try {
|
|
request.sendRequest(null, new RequestCallback() {
|
|
|
|
@SuppressWarnings("incomplete-switch")
|
|
@Override
|
|
public void onResponseReceived(Request request, Response response) {
|
|
String strippedResult = new HTML(response.getText()).getText();
|
|
final HandlerResultMessage resultMessage = HandlerResultMessage.parseResult(strippedResult);
|
|
|
|
switch (resultMessage.getStatus()) {
|
|
case ERROR: {
|
|
GWT.log("ERROR: Error during stop upload "+resultMessage.getMessage());
|
|
Window.alert("Sorry, An error occurred during upload abort!");
|
|
return;
|
|
}
|
|
case OK: {
|
|
HorizontalPanel hp = deleteTimerEvent.getHp();
|
|
if(hp!=null){
|
|
hp.clear();
|
|
HTML html = new HTML();
|
|
hp.add(html);
|
|
String text = "<div><img src='"+WorkspaceUploaderResources.getImageCancelRed().getUrl()+"'>";
|
|
text+="<span style='margin-left:5px; vertical-align: top;'>Upload "+deleteTimerEvent.getFileName()+ " aborted</span></div>";
|
|
html.setHTML(text);
|
|
deleteTimerEvent.getUploader().setUploadStatus(UPLOAD_STATUS.ABORTED);
|
|
notifyUploadAborted(deleteTimerEvent.getUploader().getFile().getParentId(), null);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(Request request, Throwable exception) {
|
|
GWT.log("ERROR: Error during stop upload "+exception.getMessage());
|
|
|
|
}
|
|
});
|
|
} catch (RequestException e) {
|
|
GWT.log("RequestException: Error during stop upload "+e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Show monitor.
|
|
*/
|
|
public void showMonitor(){
|
|
if(!dialogUploadMonitor.isShowing())
|
|
dialogUploadMonitor.center();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Adds the new submit.
|
|
*
|
|
* @param uploader the uploader
|
|
* @param fileName the file name
|
|
*/
|
|
public void addNewSubmit(WorkspaceUploaderItem uploader, String fileName){
|
|
if(!dialogUploadMonitor.isShowing())
|
|
dialogUploadMonitor.center();
|
|
|
|
monitorPanel.newWorkspaceUploder(uploader, fileName);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Poll workspace uploader.
|
|
*
|
|
* @param workspaceUploader the workspace uploader item
|
|
*/
|
|
public void pollWorkspaceUploader(final WorkspaceUploaderItem workspaceUploader){
|
|
|
|
Timer timer = new Timer() {
|
|
|
|
@Override
|
|
public void run() {
|
|
WorkspaceUploaderServiceAsync.Util.getInstance().getUploadStatus(workspaceUploader.getClientUploadKey(), new AsyncCallback<WorkspaceUploaderItem>() {
|
|
|
|
@Override
|
|
public void onSuccess(WorkspaceUploaderItem result) {
|
|
// GWT.log("onSuccess: "+result.toString());
|
|
synchronized(this){
|
|
Timer tmn = mapTimer.get(workspaceUploader.getClientUploadKey());
|
|
if(tmn!=null && tmn.isRunning()){
|
|
if(result.getUploadStatus().equals(UPLOAD_STATUS.COMPLETED)){
|
|
mapTimer.remove(tmn);
|
|
notifyUploadCompleted(result.getFile().getParentId(), result.getFile().getItemId());
|
|
// new DialogResult(null, "Upload Completed!!", result.getStatusDescription()).center();
|
|
cancel();
|
|
}else if(result.getUploadStatus().equals(UPLOAD_STATUS.FAILED)){
|
|
mapTimer.remove(tmn);
|
|
notifyUploadError(result.getFile().getParentId(), null, null);
|
|
// new DialogResult(null, "Upload Failed!!", result.getStatusDescription()).center();
|
|
cancel();
|
|
}
|
|
monitorPanel.updateWorkspaceUploder(result);
|
|
// GWT.log(result.toString());
|
|
}else{
|
|
GWT.log("Timer is null or already closed or completed");
|
|
cancel();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
GWT.log("onFailure: "+caught.getMessage());
|
|
notifyUploadError(workspaceUploader.getFile().getParentId(), null, caught);
|
|
cancel();
|
|
}
|
|
});
|
|
|
|
}
|
|
};
|
|
mapTimer.put(workspaceUploader.getClientUploadKey(), timer);
|
|
timer.scheduleRepeating(2000);
|
|
}
|
|
|
|
|
|
/**
|
|
* Notify upload completed.
|
|
*
|
|
* @param parentId the parent id
|
|
* @param itemId the item id
|
|
*/
|
|
private static void notifyUploadCompleted(String parentId, String itemId){
|
|
for (WorskpaceUploadNotificationListener listener : listenersUpload) {
|
|
listener.onUploadCompleted(parentId, itemId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notify upload aborted.
|
|
*
|
|
* @param parentId the parent id
|
|
* @param itemId the item id
|
|
*/
|
|
private static void notifyUploadAborted(String parentId, String itemId){
|
|
for (WorskpaceUploadNotificationListener listener : listenersUpload) {
|
|
listener.onUploadAborted(parentId, itemId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notify upload error.
|
|
*
|
|
* @param parentId the parent id
|
|
* @param itemId the item id
|
|
* @param t the t
|
|
*/
|
|
private static void notifyUploadError(String parentId, String itemId, Throwable t){
|
|
for (WorskpaceUploadNotificationListener listener : listenersUpload) {
|
|
listener.onError(parentId, itemId, t);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Adds the workspace upload listener.
|
|
*
|
|
* @param handler the handler
|
|
*/
|
|
public void addWorkspaceUploadListener(WorskpaceUploadNotificationListener handler) {
|
|
this.listenersUpload.add(handler);
|
|
|
|
}
|
|
|
|
/**
|
|
* Removes the workspace upload listener.
|
|
*
|
|
* @param handler the handler
|
|
*/
|
|
public void removeWorkspaceUploadListener(WorskpaceUploadNotificationListener handler) {
|
|
try {
|
|
this.listenersUpload.remove(handler);
|
|
} catch (Exception e) {
|
|
// SILENT
|
|
}
|
|
}
|
|
}
|