d0311423ebc5a7d785809a64a9bcdc49d64c3c7a
[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.gui.AlignFrame;
24 import jalview.gui.Desktop;
25 import jalview.gui.WebserviceInfo;
26 import jalview.gui.WsJobParameters;
27 import jalview.util.MessageManager;
28 import jalview.ws.api.ServiceWithParameters;
29 import jalview.ws.api.UIinfo;
30 import jalview.ws.params.ArgumentI;
31 import jalview.ws.params.ParamDatastoreI;
32 import jalview.ws.params.WsParamSetI;
33
34 import java.util.List;
35
36 public abstract class WSClient // implements WSMenuEntryProviderI
37 {
38   /**
39    * WSClient holds the basic attributes that are displayed to the user for all
40    * jalview web service clients
41    */
42   /**
43    * displayed name for this web service
44    */
45   protected String WebServiceName;
46
47   /**
48    * specific job title (e.g. 'ClustalW Alignment of Selection from Aligment
49    * from Cut and Paste input')
50    */
51   protected String WebServiceJobTitle;
52
53   /**
54    * String giving additional information such as method citations for this
55    * service
56    */
57   protected String WebServiceReference;
58
59   /**
60    * Service endpoint
61    */
62   protected String WsURL;
63
64   /**
65    * Web service information used to initialise the WSClient attributes
66    */
67   protected WebserviceInfo wsInfo;
68
69   /**
70    * total number of jobs managed by this web service client instance.
71    */
72   int jobsRunning = 0;
73
74   /**
75    * TODO: this is really service metadata, and should be moved elsewhere.
76    * mappings between abstract interface names and menu entries
77    */
78   protected java.util.Hashtable ServiceActions;
79
80   /**
81    * alignFrame associated with this client
82    */
83   protected AlignFrame alignFrame;
84   {
85     ServiceActions = new java.util.Hashtable();
86     ServiceActions.put("MsaWS", "Multiple Sequence Alignment");
87     ServiceActions.put("SecStrPred", "Secondary Structure Prediction");
88   };
89
90   /**
91    * The preset for the job executed by this client (may be null)
92    */
93   protected WsParamSetI preset;
94
95   /**
96    * The parameters for the job executed by this client (may be null)
97    */
98   protected List<ArgumentI> paramset;
99
100   public WSClient()
101   {
102   }
103
104   /**
105    * base constructor for a web service with parameters. Extending classes
106    * should implement this constructor with additional logic to verify that
107    * preset and arguments are compatible with the service being configured.
108    * 
109    * @param _alignFrame
110    * @param preset
111    * @param arguments
112    */
113   public WSClient(AlignFrame _alignFrame, WsParamSetI preset,
114           List<ArgumentI> arguments)
115   {
116     alignFrame = _alignFrame;
117     this.preset = preset;
118     this.paramset = arguments;
119   }
120
121   protected WebserviceInfo setWebService(UIinfo serv, boolean b)
122   {
123     WebServiceName = serv.getName();
124     WebServiceJobTitle = serv.getActionText();
125     WsURL = serv.getHostURL();
126     if (!b)
127     {
128       return new WebserviceInfo(WebServiceJobTitle,
129               WebServiceJobTitle + " using service hosted at "
130                       + WsURL + "\n"
131                       + (serv.getDescription() != null
132                               ? serv.getDescription()
133                               : ""),
134               false);
135     }
136     return null;
137   }
138
139   /**
140    * called to open a parameter editing dialog for parameterised services
141    * 
142    * @param sh
143    * @param editParams
144    * @return
145    */
146   protected boolean processParams(ServiceWithParameters sh,
147           boolean editParams)
148   {
149     return processParams(sh, editParams, false);
150   }
151
152   protected boolean processParams(ServiceWithParameters sh,
153           boolean editParams,
154           boolean adjustingExisting)
155   {
156
157     if (editParams)
158     {
159       // always do this
160       sh.initParamStore(Desktop.getUserParameterStore());
161
162       WsJobParameters jobParams = (preset == null && paramset != null
163               && paramset.size() > 0)
164                       ? new WsJobParameters((ParamDatastoreI) null, sh,
165                               (WsParamSetI) null, paramset)
166                       : new WsJobParameters((ParamDatastoreI) null, sh,
167                               preset, (List<ArgumentI>) null);
168       if (adjustingExisting)
169       {
170         jobParams.setName(MessageManager
171                 .getString("label.adjusting_parameters_for_calculation"));
172       }
173       if (!jobParams.showRunDialog())
174       {
175         return false; // dialog cancelled
176       }
177
178       WsParamSetI prset = jobParams.getPreset();
179       if (prset == null)
180       {
181         paramset = jobParams.isServiceDefaults() ? null
182                 : jobParams.getJobParams();
183         this.preset = null;
184       }
185       else
186       {
187         this.preset = prset; // ((JabaPreset) prset).p;
188         paramset = null; // no user supplied parameters.
189       }
190     }
191     return true;
192   }
193
194 }