a68f3f387cca8fcb13cde170ed1e43646d22c4a7
[jalview.git] / src / jalview / ws / WSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws;
22
23 import jalview.bin.Cache;
24 import jalview.gui.AlignFrame;
25 import jalview.gui.Desktop;
26 import jalview.gui.WebserviceInfo;
27 import jalview.gui.WsJobParameters;
28 import jalview.util.MessageManager;
29 import jalview.ws.api.ServiceWithParameters;
30 import jalview.ws.api.UIinfo;
31 import jalview.ws.params.ArgumentI;
32 import jalview.ws.params.ParamDatastoreI;
33 import jalview.ws.params.WsParamSetI;
34
35 import java.util.List;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.CompletionStage;
38
39 public abstract class WSClient // implements WSMenuEntryProviderI
40 {
41   /**
42    * WSClient holds the basic attributes that are displayed to the user for all
43    * jalview web service clients
44    */
45   /**
46    * displayed name for this web service
47    */
48   protected String WebServiceName;
49
50   /**
51    * specific job title (e.g. 'ClustalW Alignment of Selection from Aligment
52    * from Cut and Paste input')
53    */
54   protected String WebServiceJobTitle;
55
56   /**
57    * String giving additional information such as method citations for this
58    * service
59    */
60   protected String WebServiceReference;
61
62   /**
63    * Service endpoint
64    */
65   protected String WsURL;
66
67   /**
68    * Web service information used to initialise the WSClient attributes
69    */
70   protected WebserviceInfo wsInfo;
71
72   /**
73    * the root object for the service client
74    */
75   protected UIinfo serviceHandle;
76
77   /**
78    * total number of jobs managed by this web service client instance.
79    */
80   int jobsRunning = 0;
81
82   /**
83    * TODO: this is really service metadata, and should be moved elsewhere.
84    * mappings between abstract interface names and menu entries
85    */
86   protected java.util.Hashtable ServiceActions;
87
88   /**
89    * alignFrame associated with this client
90    */
91   protected AlignFrame alignFrame;
92   {
93     ServiceActions = new java.util.Hashtable();
94     ServiceActions.put("MsaWS", "Multiple Sequence Alignment");
95     ServiceActions.put("SecStrPred", "Secondary Structure Prediction");
96   };
97
98   /**
99    * The preset for the job executed by this client (may be null)
100    */
101   protected WsParamSetI preset;
102
103   /**
104    * The parameters for the job executed by this client (may be null)
105    */
106   protected List<ArgumentI> paramset;
107
108   public WSClient()
109   {
110   }
111
112   /**
113    * base constructor for a web service with parameters. Extending classes
114    * should implement this constructor with additional logic to verify that
115    * preset and arguments are compatible with the service being configured.
116    * 
117    * @param _alignFrame
118    * @param preset
119    * @param arguments
120    */
121   public WSClient(AlignFrame _alignFrame, WsParamSetI preset,
122       List<ArgumentI> arguments)
123   {
124     alignFrame = _alignFrame;
125     this.preset = preset;
126     this.paramset = arguments;
127   }
128
129   protected WebserviceInfo setWebService(UIinfo serv, boolean b)
130   {
131     WebServiceName = serv.getName();
132     WebServiceJobTitle = serv.getActionText();
133     WsURL = serv.getHostURL();
134     if (!b)
135     {
136       return new WebserviceInfo(WebServiceJobTitle,
137           WebServiceJobTitle + " using service hosted at "
138               + WsURL + "\n"
139               + (serv.getDescription() != null
140                   ? serv.getDescription()
141                   : ""),
142           false);
143     }
144     return null;
145   }
146
147   /**
148    * called to open a parameter editing dialog for parameterised services
149    * 
150    * @param sh
151    * @param editParams
152    * @return
153    */
154   protected CompletionStage<Boolean> processParams(ServiceWithParameters sh,
155       boolean editParams)
156   {
157     return processParams(sh, editParams, false);
158   }
159
160   protected CompletionStage<Boolean> processParams(ServiceWithParameters sh,
161       boolean editParams, boolean adjustingExisting)
162   {
163
164     if (editParams)
165     {
166       // always do this
167       sh.initParamStore(Desktop.getUserParameterStore());
168
169       WsJobParameters jobParams = (preset == null && paramset != null
170           && paramset.size() > 0)
171               ? new WsJobParameters((ParamDatastoreI) null, sh,
172                   (WsParamSetI) null, paramset)
173               : new WsJobParameters((ParamDatastoreI) null, sh,
174                   preset, (List<ArgumentI>) null);
175       if (adjustingExisting)
176       {
177         jobParams.setName(MessageManager
178             .getString("label.adjusting_parameters_for_calculation"));
179       }
180       var stage = jobParams.showRunDialog();
181       return stage.thenApply((startJob) -> {
182         if (startJob)
183         {
184           WsParamSetI prset = jobParams.getPreset();
185           if (prset == null)
186           {
187             paramset = jobParams.isServiceDefaults() ? null
188                 : jobParams.getJobParams();
189             this.preset = null;
190           }
191           else
192           {
193             this.preset = prset; // ((JabaPreset) prset).p;
194             paramset = null; // no user supplied parameters.
195           }
196         }
197         return startJob;
198       });
199
200     }
201     return CompletableFuture.completedFuture(true);
202   }
203
204 }