2013-10-24 14:58:56 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2013-10-24 15:32:03 +02:00
|
|
|
package org.gcube.portlets.user.td.csvimportwidget.client.progress;
|
2013-10-24 14:58:56 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2013-10-24 15:32:03 +02:00
|
|
|
import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync;
|
|
|
|
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportMonitor;
|
2013-12-05 15:17:46 +01:00
|
|
|
import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId;
|
2013-10-24 14:58:56 +02:00
|
|
|
|
|
|
|
import com.allen_sauer.gwt.log.client.Log;
|
|
|
|
import com.google.gwt.user.client.Timer;
|
|
|
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
2013-11-05 18:59:55 +01:00
|
|
|
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
|
|
|
|
import com.sencha.gxt.widget.core.client.event.HideEvent;
|
|
|
|
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
|
2013-10-24 14:58:56 +02:00
|
|
|
|
|
|
|
/**
|
2013-11-04 15:44:49 +01:00
|
|
|
* This {@link Timer} retrieves {@link OperationProgress} from the specified
|
|
|
|
* {@link OperationProgressSource} with the scheduled interval. The retrieved
|
|
|
|
* information are spread to the subscribed {@link CSVImportProgressListener}.
|
|
|
|
*
|
|
|
|
* @author "Giancarlo Panichi" <a
|
|
|
|
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
|
2013-10-24 14:58:56 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class CSVImportProgressUpdater extends Timer {
|
|
|
|
|
|
|
|
protected ArrayList<CSVImportProgressListener> listeners = new ArrayList<CSVImportProgressListener>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Log.debug("requesting operation progress");
|
2013-11-04 15:44:49 +01:00
|
|
|
TDGWTServiceAsync.INSTANCE
|
|
|
|
.getCSVImportMonitor(new AsyncCallback<CSVImportMonitor>() {
|
|
|
|
|
2013-12-16 18:51:54 +01:00
|
|
|
|
2013-11-04 15:44:49 +01:00
|
|
|
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(CSVImportMonitor result) {
|
|
|
|
Log.info("retrieved CSVImportMonitor: "
|
|
|
|
+ result.getStatus());
|
|
|
|
switch (result.getStatus()) {
|
|
|
|
case INITIALIZING:
|
|
|
|
Log.info("CSV Import Initializing...");
|
|
|
|
fireOperationInitializing();
|
|
|
|
break;
|
|
|
|
case ABORTED:
|
|
|
|
cancel();
|
|
|
|
Log.info("CSV Import Operation Aborted");
|
|
|
|
break;
|
|
|
|
case IN_PROGRESS:
|
|
|
|
fireOperationUpdate(result.getProgress());
|
|
|
|
break;
|
|
|
|
case FAILED:
|
|
|
|
cancel();
|
2013-11-05 18:59:55 +01:00
|
|
|
errorMessage(result);
|
2013-11-04 15:44:49 +01:00
|
|
|
break;
|
|
|
|
case SUCCEDED:
|
|
|
|
cancel();
|
|
|
|
Log.info("Import fisnish TableId :"
|
|
|
|
+ result.getTrId());
|
|
|
|
fireOperationComplete(result.getTrId());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-11-04 15:47:48 +01:00
|
|
|
Log.info("Unknow State");
|
2013-11-04 15:44:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-24 14:58:56 +02:00
|
|
|
}
|
2013-11-04 15:44:49 +01:00
|
|
|
|
2013-11-05 18:59:55 +01:00
|
|
|
protected void errorMessage(CSVImportMonitor result) {
|
|
|
|
Log.info("CSV Import Failed");
|
|
|
|
Throwable th = null;
|
|
|
|
String failure = null;
|
|
|
|
String details = null;
|
|
|
|
if (result.getError() != null) {
|
|
|
|
th = result.getError();
|
|
|
|
failure = "Failed Client Library Import";
|
|
|
|
details = result.getError().getLocalizedMessage();
|
|
|
|
} else {
|
|
|
|
th = new Throwable("Failed");
|
|
|
|
failure = "Failed Client Library Import";
|
|
|
|
details = "Error in import";
|
|
|
|
}
|
|
|
|
fireOperationFailed(th, failure, details);
|
|
|
|
|
|
|
|
AlertMessageBox d = new AlertMessageBox("Error in CSV Import", details);
|
|
|
|
d.addHideHandler(new HideHandler() {
|
|
|
|
|
|
|
|
public void onHide(HideEvent event) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
d.show();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:44:49 +01:00
|
|
|
protected String getStack(Throwable e) {
|
|
|
|
String message = e.getLocalizedMessage() + " -> <br>";
|
2013-10-24 14:58:56 +02:00
|
|
|
Throwable c = e.getCause();
|
2013-11-04 15:44:49 +01:00
|
|
|
if (c != null)
|
|
|
|
message += getStack(c);
|
2013-10-24 14:58:56 +02:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:44:49 +01:00
|
|
|
protected void fireOperationInitializing() {
|
|
|
|
for (CSVImportProgressListener listener : listeners)
|
|
|
|
listener.operationInitializing();
|
2013-10-24 14:58:56 +02:00
|
|
|
}
|
2013-11-04 15:44:49 +01:00
|
|
|
|
|
|
|
protected void fireOperationUpdate(float elaborated) {
|
|
|
|
for (CSVImportProgressListener listener : listeners)
|
|
|
|
listener.operationUpdate(elaborated);
|
2013-10-24 14:58:56 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 15:44:49 +01:00
|
|
|
protected void fireOperationComplete(TRId trId) {
|
|
|
|
for (CSVImportProgressListener listener : listeners)
|
|
|
|
listener.operationComplete(trId);
|
2013-10-24 14:58:56 +02:00
|
|
|
}
|
2013-11-04 15:44:49 +01:00
|
|
|
|
|
|
|
protected void fireOperationFailed(Throwable caught, String failure,
|
|
|
|
String failureDetails) {
|
|
|
|
for (CSVImportProgressListener listener : listeners)
|
|
|
|
listener.operationFailed(caught, failure, failureDetails);
|
2013-10-24 14:58:56 +02:00
|
|
|
}
|
2013-11-04 15:44:49 +01:00
|
|
|
|
2013-10-24 14:58:56 +02:00
|
|
|
/**
|
2013-11-04 15:44:49 +01:00
|
|
|
* Add a new {@link CSVImportProgressListener} to this
|
|
|
|
* {@link CSVImportProgressUpdater}.
|
|
|
|
*
|
|
|
|
* @param listener
|
|
|
|
* the listener to add.
|
2013-10-24 14:58:56 +02:00
|
|
|
*/
|
2013-11-04 15:44:49 +01:00
|
|
|
public void addListener(CSVImportProgressListener listener) {
|
2013-10-24 14:58:56 +02:00
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-04 15:44:49 +01:00
|
|
|
* Removes the specified {@link CSVImportProgressListener} from this
|
|
|
|
* {@link CSVImportProgressUpdater}.
|
|
|
|
*
|
|
|
|
* @param listener
|
|
|
|
* the listener to remove.
|
2013-10-24 14:58:56 +02:00
|
|
|
*/
|
2013-11-04 15:44:49 +01:00
|
|
|
public void removeListener(CSVImportProgressListener listener) {
|
2013-10-24 14:58:56 +02:00
|
|
|
listeners.remove(listener);
|
|
|
|
}
|
|
|
|
}
|