JAL-629 Change all stdout and stderr output to use Console.outPrintln and Console...
[jalview.git] / src / jalview / ws / jws2 / JabaParamStore.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.jws2;
22
23 import jalview.util.MessageManager;
24 import jalview.ws.jws2.dm.JabaOption;
25 import jalview.ws.jws2.dm.JabaParameter;
26 import jalview.ws.jws2.dm.JabaWsParamSet;
27 import jalview.ws.jws2.jabaws2.Jws2Instance;
28 import jalview.ws.params.ArgumentI;
29 import jalview.ws.params.ParamDatastoreI;
30 import jalview.ws.params.ParamManager;
31 import jalview.ws.params.WsParamSetI;
32
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Hashtable;
37 import java.util.List;
38 import java.util.StringTokenizer;
39
40 import compbio.metadata.Argument;
41 import compbio.metadata.Option;
42 import compbio.metadata.Parameter;
43 import compbio.metadata.Preset;
44 import compbio.metadata.PresetManager;
45 import compbio.metadata.RunnerConfig;
46
47 public class JabaParamStore implements ParamDatastoreI
48 {
49
50   Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
51
52   private Jws2Instance service;
53
54   private RunnerConfig serviceOptions;
55
56   private Hashtable<String, JabaPreset> servicePresets;
57
58   public JabaParamStore(Jws2Instance service)
59   {
60     this(service, null);
61   }
62
63   ParamManager manager;
64
65   public JabaParamStore(Jws2Instance service, ParamManager manager)
66   {
67     this.service = service;
68     serviceOptions = service.getRunnerConfig();
69     this.manager = manager;
70     // discover all the user's locally stored presets for this service and
71     // populate the hash table
72     if (manager != null)
73     {
74       manager.registerParser(service.getUri(), this);
75       WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
76               true, false);
77       if (prams != null)
78       {
79         for (WsParamSetI paramset : prams)
80         {
81           if (paramset instanceof JabaWsParamSet)
82           {
83             editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
84           }
85           else
86           {
87             jalview.bin.Console.errPrintln(
88                     "Warning: Ignoring parameter set instance of type "
89                             + paramset.getClass()
90                             + " : Bound but not applicable for service at "
91                             + service.getUri());
92           }
93         }
94       }
95     }
96   }
97
98   @Override
99   public List<WsParamSetI> getPresets()
100   {
101     List<WsParamSetI> prefs = new ArrayList<>();
102     if (servicePresets == null)
103     {
104       servicePresets = new Hashtable<String, JabaPreset>();
105       PresetManager prman;
106       if ((prman = service.getPresets()) != null)
107       {
108         List pset = prman.getPresets();
109         if (pset != null)
110         {
111           for (Object pr : pset)
112           {
113             JabaPreset prset = new JabaPreset(service, (Preset) pr);
114             servicePresets.put(prset.getName(), prset);
115           }
116         }
117       }
118     }
119     for (JabaPreset pr : servicePresets.values())
120     {
121       prefs.add(pr);
122     }
123     for (WsParamSetI wspset : editedParams.values())
124     {
125       prefs.add(wspset);
126     }
127     return prefs;
128   }
129
130   @Override
131   public WsParamSetI getPreset(String name)
132   {
133     for (WsParamSetI pr : getPresets())
134     {
135       if (pr.getName().equals(name))
136       {
137         return pr;
138       }
139     }
140     return null;
141   }
142
143   public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
144   {
145     return getJwsArgsfromJaba(jabargs, true);
146   }
147
148   public static List<ArgumentI> getJwsArgsfromJaba(List jabargs,
149           boolean sortByOpt)
150   {
151     List<ArgumentI> rgs = new ArrayList<ArgumentI>();
152     List<String> rgnames = new ArrayList<String>();
153     for (Object rg : jabargs)
154     {
155       ArgumentI narg = null;
156       String nargstring = null;
157       if (rg instanceof Parameter)
158       {
159         narg = new JabaParameter((Parameter) rg);
160         nargstring = narg.getName(); // just sort by name for this
161       }
162       else if (rg instanceof Option)
163       {
164         narg = new JabaOption((Option) rg);
165         nargstring = (String) ((Option) rg).getOptionNames().get(0);
166       }
167       if (narg == null)
168       {
169         throw new Error(MessageManager.formatMessage(
170                 "error.implementation_error_cannot_handle_jaba_param",
171                 new String[]
172                 { rg.getClass().toString() }));
173       }
174       else
175       {
176         rgs.add(narg);
177         rgnames.add(nargstring);
178       }
179     }
180     if (!sortByOpt)
181     {
182       return rgs;
183     }
184     ArgumentI[] rgssort = rgs.toArray(new ArgumentI[rgs.size()]);
185     String[] rgssorton = rgnames.toArray(new String[rgs.size()]);
186     jalview.util.QuickSort.sort(rgssorton, rgssort);
187     ArgumentI tmp1;
188     int i = 0;
189     while (rgssort.length - i > i)
190     {
191       tmp1 = rgssort[rgssort.length - i - 1];
192       rgssort[rgssort.length - i - 1] = rgssort[i];
193       rgssort[i++] = tmp1;
194     }
195     return Arrays.asList(rgssort);
196   }
197
198   public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
199   {
200     List rgs = new ArrayList();
201     for (ArgumentI rg : jwsargs)
202     {
203       Argument narg = (rg instanceof JabaOption)
204               ? ((JabaOption) rg).getOption()
205               : null;
206       if (narg == null)
207       {
208         throw new Error(MessageManager.formatMessage(
209                 "error.implementation_error_cannot_handle_jaba_param",
210                 new String[]
211                 { rg.getClass().toString() }));
212       }
213       else
214       {
215         rgs.add(narg);
216       }
217     }
218     return rgs;
219   }
220
221   @Override
222   public List<ArgumentI> getServiceParameters()
223   {
224     return getJwsArgsfromJaba(serviceOptions.getArguments());
225   }
226
227   @Override
228   public boolean presetExists(String name)
229   {
230     return (editedParams.containsKey(name)
231             || servicePresets.containsKey(name));
232   }
233
234   @Override
235   public void deletePreset(String name)
236   {
237     if (editedParams.containsKey(name))
238     {
239       WsParamSetI parameterSet = editedParams.get(name);
240       editedParams.remove(name);
241       if (manager != null)
242       {
243         manager.deleteParameterSet(parameterSet);
244       }
245       return;
246     }
247     if (servicePresets.containsKey(name))
248     {
249       throw new Error(MessageManager.getString(
250               "error.implementation_error_attempt_to_delete_service_preset"));
251     }
252   }
253
254   @Override
255   public void storePreset(String presetName, String text,
256           List<ArgumentI> jobParams)
257   {
258     JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
259     jps.setApplicableUrls(new String[] { service.getUri() });
260     editedParams.put(jps.getName(), jps);
261     if (manager != null)
262     {
263       manager.storeParameterSet(jps);
264     }
265   }
266
267   @Override
268   public void updatePreset(String oldName, String presetName, String text,
269           List<ArgumentI> jobParams)
270   {
271     JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null)
272             ? getPreset(oldName)
273             : getPreset(presetName));
274     if (jps == null)
275     {
276       throw new Error(MessageManager.formatMessage(
277               "error.implementation_error_cannot_locate_oldname_presetname",
278               new String[]
279               { oldName, presetName }));
280     }
281     jps.setName(presetName);
282     jps.setDescription(text);
283     jps.setArguments(jobParams);
284     jps.setApplicableUrls(new String[] { service.getUri() });
285     if (oldName != null && !oldName.equals(jps.getName()))
286     {
287       editedParams.remove(oldName);
288     }
289     editedParams.put(jps.getName(), jps);
290
291     if (manager != null)
292     {
293       manager.storeParameterSet(jps);
294     }
295   }
296
297   /**
298    * create a new, empty parameter set for this service
299    * 
300    * @return
301    */
302   WsParamSetI newWsParamSet()
303   {
304     return new JabaWsParamSet();
305   };
306
307   private boolean involves(String[] urls)
308   {
309     boolean found = false;
310     for (String url : urls)
311     {
312       if (service.getServiceTypeURI().equals(url)
313               || service.getUri().equalsIgnoreCase(url))
314       {
315         found = true;
316         break;
317       }
318     }
319     return found;
320   }
321
322   @Override
323   public WsParamSetI parseServiceParameterFile(String name, String descr,
324           String[] urls, String parameterfile) throws IOException
325   {
326     if (!involves(urls))
327     {
328       throw new IOException(MessageManager.getString(
329               "error.implementation_error_cannot_find_service_url_in_given_set"));
330
331     }
332     JabaWsParamSet wsp = new JabaWsParamSet();
333     wsp.setName(name);
334     wsp.setDescription(descr);
335     wsp.setApplicableUrls(urls.clone());
336
337     List<String> lines = new ArrayList<String>();
338     StringTokenizer st = new StringTokenizer(parameterfile, "\n");
339     while (st.hasMoreTokens())
340     {
341       lines.add(st.nextToken());
342     }
343     wsp.setjabaArguments(
344             ParameterUtils.processParameters(lines, serviceOptions, " "));
345     return wsp;
346   }
347
348   @Override
349   public String generateServiceParameterFile(WsParamSetI pset)
350           throws IOException
351   {
352     if (!involves(pset.getApplicableUrls()))
353     {
354       throw new IOException(MessageManager.formatMessage(
355               "error.implementation_error_cannot_find_service_url_in_given_set_param_store",
356               new String[]
357               { service.getUri() }));
358     }
359     if (!(pset instanceof JabaWsParamSet))
360     {
361       throw new Error(MessageManager.getString(
362               "error.implementation_error_jabaws_param_set_only_handled_by"));
363     }
364
365     StringBuffer rslt = new StringBuffer();
366     for (String ln : ParameterUtils.writeParameterSet(
367             ((JabaWsParamSet) pset).getjabaArguments(), " "))
368     {
369       rslt.append(ln);
370       rslt.append("\n");
371     }
372     ;
373     return rslt.toString();
374   }
375
376 }