193 lines
5.1 KiB
Java
193 lines
5.1 KiB
Java
|
/**
|
||
|
*
|
||
|
*/
|
||
|
package org.gcube.portlets.user.td.extractcodelistwidget.client.progress;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
|
||
|
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
|
||
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVExportMonitor;
|
||
|
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"
|
||
|
* <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
public class ExtractCodelistProgressUpdater extends Timer {
|
||
|
|
||
|
protected ArrayList<ExtractCodelistProgressListener> listeners = new ArrayList<ExtractCodelistProgressListener>();
|
||
|
|
||
|
/**
|
||
|
* {@inheritDoc}
|
||
|
*/
|
||
|
@Override
|
||
|
public void run() {
|
||
|
Log.debug("requesting operation progress");
|
||
|
TDGWTServiceAsync.INSTANCE
|
||
|
.getCSVExportMonitor(new AsyncCallback<CSVExportMonitor>() {
|
||
|
|
||
|
|
||
|
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(CSVExportMonitor result) {
|
||
|
Log.info("retrieved CSVImportMonitor: "
|
||
|
+ result.getStatus());
|
||
|
switch (result.getStatus()) {
|
||
|
case INITIALIZING:
|
||
|
Log.info("CSV Export Initializing...");
|
||
|
fireOperationInitializing();
|
||
|
break;
|
||
|
case ABORTED:
|
||
|
cancel();
|
||
|
Log.info("CSV Export 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 FAILED:
|
||
|
cancel();
|
||
|
errorMessage(result);
|
||
|
break;
|
||
|
case STOPPED:
|
||
|
cancel();
|
||
|
stopMessage(result);
|
||
|
break;
|
||
|
case SUCCEDED:
|
||
|
cancel();
|
||
|
Log.info("Export fisnish ExportMetadata:"
|
||
|
+ result.getTrExportMetadata());
|
||
|
fireOperationComplete();
|
||
|
break;
|
||
|
default:
|
||
|
Log.info("Unknow State");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
protected void errorMessage(CSVExportMonitor result) {
|
||
|
Log.info("CSV Export Failed");
|
||
|
Throwable th = null;
|
||
|
String failure = null;
|
||
|
String details = null;
|
||
|
if (result.getError() != null) {
|
||
|
th = result.getError();
|
||
|
failure = "Failed Client Library Export";
|
||
|
details = result.getError().getLocalizedMessage();
|
||
|
} else {
|
||
|
th = new Throwable("Failed");
|
||
|
failure = "Failed Client Library Export";
|
||
|
details = "Error in export";
|
||
|
}
|
||
|
|
||
|
fireOperationFailed(th, failure, details);
|
||
|
}
|
||
|
|
||
|
|
||
|
protected void stopMessage(CSVExportMonitor result) {
|
||
|
Log.info("CSV Export Stopped");
|
||
|
String failure = null;
|
||
|
String details = null;
|
||
|
if (result.getError() != null) {
|
||
|
failure = "Stopped export csv";
|
||
|
details = result.getError().getLocalizedMessage();
|
||
|
} else {
|
||
|
failure = "Stopped export csv";
|
||
|
details = "Stopped export csv";
|
||
|
}
|
||
|
|
||
|
fireOperationStopped(result.getTrId(),failure, details);
|
||
|
}
|
||
|
|
||
|
protected String getStack(Throwable e) {
|
||
|
String message = e.getLocalizedMessage() + " -> <br>";
|
||
|
Throwable c = e.getCause();
|
||
|
if (c != null)
|
||
|
message += getStack(c);
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
protected void fireOperationInitializing() {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationInitializing();
|
||
|
}
|
||
|
|
||
|
protected void fireOperationGeneratingView() {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationGeneratingView();
|
||
|
}
|
||
|
|
||
|
protected void fireOperationUpdate(float elaborated) {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationUpdate(elaborated);
|
||
|
}
|
||
|
|
||
|
protected void fireOperationValidate(float elaborated) {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationValidate(elaborated);
|
||
|
}
|
||
|
|
||
|
protected void fireOperationComplete() {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationComplete();
|
||
|
}
|
||
|
|
||
|
protected void fireOperationFailed(Throwable caught, String failure,
|
||
|
String failureDetails) {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationFailed(caught, failure, failureDetails);
|
||
|
}
|
||
|
|
||
|
protected void fireOperationStopped(TRId trId, String reason, String details) {
|
||
|
for (ExtractCodelistProgressListener listener : listeners)
|
||
|
listener.operationStopped(trId,reason, details);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Add a new {@link ExtractCodelistProgressListener} to this
|
||
|
* {@link ExtractCodelistProgressUpdater}.
|
||
|
*
|
||
|
* @param listener
|
||
|
* the listener to add.
|
||
|
*/
|
||
|
public void addListener(ExtractCodelistProgressListener listener) {
|
||
|
listeners.add(listener);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Removes the specified {@link ExtractCodelistProgressListener} from this
|
||
|
* {@link ExtractCodelistProgressUpdater}.
|
||
|
*
|
||
|
* @param listener
|
||
|
* the listener to remove.
|
||
|
*/
|
||
|
public void removeListener(ExtractCodelistProgressListener listener) {
|
||
|
listeners.remove(listener);
|
||
|
}
|
||
|
}
|