JAL-3066 Move common web service methods to abstract SlivkaWSInstance
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 23 Sep 2019 13:07:43 +0000 (14:07 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Mon, 23 Sep 2019 13:07:43 +0000 (14:07 +0100)
ServiceWithParameters and JalviewWebService overriden methods would be common to both Msa and Annotation services thus they are moved to the common abstract base class.

j11lib/slivka-client.jar
src/jalview/ws/slivkaws/SlivkaMsaServiceInstance.java [new file with mode: 0644]
src/jalview/ws/slivkaws/SlivkaWSDiscoverer.java
src/jalview/ws/slivkaws/SlivkaWSInstance.java

index 12cc028..c990dd8 100644 (file)
Binary files a/j11lib/slivka-client.jar and b/j11lib/slivka-client.jar differ
diff --git a/src/jalview/ws/slivkaws/SlivkaMsaServiceInstance.java b/src/jalview/ws/slivkaws/SlivkaMsaServiceInstance.java
new file mode 100644 (file)
index 0000000..726b556
--- /dev/null
@@ -0,0 +1,95 @@
+package jalview.ws.slivkaws;
+
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.FileFormat;
+import jalview.io.FormatAdapter;
+import jalview.ws.api.JobId;
+import jalview.ws.api.MultipleSequenceAlignmentI;
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.InvalidArgumentException;
+import jalview.ws.params.WsParamSetI;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOError;
+import java.io.IOException;
+import java.io.InputStream;
+import java.rmi.ServerError;
+import java.util.HashMap;
+import java.util.List;
+
+import uk.ac.dundee.compbio.slivkaclient.FormField;
+import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
+import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
+import uk.ac.dundee.compbio.slivkaclient.SlivkaForm;
+import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
+
+public class SlivkaMsaServiceInstance extends SlivkaWSInstance implements MultipleSequenceAlignmentI
+{
+  SlivkaMsaServiceInstance(SlivkaClient client, SlivkaService service) {
+    super(client, service, "Alignment");
+  }
+
+  @Override
+  public JobId align(List<SequenceI> toalign, WsParamSetI parameters, List<ArgumentI> list) throws Throwable
+  {
+    StringBuilder builder = new StringBuilder();
+    for (SequenceI seq : toalign)
+    {
+      builder.append(">").append(seq.getName()).append("\n");
+      builder.append(seq.getSequence()).append("\n");
+    }
+    InputStream stream = new ByteArrayInputStream(builder.toString().getBytes());
+    RemoteFile file = client.uploadFile(stream, "input.fasta", "application/fasta");
+    SlivkaForm form = service.getForm();
+    HashMap<String, String> values = new HashMap<>(list != null ? list.size() : 0);
+    if (list != null)
+    {
+      for (ArgumentI arg : list)
+      {
+        values.put(arg.getName(), arg.getValue());
+      }
+    }
+    for (FormField field : form.getFields())
+    {
+      switch (field.getType()) {
+      case FILE:
+        form.insert(field.getName(), file);
+        break;
+      case BOOLEAN:
+        String value = values.get(field.getName());
+        form.insert(field.getName(), (value != null && !value.isBlank()) ? true : false);
+        break;
+      default:
+        form.insert(field.getName(), field.valueOf(values.get(field.getName())));
+      }
+    }
+    return new JobId(service.getName(), service.getName(), form.submit());
+  }
+
+  @Override
+  public AlignmentI getAlignmentFor(JobId jobId) throws InvalidArgumentException, ServerError, IOError
+  {
+    List<RemoteFile> files;
+    try
+    {
+      files = client.getJobResults(jobId.getJobId());
+      for (RemoteFile f : files)
+      {
+        if (f.getMimeType().equals("application/clustal"))
+        {
+          return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Clustal);
+        }
+        else if (f.getMimeType().equals("application/fasta"))
+        {
+          return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Fasta);
+        }
+      }
+    } catch (IOException e)
+    {
+      throw new IOError(e);
+    }
+    return null;
+  }
+}
index 009e388..54cfbc4 100644 (file)
@@ -59,7 +59,7 @@ public class SlivkaWSDiscoverer
           }
           continue;
         }
-        SlivkaWSInstance instance = new SlivkaWSInstance(client, service);
+        SlivkaMsaServiceInstance instance = new SlivkaMsaServiceInstance(client, service);
         JMenuItem defaultEntry = new JMenuItem(String.format("%s with defaults", service.label));
         defaultEntry.addActionListener((ActionEvent evt) -> {
           AlignmentView msa = alignFrame.gatherSequencesForAlignment();
index 8bb554a..751637c 100644 (file)
@@ -1,48 +1,33 @@
 package jalview.ws.slivkaws;
 
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.SequenceI;
 import jalview.gui.WebserviceInfo;
-import jalview.io.DataSourceType;
-import jalview.io.FileFormat;
-import jalview.io.FormatAdapter;
 import jalview.ws.api.JalviewServiceEndpointProviderI;
-import jalview.ws.api.JobId;
-import jalview.ws.api.MultipleSequenceAlignmentI;
+import jalview.ws.api.JalviewWebServiceI;
 import jalview.ws.api.ServiceWithParameters;
 import jalview.ws.gui.WsJob;
-import jalview.ws.params.ArgumentI;
-import jalview.ws.params.InvalidArgumentException;
 import jalview.ws.params.ParamDatastoreI;
 import jalview.ws.params.ParamManager;
-import jalview.ws.params.WsParamSetI;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOError;
 import java.io.IOException;
-import java.io.InputStream;
-import java.rmi.ServerError;
 import java.util.EnumMap;
-import java.util.HashMap;
-import java.util.List;
 
-import uk.ac.dundee.compbio.slivkaclient.FormField;
 import uk.ac.dundee.compbio.slivkaclient.FormValidationException;
 import uk.ac.dundee.compbio.slivkaclient.JobState;
-import uk.ac.dundee.compbio.slivkaclient.RemoteFile;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaClient;
-import uk.ac.dundee.compbio.slivkaclient.SlivkaForm;
 import uk.ac.dundee.compbio.slivkaclient.SlivkaService;
 import uk.ac.dundee.compbio.slivkaclient.ValidationException;
 
-public class SlivkaWSInstance extends ServiceWithParameters
-    implements MultipleSequenceAlignmentI, JalviewServiceEndpointProviderI
+public abstract class SlivkaWSInstance extends ServiceWithParameters
+    implements JalviewServiceEndpointProviderI, JalviewWebServiceI
 {
-  private SlivkaClient client;
-  private SlivkaService service;
-  private SlivkaDatastore store = null;
+  protected final SlivkaClient client;
 
-  private EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
+  protected final SlivkaService service;
+
+  protected SlivkaDatastore store = null;
+
+  protected static final EnumMap<JobState, WsJob.JobState> stateMap = new EnumMap<>(JobState.class);
   {
     stateMap.put(JobState.PENDING, WsJob.JobState.QUEUED);
     stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED);
@@ -53,37 +38,33 @@ public class SlivkaWSInstance extends ServiceWithParameters
     stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN);
   }
 
-  SlivkaWSInstance(SlivkaClient client, SlivkaService service) {
-    super(service.getName(), "Alignment", service.getName(), "Slivka", client.getUrl().toString());
+  public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action)
+  {
+    super(service.getName(), action, service.getLabel(), "Slivka", client.getUrl().toString());
     this.client = client;
     this.service = service;
   }
 
   @Override
-  public void updateStatus(WsJob job)
+  public final void updateStatus(WsJob job)
   {
     try
     {
       job.setState(stateMap.get(client.getJobState(job.getJobId())));
     } catch (IOException e)
     {
-      throw new RuntimeException(e);
+      throw new IOError(e);
     }
   }
 
-  SlivkaService getService()
-  {
-    return service;
-  }
-
   @Override
-  public boolean updateJobProgress(WsJob job)
+  public final boolean updateJobProgress(WsJob job)
   {
     return false;
   }
 
   @Override
-  public boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
+  public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo)
   {
     if (_lex instanceof FormValidationException)
     {
@@ -102,81 +83,25 @@ public class SlivkaWSInstance extends ServiceWithParameters
   }
 
   @Override
-  public boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo)
+  public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo)
   {
+    // TODO
     return false;
   }
 
-  @Override
-  public JobId align(List<SequenceI> toalign, WsParamSetI parameters, List<ArgumentI> list) throws Throwable
-  {
-    StringBuilder builder = new StringBuilder();
-    for (SequenceI seq : toalign)
-    {
-      builder.append(">").append(seq.getName()).append("\n");
-      builder.append(seq.getSequence()).append("\n");
-    }
-    InputStream stream = new ByteArrayInputStream(builder.toString().getBytes());
-    RemoteFile file = client.uploadFile(stream, "input.fasta", "application/fasta");
-    SlivkaForm form = service.getForm();
-    HashMap<String, String> values = new HashMap<>(list != null ? list.size() : 0);
-    if (list != null)
-    {
-      for (ArgumentI arg : list)
-      {
-        values.put(arg.getName(), arg.getValue());
-      }
-    }
-    for (FormField field : form.getFields())
-    {
-      switch (field.getType()) {
-      case FILE:
-        form.insert(field.getName(), file);
-        break;
-      case BOOLEAN:
-        String value = values.get(field.getName());
-        form.insert(field.getName(), (value != null && !value.isBlank()) ? true : false);
-        break;
-      default:
-        form.insert(field.getName(), field.valueOf(values.get(field.getName())));
-      }
-    }
-    return new JobId(service.getName(), service.getName(), form.submit());
-  }
-
-  @Override
-  public AlignmentI getAlignmentFor(JobId jobId) throws InvalidArgumentException, ServerError, IOError
+  final SlivkaService getService()
   {
-    List<RemoteFile> files;
-    try
-    {
-      files = client.getJobResults(jobId.getJobId());
-      for (RemoteFile f : files)
-      {
-        if (f.getMimeType().equals("application/clustal"))
-        {
-          return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Clustal);
-        }
-        else if (f.getMimeType().equals("application/fasta"))
-        {
-          return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Fasta);
-        }
-      }
-    } catch (IOException e)
-    {
-      throw new IOError(e);
-    }
-    return null;
+    return service;
   }
 
   @Override
-  public Object getEndpoint()
+  public final Object getEndpoint()
   {
     return this;
   }
 
   @Override
-  public void initParamStore(ParamManager userParameterStore)
+  public final void initParamStore(ParamManager userParameterStore)
   {
     if (store == null)
     {
@@ -197,8 +122,9 @@ public class SlivkaWSInstance extends ServiceWithParameters
   }
 
   @Override
-  public ParamDatastoreI getParamStore()
+  public final ParamDatastoreI getParamStore()
   {
     return store;
   }
+
 }