update author list in license for (JAL-826)
[jalview.git] / src / jalview / ws / jws2 / JabaParamStore.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.jws2;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Hashtable;
26 import java.util.List;
27 import java.util.StringTokenizer;
28
29 import compbio.metadata.Argument;
30 import compbio.metadata.Option;
31 import compbio.metadata.Parameter;
32 import compbio.metadata.Preset;
33 import compbio.metadata.PresetManager;
34 import compbio.metadata.RunnerConfig;
35
36 import jalview.ws.jws2.Jws2Discoverer.Jws2Instance;
37 import jalview.ws.jws2.dm.JabaOption;
38 import jalview.ws.jws2.dm.JabaParameter;
39 import jalview.ws.jws2.dm.JabaWsParamSet;
40 import jalview.ws.params.ArgumentI;
41 import jalview.ws.params.ParamDatastoreI;
42 import jalview.ws.params.ParamManager;
43 import jalview.ws.params.WsParamSetI;
44
45 public class JabaParamStore implements ParamDatastoreI
46 {
47
48   Hashtable<String, JabaWsParamSet> editedParams = new Hashtable<String, JabaWsParamSet>();
49
50   private Jws2Instance service;
51
52   private RunnerConfig serviceOptions;
53
54   private Hashtable<String, JabaPreset> servicePresets;
55
56   public JabaParamStore(Jws2Instance service)
57   {
58     this(service, null);
59   }
60
61   ParamManager manager;
62
63   public JabaParamStore(Jws2Instance service, ParamManager manager)
64   {
65     this.service = service;
66     serviceOptions = service.getRunnerConfig();
67     this.manager = manager;
68     // discover all the user's locally stored presets for this service and
69     // populate the hash table
70     if (manager != null)
71     {
72       manager.registerParser(service.getUri(), this);
73       WsParamSetI[] prams = manager.getParameterSet(null, service.getUri(),
74               true, false);
75       if (prams != null)
76       {
77         for (WsParamSetI paramset : prams)
78         {
79           if (paramset instanceof JabaWsParamSet)
80           {
81             editedParams.put(paramset.getName(), (JabaWsParamSet) paramset);
82           }
83           else
84           {
85             System.err
86                     .println("Warning: Ignoring parameter set instance of type "
87                             + paramset.getClass()
88                             + " : Bound but not applicable for service at "
89                             + service.getUri());
90           }
91         }
92       }
93     }
94   }
95
96   @Override
97   public List<WsParamSetI> getPresets()
98   {
99     List<WsParamSetI> prefs = new ArrayList();
100     if (servicePresets == null)
101     {
102       servicePresets = new Hashtable<String, JabaPreset>();
103       PresetManager prman;
104       if ((prman = service.getPresets()) != null)
105       {
106         List pset = prman.getPresets();
107         if (pset != null)
108         {
109           for (Object pr : pset)
110           {
111             JabaPreset prset = new JabaPreset(service, (Preset) pr);
112             servicePresets.put(prset.getName(), prset);
113           }
114         }
115       }
116     }
117     for (JabaPreset pr : servicePresets.values())
118     {
119       prefs.add(pr);
120     }
121     for (WsParamSetI wspset : editedParams.values())
122     {
123       prefs.add(wspset);
124     }
125     return prefs;
126   }
127
128   @Override
129   public WsParamSetI getPreset(String name)
130   {
131     for (WsParamSetI pr : getPresets())
132     {
133       if (pr.getName().equals(name))
134       {
135         return pr;
136       }
137     }
138     return null;
139   }
140
141   public static List<ArgumentI> getJwsArgsfromJaba(List jabargs)
142   {
143     List<ArgumentI> rgs = new ArrayList<ArgumentI>();
144     for (Object rg : jabargs)
145     {
146       ArgumentI narg = (rg instanceof Parameter) ? new JabaParameter(
147               (Parameter) rg) : (rg instanceof Option) ? new JabaOption(
148               (Option) rg) : null;
149       if (narg == null)
150       {
151         throw new Error(
152                 "Implementation Error: Cannot handle Jaba parameter object "
153                         + rg.getClass());
154       }
155       else
156       {
157         rgs.add(narg);
158       }
159     }
160     return rgs;
161   }
162
163   public static List getJabafromJwsArgs(List<ArgumentI> jwsargs)
164   {
165     List rgs = new ArrayList();
166     for (ArgumentI rg : jwsargs)
167     {
168       Argument narg = (rg instanceof JabaOption) ? ((JabaOption) rg)
169               .getOption() : null;
170       if (narg == null)
171       {
172         throw new Error(
173                 "Implementation Error: Cannot handle Jaba parameter object "
174                         + rg.getClass());
175       }
176       else
177       {
178         rgs.add(narg);
179       }
180     }
181     return rgs;
182   }
183
184   @Override
185   public List<ArgumentI> getServiceParameters()
186   {
187     return getJwsArgsfromJaba(serviceOptions.getArguments());
188   }
189
190   @Override
191   public boolean presetExists(String name)
192   {
193     return (editedParams.containsKey(name) || servicePresets
194             .containsKey(name));
195   }
196
197   @Override
198   public void deletePreset(String name)
199   {
200     if (editedParams.containsKey(name))
201     {
202       WsParamSetI parameterSet = editedParams.get(name);
203       editedParams.remove(name);
204       if (manager != null)
205       {
206         manager.deleteParameterSet(parameterSet);
207       }
208       return;
209     }
210     if (servicePresets.containsKey(name))
211     {
212       throw new Error(
213               "Implementation error: Attempt to delete a service preset!");
214     }
215   }
216
217   @Override
218   public void storePreset(String presetName, String text,
219           List<ArgumentI> jobParams)
220   {
221     JabaWsParamSet jps = new JabaWsParamSet(presetName, text, jobParams);
222     jps.setApplicableUrls(new String[]
223     { service.getUri() });
224     editedParams.put(jps.getName(), jps);
225     if (manager != null)
226     {
227       manager.storeParameterSet(jps);
228     }
229   }
230
231   @Override
232   public void updatePreset(String oldName, String presetName, String text,
233           List<ArgumentI> jobParams)
234   {
235     JabaWsParamSet jps = (JabaWsParamSet) ((oldName != null) ? getPreset(oldName)
236             : getPreset(presetName));
237     if (jps == null)
238     {
239       throw new Error("Implementation error: Can't locate either oldname ("
240               + oldName + ") or presetName (" + presetName
241               + "in the datastore!");
242     }
243     jps.setName(presetName);
244     jps.setDescription(text);
245     jps.setArguments(jobParams);
246     jps.setApplicableUrls(new String[]
247     { service.getUri() });
248     if (oldName != null && !oldName.equals(jps.getName()))
249     {
250       editedParams.remove(oldName);
251     }
252     editedParams.put(jps.getName(), jps);
253
254     if (manager != null)
255     {
256       manager.storeParameterSet(jps);
257     }
258   }
259
260   /**
261    * create a new, empty parameter set for this service
262    * 
263    * @return
264    */
265   WsParamSetI newWsParamSet()
266   {
267     return new JabaWsParamSet();
268   };
269
270   private boolean involves(String[] urls)
271   {
272     boolean found = false;
273     for (String url : urls)
274     {
275       if (service.getUri().equalsIgnoreCase(url))
276       {
277         found = true;
278         break;
279       }
280     }
281     return found;
282   }
283
284   @Override
285   public WsParamSetI parseServiceParameterFile(String name, String descr,
286           String[] urls, String parameterfile) throws IOException
287   {
288     if (!involves(urls))
289     {
290       throw new IOException(
291               "Implementation error: Cannot find service url in the given url set!");
292
293     }
294     JabaWsParamSet wsp = new JabaWsParamSet();
295     wsp.setName(name);
296     wsp.setDescription(descr);
297     wsp.setApplicableUrls(urls.clone());
298
299     List<String> lines = new ArrayList<String>();
300     StringTokenizer st = new StringTokenizer(parameterfile, "\n");
301     while (st.hasMoreTokens())
302     {
303       lines.add(st.nextToken());
304     }
305     wsp.setjabaArguments(ParameterUtils.processParameters(lines,
306             serviceOptions, " "));
307     return wsp;
308   }
309
310   @Override
311   public String generateServiceParameterFile(WsParamSetI pset)
312           throws IOException
313   {
314     if (!involves(pset.getApplicableUrls()))
315     {
316       throw new IOException(
317               "Implementation error: Cannot find service url in the given url set for this service parameter store ("
318                       + service.getUri() + ") !");
319
320     }
321     if (!(pset instanceof JabaWsParamSet))
322     {
323       throw new Error(
324               "Implementation error: JabaWsParamSets can only be handled by JabaParamStore");
325     }
326
327     StringBuffer rslt = new StringBuffer();
328     for (String ln : ParameterUtils.writeParameterSet(
329             ((JabaWsParamSet) pset).getjabaArguments(), " "))
330     {
331       rslt.append(ln);
332       rslt.append("\n");
333     }
334     ;
335     return rslt.toString();
336   }
337
338 }