JAL - 3690 AlignCalc rebuilt - FutureTask-based manager
[jalview.git] / src / jalview / ws / jws2 / SeqAnnotationServiceCalcWorker.java
index df3a311..ae03dfb 100644 (file)
@@ -227,8 +227,13 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
   private int min_valid_seqs;
 
   @Override
-  public void run()
+  public void run() throws Exception
   {
+    if (alignViewport.isClosed())
+    {
+      abortAndDestroy();
+      return;
+    }
     if (!hasService())
     {
       return;
@@ -244,10 +249,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     WebserviceInfo info = new WebserviceInfo("foo", "bar", false);
     try
     {
-      if (checkDone())
-      {
-        return;
-      }
       List<SequenceI> seqs = getInputSequences(
               alignViewport.getAlignment(),
               bySequence ? alignViewport.getSelectionGroup() : null);
@@ -256,7 +257,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       {
         jalview.bin.Cache.log.debug(
                 "Sequences for analysis service were null or not valid");
-        calcMan.workerComplete(this);
         return;
       }
 
@@ -275,7 +275,9 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         return;
       }
       // TODO: handle job submission error reporting here.
-      
+      Cache.log.debug("Service " + service.getUri() + "\nSubmitted job ID: "
+              + rslt);
+      ;
       // ///
       // otherwise, construct WsJob and any UI handlers
       running = new AnnotationWsJob();
@@ -326,41 +328,13 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
           Cache.log.debug("Status now " + running.getState());
         }
 
-        if (calcMan.isPending(this) && isInteractiveUpdate())
-        {
-          Cache.log.debug("Analysis service job is stale. aborting.");
-          // job has become stale.
-          if (!finished) {
-            finished = true;
-            // cancel this job and yield to the new job
-            try
-            {
-              if (cancellable
-                        && ((CancellableI) annotService).cancel(running))
-              {
-                System.err.println("Cancelled job: " + rslt);
-              }
-              else
-              {
-                System.err.println("FAILED TO CANCEL job: " + rslt);
-              }
-  
-            } catch (Exception x)
-            {
-  
-            }
-          }
-          rslt = running.getJobHandle();
-          return;
-        }
-
         // pull any stats - some services need to flush log output before
         // results are available
         Cache.log.debug("Updating progress log for annotation service.");
 
         try
         {
-        annotService.updateJobProgress(running);
+          annotService.updateJobProgress(running);
         } catch (Throwable thr)
         {
           Cache.log.debug("Ignoring exception during progress update.",
@@ -421,6 +395,24 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
         // sequence, excluding regions not annotated due to gapMap/column
         // visibility
 
+        // update calcId if it is not already set on returned annotation
+        if (returnedAnnot != null)
+        {
+          for (AlignmentAnnotation aa : returnedAnnot)
+          {
+            // assume that any CalcIds already set
+            if (getCalcId() != null && aa.getCalcId() == null
+                    || "".equals(aa.getCalcId()))
+            {
+              aa.setCalcId(getCalcId());
+            }
+            // autocalculated annotation are created by interactive alignment
+            // analysis services
+            aa.autoCalculated = service.isAlignmentAnalysis()
+                    && service.isInteractiveUpdate();
+          }
+        }
+
         running.setAnnotation(returnedAnnot);
 
         if (running.hasResults())
@@ -502,13 +494,12 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       System.err
               .println("Blacklisting worker due to unexpected exception:");
       x.printStackTrace();
+      throw new Exception(x);
     } finally
     {
 
-      calcMan.workerComplete(this);
       if (ap != null)
       {
-        calcMan.workerComplete(this);
         if (guiProgress != null && progressId != -1)
         {
           guiProgress.setProgressBar("", progressId);
@@ -817,7 +808,7 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
       for (SequenceI sq : running.getSeqs())
       {
         if (!sq.getFeatures().hasFeatures()
-                && (sq.getDBRefs() == null || sq.getDBRefs().length == 0))
+                && (sq.getDBRefs() == null || sq.getDBRefs().size() == 0))
         {
           continue;
         }
@@ -869,43 +860,6 @@ public class SeqAnnotationServiceCalcWorker extends AlignCalcWorker
     }
   }
 
-  /**
-   * notify manager that we have started, and wait for a free calculation slot
-   * 
-   * @return true if slot is obtained and work still valid, false if another
-   *         thread has done our work for us.
-   */
-  protected boolean checkDone()
-  {
-    calcMan.notifyStart(this);
-    ap.paintAlignment(false, false);
-    while (!calcMan.notifyWorking(this))
-    {
-      if (calcMan.isWorking(this))
-      {
-        return true;
-      }
-      try
-      {
-        if (ap != null)
-        {
-          ap.paintAlignment(false, false);
-        }
-
-        Thread.sleep(200);
-      } catch (Exception ex)
-      {
-        ex.printStackTrace();
-      }
-    }
-    if (alignViewport.isClosed())
-    {
-      abortAndDestroy();
-      return true;
-    }
-    return false;
-  }
-
   protected void updateOurAnnots(List<AlignmentAnnotation> ourAnnot)
   {
     List<AlignmentAnnotation> our = ourAnnots;