222 lines
7.3 KiB
Java
222 lines
7.3 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.widgets.workspaceuploader.client.uploader;
|
|
|
|
import java.util.HashMap;
|
|
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.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);
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
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);
|
|
}
|
|
|
|
// Timer timer = mapTimer.get(deleteTimerEvent.getClientUploadKey());
|
|
// GWT.log("Delete timer "+timer);
|
|
// if(timer!=null && timer.isRunning()){
|
|
// timer.cancel();
|
|
// mapTimer.put(deleteTimerEvent.getClientUploadKey(), null);
|
|
// }
|
|
//
|
|
// SubmitEvent submitEvent = mapSubmitEvent.get(deleteTimerEvent.getClientUploadKey());
|
|
// GWT.log("Delete SubmitEvent "+submitEvent);
|
|
// if(submitEvent!=null){
|
|
// submitEvent.cancel();
|
|
// mapSubmitEvent.put(deleteTimerEvent.getClientUploadKey(), null);
|
|
// }
|
|
|
|
// DialogBox dialog = mapDialog.get(deleteTimerEvent.getClientUploadKey());
|
|
// GWT.log("Delete dialog "+dialog);
|
|
// if(dialog!=null){
|
|
//// dialog.setVisible(true);
|
|
// dialog.hide();
|
|
// mapDialog.put(deleteTimerEvent.getClientUploadKey(), 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());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public void showMonitor(){
|
|
if(!dialogUploadMonitor.isShowing())
|
|
dialogUploadMonitor.center();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Adds the new submit.
|
|
*
|
|
* @param uploader the uploader
|
|
* @param fileName the file name
|
|
* @param event
|
|
* @param dialog
|
|
*/
|
|
public void addNewSubmit(WorkspaceUploaderItem uploader, String fileName){
|
|
if(!dialogUploadMonitor.isShowing())
|
|
dialogUploadMonitor.center();
|
|
|
|
monitorPanel.newWorkspaceUploder(uploader, fileName);
|
|
}
|
|
|
|
|
|
/**
|
|
* Poll workspace uploader id.
|
|
*
|
|
* @param uploaderId the uploader id
|
|
*/
|
|
public void pollWorkspaceUploaderId(final String uploaderId){
|
|
|
|
Timer timer = new Timer() {
|
|
|
|
@Override
|
|
public void run() {
|
|
WorkspaceUploaderServiceAsync.Util.getInstance().getUploadStatus(uploaderId, new AsyncCallback<WorkspaceUploaderItem>() {
|
|
|
|
@Override
|
|
public void onSuccess(WorkspaceUploaderItem result) {
|
|
// GWT.log("onSuccess: "+result.toString());
|
|
synchronized(this){
|
|
Timer tmn = mapTimer.get(uploaderId);
|
|
if(tmn!=null && tmn.isRunning()){
|
|
if(result.getUploadStatus().equals(UPLOAD_STATUS.COMPLETED)){
|
|
mapTimer.remove(tmn);
|
|
// new DialogResult(null, "Upload Completed!!", result.getStatusDescription()).center();
|
|
cancel();
|
|
}else if(result.getUploadStatus().equals(UPLOAD_STATUS.FAILED)){
|
|
mapTimer.remove(tmn);
|
|
// 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());
|
|
cancel();
|
|
}
|
|
});
|
|
|
|
}
|
|
};
|
|
mapTimer.put(uploaderId, timer);
|
|
timer.scheduleRepeating(2000);
|
|
}
|
|
}
|