JAL-3878 Add interactive flag to WebService class
[jalview.git] / src / jalview / ws / WSClient.java
index 80414ef..a68f3f3 100755 (executable)
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ws;
 
+import jalview.bin.Cache;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
 import jalview.gui.WebserviceInfo;
+import jalview.gui.WsJobParameters;
+import jalview.util.MessageManager;
+import jalview.ws.api.ServiceWithParameters;
+import jalview.ws.api.UIinfo;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws.params.WsParamSetI;
 
-public class WSClient
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+
+public abstract class WSClient // implements WSMenuEntryProviderI
 {
+  /**
+   * WSClient holds the basic attributes that are displayed to the user for all
+   * jalview web service clients
+   */
+  /**
+   * displayed name for this web service
+   */
+  protected String WebServiceName;
+
+  /**
+   * specific job title (e.g. 'ClustalW Alignment of Selection from Aligment
+   * from Cut and Paste input')
+   */
+  protected String WebServiceJobTitle;
+
+  /**
+   * String giving additional information such as method citations for this
+   * service
+   */
+  protected String WebServiceReference;
+
+  /**
+   * Service endpoint
+   */
+  protected String WsURL;
+
+  /**
+   * Web service information used to initialise the WSClient attributes
+   */
+  protected WebserviceInfo wsInfo;
+
+  /**
+   * the root object for the service client
+   */
+  protected UIinfo serviceHandle;
+
+  /**
+   * total number of jobs managed by this web service client instance.
+   */
+  int jobsRunning = 0;
+
+  /**
+   * TODO: this is really service metadata, and should be moved elsewhere.
+   * mappings between abstract interface names and menu entries
+   */
+  protected java.util.Hashtable ServiceActions;
+
+  /**
+   * alignFrame associated with this client
+   */
+  protected AlignFrame alignFrame;
+  {
+    ServiceActions = new java.util.Hashtable();
+    ServiceActions.put("MsaWS", "Multiple Sequence Alignment");
+    ServiceActions.put("SecStrPred", "Secondary Structure Prediction");
+  };
+
+  /**
+   * The preset for the job executed by this client (may be null)
+   */
+  protected WsParamSetI preset;
+
+  /**
+   * The parameters for the job executed by this client (may be null)
+   */
+  protected List<ArgumentI> paramset;
+
   public WSClient()
   {
   }
+
   /**
+   * base constructor for a web service with parameters. Extending classes
+   * should implement this constructor with additional logic to verify that
+   * preset and arguments are compatible with the service being configured.
+   * 
+   * @param _alignFrame
+   * @param preset
+   * @param arguments
+   */
+  public WSClient(AlignFrame _alignFrame, WsParamSetI preset,
+      List<ArgumentI> arguments)
+  {
+    alignFrame = _alignFrame;
+    this.preset = preset;
+    this.paramset = arguments;
+  }
 
-  * MsaWSClient
-  *
-  * @param msa SequenceI[]
-  */
+  protected WebserviceInfo setWebService(UIinfo serv, boolean b)
+  {
+    WebServiceName = serv.getName();
+    WebServiceJobTitle = serv.getActionText();
+    WsURL = serv.getHostURL();
+    if (!b)
+    {
+      return new WebserviceInfo(WebServiceJobTitle,
+          WebServiceJobTitle + " using service hosted at "
+              + WsURL + "\n"
+              + (serv.getDescription() != null
+                  ? serv.getDescription()
+                  : ""),
+          false);
+    }
+    return null;
+  }
+
+  /**
+   * called to open a parameter editing dialog for parameterised services
+   * 
+   * @param sh
+   * @param editParams
+   * @return
+   */
+  protected CompletionStage<Boolean> processParams(ServiceWithParameters sh,
+      boolean editParams)
+  {
+    return processParams(sh, editParams, false);
+  }
+
+  protected CompletionStage<Boolean> processParams(ServiceWithParameters sh,
+      boolean editParams, boolean adjustingExisting)
+  {
+
+    if (editParams)
+    {
+      // always do this
+      sh.initParamStore(Desktop.getUserParameterStore());
+
+      WsJobParameters jobParams = (preset == null && paramset != null
+          && paramset.size() > 0)
+              ? new WsJobParameters((ParamDatastoreI) null, sh,
+                  (WsParamSetI) null, paramset)
+              : new WsJobParameters((ParamDatastoreI) null, sh,
+                  preset, (List<ArgumentI>) null);
+      if (adjustingExisting)
+      {
+        jobParams.setName(MessageManager
+            .getString("label.adjusting_parameters_for_calculation"));
+      }
+      var stage = jobParams.showRunDialog();
+      return stage.thenApply((startJob) -> {
+        if (startJob)
+        {
+          WsParamSetI prset = jobParams.getPreset();
+          if (prset == null)
+          {
+            paramset = jobParams.isServiceDefaults() ? null
+                : jobParams.getJobParams();
+            this.preset = null;
+          }
+          else
+          {
+            this.preset = prset; // ((JabaPreset) prset).p;
+            paramset = null; // no user supplied parameters.
+          }
+        }
+        return startJob;
+      });
+
+    }
+    return CompletableFuture.completedFuture(true);
+  }
 
- protected String WebServiceName;
- protected String WebServiceJobTitle;
- protected String WebServiceReference;
- protected String WsURL;
- protected WebserviceInfo wsInfo;
-  int jobsRunning = 0;
 }