Merge branch 'release/5.1.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / ProgressMonitorClientManager.java
index c50686fa191d2779dd56624e9b7bb6c84c69170e..7cef26a1d2bcbfc717f8d8db264598be1a195bd9 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2015 EDIT
  * European Distributed Institute of Taxonomy
@@ -15,10 +14,11 @@ import java.util.List;
 import java.util.UUID;
 
 import org.apache.log4j.Logger;
-import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubMonitor;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
+import eu.etaxonomy.cdm.api.service.UpdateResult;
 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
 import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
 import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
@@ -54,7 +54,7 @@ public class ProgressMonitorClientManager {
             final int pollInterval,
             final IPostMoniteredOperationEnabled postOp,
             IFeedbackGenerator feedbackGenerator,
-            IProgressMonitor monitor) throws InterruptedException {
+            SubMonitor monitor) throws InterruptedException {
         return pollMonitor(label, uuid, pollInterval, postOp, Arrays.asList(feedbackGenerator), monitor);
     }
     /**
@@ -79,28 +79,50 @@ public class ProgressMonitorClientManager {
             final int pollInterval,
             final IPostMoniteredOperationEnabled postOp,
             List<IFeedbackGenerator> feedbackGenerators,
-            IProgressMonitor monitor) throws InterruptedException {
+            SubMonitor 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 ON THE REMOTING MONITOR)
+            if ( remotingMonitor.isDone()){
+                return remotingMonitor;
+            }
             for(int i=0;i<START_DELAY;i++) {
-                Thread.sleep(1000);
+                Thread.sleep(10);
                 logger.info("Waiting for monitered work to start ..");
                 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
-                if(remotingMonitor.getTotalWork() > 0) {
+                if (remotingMonitor != null){
+                    if(remotingMonitor.getTotalWork() > 0) {
+                        break;
+                    }
                     break;
                 }
             }
+
+            if (remotingMonitor == null){
+                return null;
+            }
             // if the total work is still not been set then we assume that the
             // operation has zero work units
-            if(remotingMonitor.getTotalWork() == 0) {
-                throw new InterruptedException("Monitor has zero work units");
+
+            if(remotingMonitor.getTotalWork() == 0 && remotingMonitor.isDone()) {
+                return remotingMonitor;
+
+            }else if (remotingMonitor.getTotalWork() == 0 ){
+                Object result = remotingMonitor.getResult();
+                InterruptedException exception = new InterruptedException("Monitor has zero work units");
+                if (result instanceof UpdateResult){
+                    ((UpdateResult)result).addException(exception);
+                }else if (result == null){
+                    result = new UpdateResult();
+                }
+
             }
             // start the client monitor
-            monitor.beginTask(label, remotingMonitor.getTotalWork());
+           // monitor.subTask(remotingMonitor.getTaskName());
+            monitor.beginTask(remotingMonitor.getTaskName(), remotingMonitor.getTotalWork());
             logger.info("Work to be done: " + remotingMonitor.getTotalWork());
             int editorTotalWorkDone = 0;
             int serverTotalWorkDone = 0;
@@ -113,37 +135,56 @@ public class ProgressMonitorClientManager {
                 Thread.sleep(pollInterval);
                 remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
                 // check if remoting monitor is waiting for feedback
-                if(remotingMonitor.isWaitingForFeedback()) {
+                if(remotingMonitor.getIsWaitingForFeedback()) {
                     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");
+                            IllegalStateException exception = new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
+                            Object result = remotingMonitor.getResult();
+                            if (result instanceof UpdateResult){
+                                ((UpdateResult)result).addException(exception);
+                            }
+                            return remotingMonitor;
                         }
-                        progressMonitorService.setFeedback(uuid, feedbackGenerators.get(feedbackCount).generateFeedback());
+                        feedbackGenerators.get(feedbackCount).setFeedbackForMonitor(uuid);
                         feedbackCount++;
                     }
                 }
                 serverTotalWorkDone = (int) remotingMonitor.getWorkDone();
                 logger.info("Work done from start: " + serverTotalWorkDone);
-                String percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
+                String percentage = "100";
+                if (remotingMonitor.getTotalWork() != 0){
+                    percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
+                }
+
+
                 // set dialog text
                 monitor.setTaskName(label + " " + percentage + "% done ");
                 monitor.subTask(remotingMonitor.getSubTask());
-                int worked = serverTotalWorkDone - editorTotalWorkDone;
+//                monitor.setWorkRemaining((int) (remotingMonitor.getTotalWork()-remotingMonitor.getWorkDone()));
+               // monitor.subTask(remotingMonitor.getSubTask(), remotingMonitor.getWorkDone());
+                int worked = 0;
+                if (serverTotalWorkDone > editorTotalWorkDone){
+                    worked = serverTotalWorkDone - editorTotalWorkDone;
+                }
                 if(worked > 0) {
                     logger.info("Work done since last check: " + worked);
                     monitor.worked(worked);
                 }
+
+
                 editorTotalWorkDone = serverTotalWorkDone;
             }
             if(remotingMonitor.getResult() instanceof Exception) {
-                throw new IllegalStateException((Exception)remotingMonitor.getResult());
+                UpdateResult result = new UpdateResult();
+                result.addException((Exception) remotingMonitor.getResult());
+//                throw new IllegalStateException((Exception)remotingMonitor.getResult());
             }
             return remotingMonitor;
         } finally {
-            if(postOp != null && remotingMonitor.isDone()) {
+            if(postOp != null && remotingMonitor != null &&  remotingMonitor.isDone()) {
                 postOp.postOperation(remotingMonitor);
             }
         }