#5297 Implement monitor feedback, Add corresponding tests
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / ProgressMonitorClientManager.java
index 62f00179d4bc65bd240fecea12c384fdf7824f65..c50686fa191d2779dd56624e9b7bb6c84c69170e 100644 (file)
@@ -10,6 +10,8 @@
 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;
@@ -18,6 +20,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
 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;
 
 /**
@@ -31,6 +34,29 @@ 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.
@@ -41,6 +67,9 @@ public class ProgressMonitorClientManager {
      * @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
@@ -49,12 +78,14 @@ public class ProgressMonitorClientManager {
             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 ..");
@@ -74,15 +105,30 @@ public class ProgressMonitorClientManager {
             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;
@@ -92,9 +138,12 @@ public class ProgressMonitorClientManager {
                 }
                 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);
             }
         }