33aea909e67225d72871fece3875091ba7913984
[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    * the root object for the service client
71    */
72   protected UIinfo serviceHandle;
73
74   /**
75    * total number of jobs managed by this web service client instance.
76    */
77   int jobsRunning = 0;
78
79   /**
80    * TODO: this is really service metadata, and should be moved elsewhere.
81    * mappings between abstract interface names and menu entries
82    */
83   protected java.util.Hashtable ServiceActions;
84
85   /**
86    * alignFrame associated with this client
87    */
88   protected AlignFrame alignFrame;
89   {
90     ServiceActions = new java.util.Hashtable();
91     ServiceActions.put("MsaWS", "Multiple Sequence Alignment");
92     ServiceActions.put("SecStrPred", "Secondary Structure Prediction");
93   };
94
95   /**
96    * The preset for the job executed by this client (may be null)
97    */
98   protected WsParamSetI preset;
99
100   /**
101    * The parameters for the job executed by this client (may be null)
102    */
103   protected List<ArgumentI> paramset;
104
105   public WSClient()
106   {
107   }
108
109   /**
110    * base constructor for a web service with parameters. Extending classes
111    * should implement this constructor with additional logic to verify that
112    * preset and arguments are compatible with the service being configured.
113    * 
114    * @param _alignFrame
115    * @param preset
116    * @param arguments
117    */
118   public WSClient(AlignFrame _alignFrame, WsParamSetI preset,
119           List<ArgumentI> arguments)
120   {
121     alignFrame = _alignFrame;
122     this.preset = preset;
123     this.paramset = arguments;
124   }
125
126   protected WebserviceInfo setWebService(UIinfo serv, boolean b)
127   {
128     WebServiceName = serv.getName();
129     WebServiceJobTitle = serv.getActionText();
130     WsURL = serv.getHostURL();
131     if (!b)
132     {
133       return new WebserviceInfo(WebServiceJobTitle,
134               WebServiceJobTitle + " using service hosted at "
135                       + WsURL + "\n"
136                       + (serv.getDescription() != null
137                               ? serv.getDescription()
138                               : ""),
139               false);
140     }
141     return null;
142   }
143
144   /**
145    * called to open a parameter editing dialog for parameterised services
146    * 
147    * @param sh
148    * @param editParams
149    * @return
150    */
151   protected boolean processParams(ServiceWithParameters sh,
152           boolean editParams)
153   {
154     return processParams(sh, editParams, false);
155   }
156
157   protected boolean processParams(ServiceWithParameters sh,
158           boolean editParams,
159           boolean adjustingExisting)
160   {
161
162     if (editParams)
163     {
164       // always do this
165       sh.initParamStore(Desktop.getUserParameterStore());
166
167       WsJobParameters jobParams = (preset == null && paramset != null
168               && paramset.size() > 0)
169                       ? new WsJobParameters((ParamDatastoreI) null, sh,
170                               (WsParamSetI) null, paramset)
171                       : new WsJobParameters((ParamDatastoreI) null, sh,
172                               preset, (List<ArgumentI>) null);
173       if (adjustingExisting)
174       {
175         jobParams.setName(MessageManager
176                 .getString("label.adjusting_parameters_for_calculation"));
177       }
178       if (!jobParams.showRunDialog())
179       {
180         return false; // dialog cancelled
181       }
182
183       WsParamSetI prset = jobParams.getPreset();
184       if (prset == null)
185       {
186         paramset = jobParams.isServiceDefaults() ? null
187                 : jobParams.getJobParams();
188         this.preset = null;
189       }
190       else
191       {
192         this.preset = prset; // ((JabaPreset) prset).p;
193         paramset = null; // no user supplied parameters.
194       }
195     }
196     return true;
197   }
198
199 }