diff --git a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/CloneTabularResource.java b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/CloneTabularResource.java index 0858af5..a2a84a9 100644 --- a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/CloneTabularResource.java +++ b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/CloneTabularResource.java @@ -3,11 +3,9 @@ package org.gcube.portlets.user.td.tablewidget.client; import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync; import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException; import org.gcube.portlets.user.td.gwtservice.shared.tr.clone.CloneTabularResourceSession; +import org.gcube.portlets.user.td.tablewidget.client.progress.CloneProgressDialog; import org.gcube.portlets.user.td.tablewidget.client.util.UtilsGXT3; -import org.gcube.portlets.user.td.widgetcommonevent.client.event.ChangeTableRequestEvent; import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent; -import org.gcube.portlets.user.td.widgetcommonevent.client.type.ChangeTableRequestType; -import org.gcube.portlets.user.td.widgetcommonevent.client.type.ChangeTableWhy; import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType; import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; @@ -34,7 +32,7 @@ public class CloneTabularResource { protected void onCloneTR() { TDGWTServiceAsync.INSTANCE.startCloneTabularResource( - cloneTabularResourceSession, new AsyncCallback() { + cloneTabularResourceSession, new AsyncCallback() { public void onFailure(Throwable caught) { if (caught instanceof TDGWTSessionExpiredException) { @@ -48,14 +46,12 @@ public class CloneTabularResource { } } - public void onSuccess(TRId result) { - Log.info("TR Clone:" + result); - final ChangeTableRequestEvent event = new ChangeTableRequestEvent( - ChangeTableRequestType.CLONETABULARRESOURCE, - result, ChangeTableWhy.TABLEUPDATED); - eventBus.fireEvent(event); - UtilsGXT3 - .info("Clone", "Clone Tabular Resource Succed"); + public void onSuccess(Void result) { + Log.debug("Start TR Clone"); + @SuppressWarnings("unused") + CloneProgressDialog cloneProgressDialog = new CloneProgressDialog( + eventBus); + } }); diff --git a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressBarUpdater.java b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressBarUpdater.java new file mode 100644 index 0000000..6496d75 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressBarUpdater.java @@ -0,0 +1,98 @@ +/** + * + */ +package org.gcube.portlets.user.td.tablewidget.client.progress; + + + + +import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; + +import com.allen_sauer.gwt.log.client.Log; +import com.sencha.gxt.widget.core.client.ProgressBar; + +/** + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public class CloneProgressBarUpdater implements CloneProgressListener { + + protected ProgressBar progressBar; + + /** + * Creates a new {@link ProgressBar} updater. + * @param progressBar the {@link ProgressBar} to update. + */ + public CloneProgressBarUpdater(ProgressBar progressBar) { + this.progressBar = progressBar; + this.progressBar.updateProgress(0, "Please Wait..."); + } + + + /** + * {@inheritDoc} + */ + @Override + public void operationComplete(TRId trId) { + Log.info("Completed"); + progressBar.updateProgress(1, "Completed"); + + } + + /** + * {@inheritDoc} + */ + @Override + public void operationFailed(Throwable caught, String reason, String failureDetails) { + Log.info("Failed"); + progressBar.updateText("Failed"); + } + + @Override + public void operationInitializing() { + Log.info("Inizializing"); + progressBar.updateProgress(0, "Initializing..."); + } + + @Override + public void operationUpdate(float elaborated) { + Log.info("Elaborated: "+elaborated); + if (elaborated>=0 && elaborated<1) { + Log.trace("progress "+elaborated); + int elab=new Float(elaborated*100).intValue(); + progressBar.updateProgress(elaborated,elab+"% Progress..."); + } + if (elaborated == 1) progressBar.updateProgress(1, "Completing..."); + + } + + @Override + public void operationValidate(float elaborated) { + Log.info("Validation Elaborated: "+elaborated); + if (elaborated == 0) progressBar.updateProgress(0, "Start Validation..."); + if (elaborated>0 && elaborated<1) { + Log.trace("Validation progress "+elaborated); + int elab=new Float(elaborated*100).intValue(); + progressBar.updateProgress(elaborated,elab+"% Validation Progress..."); + } + if (elaborated == 1) progressBar.updateProgress(1, "Validation..."); + } + + + @Override + public void operationStopped(TRId trId,String reason, String details) { + Log.debug("Operation Stopped: ["+trId.toString()+", "+reason+", "+details+"]"); + progressBar.updateText("Validations failed"); + + } + + @Override + public void operationGeneratingView() { + Log.info("Generating View..."); + progressBar.updateText("Generating View..."); + + } + +} diff --git a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressDialog.java b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressDialog.java new file mode 100644 index 0000000..b776e0e --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressDialog.java @@ -0,0 +1,146 @@ +package org.gcube.portlets.user.td.tablewidget.client.progress; + +import org.gcube.portlets.user.td.gwtservice.shared.exception.TDGWTSessionExpiredException; +import org.gcube.portlets.user.td.tablewidget.client.util.UtilsGXT3; +import org.gcube.portlets.user.td.widgetcommonevent.client.event.ChangeTableRequestEvent; +import org.gcube.portlets.user.td.widgetcommonevent.client.event.SessionExpiredEvent; +import org.gcube.portlets.user.td.widgetcommonevent.client.type.ChangeTableRequestType; +import org.gcube.portlets.user.td.widgetcommonevent.client.type.ChangeTableWhy; +import org.gcube.portlets.user.td.widgetcommonevent.client.type.SessionExpiredType; +import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; + +import com.allen_sauer.gwt.log.client.Log; +import com.google.web.bindery.event.shared.EventBus; +import com.sencha.gxt.core.client.util.Margins; +import com.sencha.gxt.widget.core.client.FramedPanel; +import com.sencha.gxt.widget.core.client.ProgressBar; +import com.sencha.gxt.widget.core.client.Window; +import com.sencha.gxt.widget.core.client.button.TextButton; +import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; +import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; +import com.sencha.gxt.widget.core.client.event.SelectEvent; +import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler; + +/** + * + * + * @author "Giancarlo Panichi" g.panichi@isti.cnr.it + * + */ +public class CloneProgressDialog extends Window implements + CloneProgressListener { + public static final int STATUS_POLLING_DELAY = 1000; + protected String WIDTH = "400px"; + protected String HEIGHT = "120px"; + protected EventBus eventBus; + protected CloneProgressUpdater progressUpdater; + protected TextButton ok; + protected TRId trId; + private ChangeTableWhy why; + + public CloneProgressDialog(EventBus eventBus) { + this.eventBus = eventBus; + setWidth(WIDTH); + setHeight(HEIGHT); + setBodyBorder(false); + setResizable(true); + setModal(true); + setHeadingText("Clone Progress"); + + trId = null; + + FramedPanel panel = new FramedPanel(); + panel.setHeaderVisible(false); + panel.setBodyBorder(false); + + VerticalLayoutContainer v = new VerticalLayoutContainer(); + + ProgressBar progressBar = new ProgressBar(); + + ok = new TextButton("OK"); + ok.addSelectHandler(new SelectHandler() { + + public void onSelect(SelectEvent event) { + updateInvocation(); + + } + }); + + v.add(progressBar, + new VerticalLayoutData(1, 1, new Margins(5, 5, 5, 5))); + + panel.add(v); + panel.addButton(ok); + add(panel); + + progressUpdater = new CloneProgressUpdater(); + progressUpdater.addListener(new CloneProgressBarUpdater(progressBar)); + + progressUpdater.addListener(this); + progressUpdater.scheduleRepeating(STATUS_POLLING_DELAY); + show(); + ok.setVisible(false); + + } + + public void operationInitializing() { + // TODO Auto-generated method stub + + } + + public void operationUpdate(float elaborated) { + // TODO Auto-generated method stub + + } + + public void operationComplete(TRId trId) { + Log.debug("Operation Complete return: " + trId.toString()); + this.trId = trId; + why = ChangeTableWhy.TABLEUPDATED; + updateInvocation(); + } + + public void operationFailed(Throwable caught, String reason, + String failureDetails) { + if (caught instanceof TDGWTSessionExpiredException) { + eventBus.fireEvent(new SessionExpiredEvent( + SessionExpiredType.EXPIREDONSERVER)); + } else { + ok.setVisible(true); + this.trId = null; + UtilsGXT3.alert("Error", reason); + } + } + + public void updateInvocation() { + if (trId != null) { + ChangeTableRequestEvent event = new ChangeTableRequestEvent( + ChangeTableRequestType.CLONETABULARRESOURCE, trId, why); + eventBus.fireEvent(event); + } + + hide(); + } + + @Override + public void operationStopped(TRId trId, String reason, String details) { + Log.debug("Operation Stopped: [" + trId.toString() + ", " + reason + + ", " + details + "]"); + ok.setVisible(true); + this.trId = trId; + why = ChangeTableWhy.TABLECURATION; + } + + @Override + public void operationGeneratingView() { + // TODO Auto-generated method stub + + } + + @Override + public void operationValidate(float elaborated) { + // TODO Auto-generated method stub + + } +} diff --git a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressListener.java b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressListener.java new file mode 100644 index 0000000..02644fb --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressListener.java @@ -0,0 +1,63 @@ +/** + * + */ +package org.gcube.portlets.user.td.tablewidget.client.progress; + +import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; + + + +/** + * Defines a listener for operation progress. + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public interface CloneProgressListener { + + /** + * Called when the operation is starting. + */ + public void operationInitializing(); + + /** + * Called when there is a progress for the operation. + * @param elaborated the elaborated part. + */ + public void operationUpdate(float elaborated); + + /** + * Called when there is a validate for the operation. + * @param elaborated the elaborated part. + */ + public void operationValidate(float elaborated); + + /** + * Called when the operation is complete. + */ + public void operationComplete(TRId trId); + + /** + * Called when the operation is failed. + * @param caught the failure exception. + * @param reason the failure reason. + */ + public void operationFailed(Throwable caught, String reason, String failureDetails); + + /** + * Called when the operation is stopped + * + * @param trId + * @param reason + * @param details + */ + public void operationStopped(TRId trId, String reason, String details); + + + /** + * Called when the operation is generating the view + */ + public void operationGeneratingView(); + +} diff --git a/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressUpdater.java b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressUpdater.java new file mode 100644 index 0000000..4540dec --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/td/tablewidget/client/progress/CloneProgressUpdater.java @@ -0,0 +1,195 @@ +/** + * + */ +package org.gcube.portlets.user.td.tablewidget.client.progress; + +import java.util.ArrayList; + +import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync; +import org.gcube.portlets.user.td.gwtservice.shared.monitor.CloneMonitor; +import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; + +import com.allen_sauer.gwt.log.client.Log; +import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.rpc.AsyncCallback; + + +/** + * + * @author "Giancarlo Panichi" + * g.panichi@isti.cnr.it + * + */ +public class CloneProgressUpdater extends Timer { + + protected ArrayList listeners = new ArrayList(); + + /** + * {@inheritDoc} + */ + @Override + public void run() { + Log.debug("requesting operation progress"); + TDGWTServiceAsync.INSTANCE + .getCloneMonitor(new AsyncCallback() { + + + public void onFailure(Throwable caught) { + cancel(); + Log.error("Error retrieving the operation state", + caught); + String message = getStack(caught); + fireOperationFailed(caught, + "Failed getting operation updates", message); + } + + public void onSuccess(CloneMonitor result) { + Log.info("retrieved CloneMonitor: " + + result.getStatus()); + switch (result.getStatus()) { + case INITIALIZING: + Log.info("Initializing..."); + fireOperationInitializing(); + break; + case ABORTED: + cancel(); + Log.info("Operation Aborted"); + break; + case IN_PROGRESS: + fireOperationUpdate(result.getProgress()); + break; + case VALIDATING_RULES: + fireOperationValidate(result.getProgress()); + break; + case GENERATING_VIEW: + Log.info("Generating View..."); + fireOperationGeneratingView(); + break; + case STOPPED: + cancel(); + stopMessage(result); + break; + case FAILED: + cancel(); + errorMessage(result); + break; + case SUCCEDED: + cancel(); + Log.info("Fisnish TableId :" + + result.getTrId()); + fireOperationComplete(result.getTrId()); + break; + default: + Log.info("Unknow State"); + break; + } + + } + + + + }); + + } + + protected void errorMessage(CloneMonitor result) { + Log.info("Clone Failed"); + Throwable th = null; + String failure = null; + String details = null; + if (result.getError() != null) { + th = result.getError(); + failure = "Failed Clone operation"; + details = result.getError().getLocalizedMessage(); + } else { + th = new Throwable("Failed"); + failure = "Failed Clone operation"; + details = "Clone failed"; + } + + fireOperationFailed(th, failure, details); + } + + protected void stopMessage(CloneMonitor result) { + Log.info("Clone Stopped"); + String failure = null; + String details = null; + if (result.getError() != null) { + failure = "Stopped operation"; + details = result.getError().getLocalizedMessage(); + } else { + failure = "Stopped operation"; + details = "Operation stopped"; + } + + fireOperationStopped(result.getTrId(),failure, details); + } + + + protected String getStack(Throwable e) { + String message = e.getLocalizedMessage() + " ->
"; + Throwable c = e.getCause(); + if (c != null) + message += getStack(c); + return message; + } + + protected void fireOperationInitializing() { + for (CloneProgressListener listener : listeners) + listener.operationInitializing(); + } + + protected void fireOperationGeneratingView() { + for (CloneProgressListener listener : listeners) + listener.operationGeneratingView(); + } + + protected void fireOperationUpdate(float elaborated) { + for (CloneProgressListener listener : listeners) + listener.operationUpdate(elaborated); + } + + protected void fireOperationValidate(float elaborated) { + for (CloneProgressListener listener : listeners) + listener.operationValidate(elaborated); + } + + protected void fireOperationComplete(TRId trId) { + for (CloneProgressListener listener : listeners) + listener.operationComplete(trId); + } + + protected void fireOperationFailed(Throwable caught, String failure, + String failureDetails) { + for (CloneProgressListener listener : listeners) + listener.operationFailed(caught, failure, failureDetails); + } + + protected void fireOperationStopped(TRId trId, String reason, String details) { + for (CloneProgressListener listener : listeners) + listener.operationStopped(trId,reason, details); + } + + + /** + * Add a new {@link CloneProgressListener} to this + * {@link CloneProgressUpdater}. + * + * @param listener + * the listener to add. + */ + public void addListener(CloneProgressListener listener) { + listeners.add(listener); + } + + /** + * Removes the specified {@link CloneProgressListener} from this + * {@link CloneProgressUpdater}. + * + * @param listener + * the listener to remove. + */ + public void removeListener(CloneProgressListener listener) { + listeners.remove(listener); + } +}