package eu.etaxonomy.taxeditor.util;
import java.text.DecimalFormat;
+import java.util.Arrays;
+import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
+import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
/**
public class ProgressMonitorClientManager {
private static final Logger logger = Logger.getLogger(ProgressMonitorClientManager.class);
+ /**
+ * Polls the progress monitor service for the progress status of a monitor
+ * corresponding to the given uuid.
+ *
+ * @param label for the operation
+ * @param uuid of the remoting monitor already started on the server
+ * @param pollInterval in milliseconds
+ * @param cancelable flag which determines whether the operation can be cancelled
+ * @param postOp callback for running post operation logic
+ * @param feedbackGenerator feedback generator corresponding to the
+ * 'wait on feedback' request made on the remoting monitor
+ * @param monitor to be updated
+ * @return a final progress monitor after the operation is completed
+ * @throws InterruptedException
+ */
+ public IRemotingProgressMonitor pollMonitor(final String label,
+ final UUID uuid,
+ final int pollInterval,
+ final IPostMoniteredOperationEnabled postOp,
+ IFeedbackGenerator feedbackGenerator,
+ IProgressMonitor monitor) throws InterruptedException {
+ return pollMonitor(label, uuid, pollInterval, postOp, Arrays.asList(feedbackGenerator), monitor);
+ }
/**
* Polls the progress monitor service for the progress status of a monitor
* corresponding to the given uuid.
* @param pollInterval in milliseconds
* @param cancelable flag which determines whether the operation can be cancelled
* @param postOp callback for running post operation logic
+ * @param feedbackGenerators list of feedback generators corresponding to the
+ * size and exact order of the 'wait on feedback' requests made on the
+ * remoting monitor
* @param monitor to be updated
* @return a final progress monitor after the operation is completed
* @throws InterruptedException
final UUID uuid,
final int pollInterval,
final IPostMoniteredOperationEnabled postOp,
+ List<IFeedbackGenerator> feedbackGenerators,
IProgressMonitor monitor) throws InterruptedException {
IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
try {
final int START_DELAY=10;
- // wait about 10 seconds for the remoting monitor to be initialised (i.e. for the begin task method to be called)
+ // wait about 10 seconds for the remoting monitor to be initialised
+ // (i.e. for the begin task method to be called ON THE REMOTING MONITOR)
for(int i=0;i<START_DELAY;i++) {
Thread.sleep(1000);
logger.info("Waiting for monitered work to start ..");
int editorTotalWorkDone = 0;
int serverTotalWorkDone = 0;
// loop until the operation is done
+ int feedbackCount = 0;
while(!(remotingMonitor.isCanceled() || remotingMonitor.isFailed() || remotingMonitor.isDone())) {
// wait for pollInterval, then
// .... retrieve remoting monitor, then
// .... set client monitor info
Thread.sleep(pollInterval);
remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
+ // check if remoting monitor is waiting for feedback
+ if(remotingMonitor.isWaitingForFeedback()) {
+ if(feedbackGenerators != null) {
+ // if we have run out of feedback generators while
+ // the remoting monitor is waiting on feedback
+ // then throw exception
+ if(feedbackCount + 1 > feedbackGenerators.size()) {
+ throw new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
+ }
+ progressMonitorService.setFeedback(uuid, feedbackGenerators.get(feedbackCount).generateFeedback());
+ feedbackCount++;
+ }
+ }
serverTotalWorkDone = (int) remotingMonitor.getWorkDone();
- logger.warn("Work done from start: " + serverTotalWorkDone);
+ logger.info("Work done from start: " + serverTotalWorkDone);
String percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
+ // set dialog text
monitor.setTaskName(label + " " + percentage + "% done ");
monitor.subTask(remotingMonitor.getSubTask());
int worked = serverTotalWorkDone - editorTotalWorkDone;
}
editorTotalWorkDone = serverTotalWorkDone;
}
+ if(remotingMonitor.getResult() instanceof Exception) {
+ throw new IllegalStateException((Exception)remotingMonitor.getResult());
+ }
return remotingMonitor;
} finally {
- if(postOp != null) {
+ if(postOp != null && remotingMonitor.isDone()) {
postOp.postOperation(remotingMonitor);
}
}