JAL-3066 Create labels for fields and hack it to work with multiple valued parameters
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 11 Oct 2019 16:20:21 +0000 (17:20 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 11 Oct 2019 16:20:21 +0000 (17:20 +0100)
src/jalview/gui/OptsAndParamsPage.java
src/jalview/ws/params/ArgumentI.java
src/jalview/ws/params/simple/BooleanOption.java
src/jalview/ws/params/simple/Option.java
src/jalview/ws/slivkaws/SlivkaAnnotationServiceInstance.java
src/jalview/ws/slivkaws/SlivkaMsaServiceInstance.java
src/jalview/ws/slivkaws/SlivkaParamSet.java
src/jalview/ws/slivkaws/SlivkaWSInstance.java

index d66ec1c..046eb81 100644 (file)
@@ -137,7 +137,7 @@ public class OptsAndParamsPage
     {
       option = opt;
       setLayout(new FlowLayout(FlowLayout.LEFT));
-      enabled = new JCheckBox(opt.getName());
+      enabled = new JCheckBox(opt.getLabel());
       enabled.setSelected(opt.isRequired());
 
       /*
@@ -147,7 +147,7 @@ public class OptsAndParamsPage
       if (opt.isRequired() && !(opt instanceof JabaOption))
       {
         finfo = null;
-        add(new JLabel(opt.getName()));
+        add(new JLabel(opt.getLabel()));
       }
       else
       {
index 1554a7b..a5cb473 100644 (file)
@@ -30,6 +30,15 @@ public interface ArgumentI
 
   /**
    * 
+   * @return display name of this argument
+   */
+  default String getLabel()
+  {
+    return getName();
+  };
+
+  /**
+   * 
    * @return current value for the argument (may equal the name)
    */
   String getValue();
index 650b72d..df17296 100644 (file)
@@ -29,8 +29,22 @@ public class BooleanOption extends Option
   public BooleanOption(String name, String descr, boolean required,
       Boolean defVal, Boolean val, URL link)
   {
-    super(name, descr, required, (defVal != null && defVal ? name : null), (val != null && val ? name : null),
-            Arrays.asList(name), link);
+    super(name, descr, required, (defVal != null && defVal ? name : null),
+        (val != null && val ? name : null), Arrays.asList(name), link);
   }
 
+  public BooleanOption(String name, String description, String label,
+      boolean isrequired, Boolean defValue, String reprValue, URL link)
+  {
+    super(name, description, label, isrequired,
+        defValue != null && defValue ? reprValue : null,
+        defValue != null && defValue ? reprValue : null,
+        Arrays.asList(reprValue), link);
+  }
+
+  public BooleanOption(String name, String description, String label,
+      boolean isrequired, Boolean defValue, URL link)
+  {
+    this(name, description, label, isrequired, defValue, String.valueOf(true), link);
+  }
 }
index ce5d669..40e3804 100644 (file)
@@ -30,6 +30,8 @@ public class Option implements OptionI
 {
   String name;
 
+  String label;
+
   /*
    * current value in string format, or "null" if undefined
    */
@@ -61,6 +63,7 @@ public class Option implements OptionI
   public Option(Option opt)
   {
     name = opt.name;
+    label = opt.label;
     value = opt.value;
     defvalue = opt.defvalue;
     description = opt.description;
@@ -77,6 +80,13 @@ public class Option implements OptionI
   {
   }
 
+  public Option(String name, String description, String label, boolean isrequired,
+      String defValue, String val, List<String> possibleVals, URL fdetails)
+  {
+    this(name, description, isrequired, defValue, val, possibleVals, fdetails);
+    this.label = label;
+  }
+
   /**
    * Constructor including display names for possible values
    * 
@@ -150,6 +160,12 @@ public class Option implements OptionI
   }
 
   @Override
+  public String getLabel()
+  {
+    return label != null ? label : name;
+  }
+
+  @Override
   public String getValue()
   {
     return value == null ? defvalue : value;
index f49aa58..cc5c1e4 100644 (file)
@@ -16,19 +16,14 @@ import jalview.ws.params.ArgumentI;
 import jalview.ws.params.WsParamSetI;
 import jalview.ws.uimodel.AlignAnalysisUIText;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOError;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-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 SlivkaAnnotationServiceInstance extends SlivkaWSInstance implements SequenceAnnotationServiceI
@@ -54,39 +49,7 @@ public class SlivkaAnnotationServiceInstance extends SlivkaWSInstance implements
   @Override
   public JobId submitToService(List<SequenceI> seqs, WsParamSetI preset, List<ArgumentI> paramset) throws Throwable
   {
-    StringBuilder builder = new StringBuilder();
-    for (SequenceI seq : seqs)
-    {
-      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<>(paramset != null ? paramset.size() : 0);
-    if (paramset != null)
-    {
-      for (ArgumentI arg : paramset)
-      {
-        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());
+    return super.submit(seqs, preset, paramset);
   }
 
   @Override
index efeb582..a65e93e 100644 (file)
@@ -11,19 +11,13 @@ 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.List;
-import java.util.Optional;
 
-import uk.ac.dundee.compbio.slivkaclient.FieldType;
-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
@@ -36,37 +30,7 @@ public class SlivkaMsaServiceInstance extends SlivkaWSInstance implements Multip
   @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();
-    Optional<FormField> inputField = form.getFields().stream()
-        .filter(f -> f.getType() == FieldType.FILE).findFirst();
-    if (inputField.isPresent())
-    {
-      form.insert(inputField.get().getName(), file);
-    }
-    if (list != null)
-    {
-      for (ArgumentI arg : list)
-      {
-        FormField field = form.getField(arg.getName());
-        if (field.getType() == FieldType.BOOLEAN)
-        {
-          form.insert(arg.getName(), (arg.getValue() != null && !arg.getValue().isBlank()) ? true : false);
-        }
-        else
-        {
-          form.insert(arg.getName(), field.valueOf(arg.getValue()));
-        }
-      }
-    }
-    return new JobId(service.getName(), service.getName(), form.submit());
+    return super.submit(toalign, parameters, list);
   }
 
   @Override
index ed4dbd2..7e81684 100644 (file)
@@ -32,50 +32,63 @@ public class SlivkaParamSet implements WsParamSetI
     SlivkaForm form = service.getForm();
     for (FormField field : form.getFields())
     {
-      ArgumentI arg;
       switch (field.getType()) {
       case BOOLEAN:
         BooleanField boolField = (BooleanField) field;
-        arg = new BooleanOption(
-            field.getName(), field.getDescription(), field.isRequired(),
-            boolField.getDefault(), boolField.getDefault(), null
-        );
+        args.add(new BooleanOption(
+            field.getName(), field.getDescription(), field.getLabel(),
+            field.isRequired(), boolField.getDefault(), null
+        ));
         break;
       case TEXT:
         TextField textField = (TextField) field;
-        arg = new StringParameter(
+        args.add(new StringParameter(
             field.getName(), field.getDescription(), field.isRequired(),
             textField.getDefault(), textField.getDefault()
-        );
+        ));
         break;
       case INTEGER:
         IntegerField intField = (IntegerField) field;
-        arg = new IntegerParameter(
+        args.add(new IntegerParameter(
             field.getName(), field.getDescription(), field.isRequired(),
             intField.getDefault(), intField.getMin(), intField.getMax()
-        );
+        ));
         break;
       case DECIMAL:
         DecimalField doubleField = (DecimalField) field;
-        arg = new DoubleParameter(
+        args.add(new DoubleParameter(
             field.getName(), field.getDescription(), field.isRequired(),
             doubleField.getDefault(), doubleField.getMin(),
             doubleField.getMax()
-        );
+        ));
         break;
       case CHOICE:
         ChoiceField choiceField = (ChoiceField) field;
         List<String> choices = new ArrayList<>(choiceField.getChoices());
-        arg = new StringParameter(field.getName(), field.getDescription(),
-            field.isRequired(), choiceField.getDefault(), choiceField.getDefault(),
-            choices, choices
-        );
+        if (field.hasMultipleValues()) {
+          int counter = 0;
+          for (String choice : choices) {
+            args.add(new BooleanOption(
+                String.format("%s$%d", field.getName(), counter++),
+                field.getDescription(), choice, field.isRequired(),
+                choice.equals(choiceField.getDefault()), choice,
+                null
+            ));
+          }
+        }
+        else
+        {
+          args.add(new StringParameter(
+              field.getName(), field.getDescription(),
+              field.isRequired(), choiceField.getDefault(), choiceField.getDefault(),
+              choices, choices
+          ));
+        }
         break;
       case FILE:
       default:
         continue;
       }
-      args.add(arg);
     }
   }
 
index a5c809f..f86725b 100644 (file)
@@ -1,13 +1,18 @@
 package jalview.ws.slivkaws;
 
+import jalview.datamodel.SequenceI;
 import jalview.gui.WebserviceInfo;
 import jalview.ws.api.JalviewServiceEndpointProviderI;
 import jalview.ws.api.JalviewWebServiceI;
+import jalview.ws.api.JobId;
 import jalview.ws.api.ServiceWithParameters;
 import jalview.ws.gui.WsJob;
+import jalview.ws.params.ArgumentI;
 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;
@@ -19,10 +24,13 @@ import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 
+import uk.ac.dundee.compbio.slivkaclient.FieldType;
+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;
 
@@ -57,6 +65,48 @@ public abstract class SlivkaWSInstance extends ServiceWithParameters
     this.service = service;
   }
 
+  protected final JobId submit(List<SequenceI> sequences,
+          WsParamSetI preset, List<ArgumentI> args) throws Throwable
+  {
+    SlivkaForm form = service.getForm();
+    Optional<FormField> inputField = form.getFields().stream()
+            .filter(f -> f.getType() == FieldType.FILE).findFirst();
+    if (inputField.isPresent())
+    {
+      StringBuilder builder = new StringBuilder();
+      for (SequenceI seq : sequences)
+      {
+        builder.append(">").append(seq.getName()).append("\n")
+                .append(seq.getSequence()).append("\n");
+      }
+      InputStream stream = new ByteArrayInputStream(
+              builder.toString().getBytes());
+      RemoteFile file = client.uploadFile(stream, "input.fa",
+              "application/fasta");
+      form.insert(inputField.get().getName(), file);
+    }
+    if (args != null)
+    {
+      for (ArgumentI arg : args)
+      {
+        String fieldName = arg.getName().split("\\$", 2)[0];
+        FormField field = form.getField(fieldName);
+        if (field.getType() == FieldType.BOOLEAN)
+        {
+          form.insert(fieldName,
+                  (arg.getValue() != null && !arg.getValue().isBlank())
+                          ? true
+                          : false);
+        }
+        else
+        {
+          form.insert(fieldName, field.valueOf(arg.getValue()));
+        }
+      }
+    }
+    return new JobId(service.getName(), service.getName(), form.submit());
+  }
+
   @Override
   public final void updateStatus(WsJob job)
   {