Merge branch 'alpha/JAL-3362_Jalview_212_alpha' into alpha/merge_212_JalviewJS_2112
[jalview.git] / src / jalview / ws / params / AutoCalcSetting.java
index 29aeef9..68ec7a6 100644 (file)
  */
 package jalview.ws.params;
 
+import jalview.util.MessageManager;
+import jalview.ws.api.ServiceWithParameters;
+
+import java.util.ArrayList;
 import java.util.List;
 
-public abstract class AutoCalcSetting
+public class AutoCalcSetting
 {
 
   protected boolean autoUpdate;
@@ -31,9 +35,13 @@ public abstract class AutoCalcSetting
 
   protected List<ArgumentI> jobArgset;
 
-  public AutoCalcSetting(WsParamSetI preset2, List<ArgumentI> jobArgset2,
+  protected ServiceWithParameters service;
+
+  public AutoCalcSetting(ServiceWithParameters service2,
+          WsParamSetI preset2, List<ArgumentI> jobArgset2,
           boolean autoUpdate2)
   {
+    service = service2;
     autoUpdate = autoUpdate2;
     preset = preset2;
     jobArgset = jobArgset2;
@@ -66,25 +74,117 @@ public abstract class AutoCalcSetting
   }
 
   /**
+   * TODO: refactor to ServiceWithParameters ?
    * 
    * @return characteristic URI for this service. The URI should reflect the
    *         type and version of this service, enabling the service client code
    *         to recover the correct client for this calculation.
    */
-  public abstract String getServiceURI();
+  public String getServiceURI()
+  {
+    return service.getNameURI();
+  }
 
   /**
+   * TODO: refactor to ServiceWithParameters ?
+   * 
    * return any concrete service endpoints associated with this calculation.
    * built in services should return a zero length array
    * 
    * @return
    */
-  public abstract String[] getServiceURLs();
+  public String[] getServiceURLs()
+  {
+    return new String[] { service.getUri() };
+  }
 
   /**
+   * default WsParamFile generator method - clients with custom formats should
+   * override and implement their own
    * 
    * @return stringified representation of the parameters for this setting
    */
-  public abstract String getWsParamFile();
+  public String getWsParamFile()
+  {
+    List<ArgumentI> opts = null;
+    if (jobArgset != null)
+    {
+      opts = jobArgset;
+    }
+    else
+    {
+      if (preset != null)
+      {
+        opts = preset.getArguments();
+      }
+    }
+    if (opts == null || opts.size() == 0)
+    {
+      return "";
+    }
+    StringBuffer pset = new StringBuffer();
+    for (ArgumentI ps : opts)
+    {
+      pset.append(ps.getName() + "\t" + ps.getValue());
+      pset.append("\n");
+    }
+    return pset.toString();
+  }
+  public ServiceWithParameters getService()
+  {
+    return service;
+  }
+
+  public void setService(ServiceWithParameters service)
+  {
+    this.service = service;
+    if (preset != null)
+    {
+      // check if we need to migrate preset to a new service URL
+      for (String url : preset.getApplicableUrls())
+      {
+        if (url.equals(service.getUri()))
+        {
+          // preset already verified
+          return;
+        }
+      }
+      WsParamSetI pr = service.getParamStore().getPreset(preset.getName());
+  
+      // TODO: decide of this distinction between preset and args are needed.
+      //
+      // if (pr instanceof JabaPreset && preset instanceof JabaPreset)
+      // {
+      // // easy - Presets are identical (we assume)
+      // preset = pr;
+      // return;
+      // }
+  
+      // this verifies that all arguments in the existing preset are the same as
+      // the parameters for the preset provided by the service parameter store.
+      // ie the LastUsed settings or a predefined preset.
+  
+      List<ArgumentI> oldargs = new ArrayList<>(),
+              newargs = new ArrayList<>();
+      oldargs.addAll(preset.getArguments());
+      // need to compare parameters
+      for (ArgumentI newparg : pr.getArguments())
+      {
+        if (!oldargs.remove(newparg))
+        {
+          newargs.add(newparg);
+        }
+      }
+      if (oldargs.size() == 0 && newargs.size() == 0)
+      {
+        // exact match.
+        preset = pr;
+        return;
+      }
+      // Try even harder to migrate arguments.
+      throw new Error(MessageManager
+              .getString("error.parameter_migration_not_implemented_yet"));
+    }
+  }
 
 }