JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[jalview.git] / src / jalview / gui / WsParamSetManager.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.gui;
22
23 import jalview.bin.Cache;
24 import jalview.io.JalviewFileChooser;
25 import jalview.io.JalviewFileView;
26 import jalview.util.MessageManager;
27 import jalview.ws.params.ParamDatastoreI;
28 import jalview.ws.params.ParamManager;
29 import jalview.ws.params.WsParamSetI;
30 import jalview.xml.binding.jalview.ObjectFactory;
31 import jalview.xml.binding.jalview.WebServiceParameterSet;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.io.OutputStreamWriter;
39 import java.io.PrintWriter;
40 import java.util.ArrayList;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.StringTokenizer;
44
45 import javax.xml.bind.JAXBContext;
46 import javax.xml.bind.JAXBElement;
47 import javax.xml.bind.Marshaller;
48 import javax.xml.stream.XMLInputFactory;
49 import javax.xml.stream.XMLStreamReader;
50
51 /**
52  * store and retrieve web service parameter sets.
53  * 
54  * @author JimP
55  * 
56  */
57 public class WsParamSetManager implements ParamManager
58 {
59   Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<>();
60
61   @Override
62   public WsParamSetI[] getParameterSet(String name, String serviceUrl,
63           boolean modifiable, boolean unmodifiable)
64   {
65     String files = Cache.getProperty("WS_PARAM_FILES");
66     if (files == null)
67     {
68       return null;
69     }
70     StringTokenizer st = new StringTokenizer(files, "|");
71     String pfile = null;
72     List<WsParamSetI> params = new ArrayList<>();
73     while (st.hasMoreTokens())
74     {
75       pfile = st.nextToken();
76       try
77       {
78         WsParamSetI[] pset = parseParamFile(pfile);
79         for (WsParamSetI p : pset)
80         {
81           boolean add = false;
82           if (serviceUrl != null)
83           {
84             for (String url : p.getApplicableUrls())
85             {
86               if (url.equals(serviceUrl))
87               {
88                 add = true;
89               }
90             }
91           }
92           else
93           {
94             add = true;
95           }
96           add &= (modifiable == p.isModifiable()
97                   || unmodifiable == !p.isModifiable());
98           add &= name == null || p.getName().equals(name);
99
100           if (add)
101           {
102
103             params.add(p);
104           }
105
106         }
107       } catch (IOException e)
108       {
109         Cache.info("Failed to parse parameter file " + pfile
110                 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
111                 e);
112       }
113     }
114     return params.toArray(new WsParamSetI[0]);
115   }
116
117   private WsParamSetI[] parseParamFile(String filename) throws IOException
118   {
119     List<WsParamSetI> psets = new ArrayList<>();
120     InputStreamReader is = new InputStreamReader(
121             new FileInputStream(new File(filename)), "UTF-8");
122
123     WebServiceParameterSet wspset = null;
124     try
125     {
126       JAXBContext jc = JAXBContext
127               .newInstance("jalview.xml.binding.jalview");
128       javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
129       XMLStreamReader streamReader = XMLInputFactory.newInstance()
130               .createXMLStreamReader(is);
131       JAXBElement<WebServiceParameterSet> jbe = um.unmarshal(streamReader,
132               WebServiceParameterSet.class);
133       wspset = jbe.getValue();
134     } catch (Exception ex)
135     {
136       throw new IOException(ex);
137     }
138
139     if (wspset != null && wspset.getParameters().length() > 0)
140     {
141       List<String> urls = wspset.getServiceURL();
142       final String[] urlArray = urls.toArray(new String[urls.size()]);
143
144       for (String url : urls)
145       {
146         ParamDatastoreI parser = paramparsers.get(url);
147         if (parser != null)
148         {
149           WsParamSetI pset = parser.parseServiceParameterFile(
150                   wspset.getName(), wspset.getDescription(),
151                   urlArray, wspset.getParameters());
152           if (pset != null)
153           {
154             pset.setSourceFile(filename);
155             psets.add(pset);
156             break;
157           }
158         }
159       }
160     }
161
162     return psets.toArray(new WsParamSetI[0]);
163   }
164
165   @Override
166   public void storeParameterSet(WsParamSetI parameterSet)
167   {
168     String filename = parameterSet.getSourceFile();
169     File outfile = null;
170     try
171     {
172       if (filename != null && !((outfile = new File(filename)).canWrite()))
173       {
174         Cache.warn("Can't write to " + filename
175                 + " - Prompting for new file to write to.");
176         filename = null;
177       }
178     } catch (Exception e)
179     {
180       filename = null;
181     }
182
183     ParamDatastoreI parser = null;
184     for (String urls : parameterSet.getApplicableUrls())
185     {
186       if (parser == null)
187       {
188         parser = paramparsers.get(urls);
189       }
190     }
191     if (parser == null)
192     {
193       throw new Error(MessageManager.getString(
194               "error.implementation_error_cannot_find_marshaller_for_param_set"));
195     }
196     if (filename == null)
197     {
198       // TODO: JAL-3048 webservice - not required for Jalview-JS
199
200       JalviewFileChooser chooser = new JalviewFileChooser("wsparams",
201               "Web Service Parameter File");
202       chooser.setFileView(new JalviewFileView());
203       chooser.setDialogTitle(MessageManager
204               .getString("label.choose_filename_for_param_file"));
205       chooser.setToolTipText(MessageManager.getString("action.save"));
206       int value = chooser.showSaveDialog(Desktop.instance);
207       if (value == JalviewFileChooser.APPROVE_OPTION)
208       {
209         outfile = chooser.getSelectedFile();
210         Cache.setProperty("LAST_DIRECTORY", outfile.getParent());
211         filename = outfile.getAbsolutePath();
212         if (!filename.endsWith(".wsparams"))
213         {
214           filename = filename.concat(".wsparams");
215           outfile = new File(filename);
216         }
217       }
218     }
219     if (outfile != null)
220     {
221       String paramFiles = Cache.getDefault("WS_PARAM_FILES",
222               filename);
223       if (paramFiles.indexOf(filename) == -1)
224       {
225         if (paramFiles.length() > 0)
226         {
227           paramFiles = paramFiles.concat("|");
228         }
229         paramFiles = paramFiles.concat(filename);
230       }
231       Cache.setProperty("WS_PARAM_FILES", paramFiles);
232
233       WebServiceParameterSet paramxml = new WebServiceParameterSet();
234
235       paramxml.setName(parameterSet.getName());
236       paramxml.setDescription(parameterSet.getDescription());
237       for (String url : parameterSet.getApplicableUrls())
238       {
239         paramxml.getServiceURL().add(url);
240       }
241       paramxml.setVersion("1.0");
242       try
243       {
244         paramxml.setParameters(
245                 parser.generateServiceParameterFile(parameterSet));
246         PrintWriter out = new PrintWriter(new OutputStreamWriter(
247                 new FileOutputStream(outfile), "UTF-8"));
248         JAXBContext jaxbContext = JAXBContext
249                 .newInstance(WebServiceParameterSet.class);
250         Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
251         jaxbMarshaller.marshal(
252                 new ObjectFactory().createWebServiceParameterSet(paramxml),
253                 out);
254         out.close();
255         parameterSet.setSourceFile(filename);
256       } catch (Exception e)
257       {
258         Cache.error("Couldn't write parameter file to " + outfile, e);
259       }
260     }
261   }
262
263   /*
264    * 
265    * JalviewFileChooser chooser = new JalviewFileChooser(Cache
266    * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
267    * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
268    * jalview.io.JalviewFileView());
269    * chooser.setDialogTitle("Load colour scheme");
270    * chooser.setToolTipText("Load");
271    * 
272    * int value = chooser.showOpenDialog(this);
273    * 
274    * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
275    * chooser.getSelectedFile(); Cache.setProperty("LAST_DIRECTORY",
276    * choice.getParent()); String defaultColours = Cache.getDefault(
277    * "USER_DEFINED_COLOURS", choice.getPath()); if
278    * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
279    * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
280    * 
281    * @see
282    * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
283    * )
284    */
285   @Override
286   public void deleteParameterSet(WsParamSetI parameterSet)
287   {
288     String filename = parameterSet.getSourceFile();
289     if (filename == null || filename.trim().length() < 1)
290     {
291       return;
292     }
293     String paramFiles = Cache.getDefault("WS_PARAM_FILES", "");
294     if (paramFiles.indexOf(filename) > -1)
295     {
296       String nparamFiles = new String();
297       StringTokenizer st = new StringTokenizer(paramFiles, "|");
298       while (st.hasMoreElements())
299       {
300         String fl = st.nextToken();
301         if (!fl.equals(filename))
302         {
303           nparamFiles = nparamFiles.concat("|").concat(fl);
304         }
305       }
306       Cache.setProperty("WS_PARAM_FILES", nparamFiles);
307     }
308
309     try
310     {
311       File pfile = new File(filename);
312       if (pfile.exists() && pfile.canWrite())
313       {
314         if (JvOptionPane.showConfirmDialog(Desktop.instance,
315                 "Delete the preset's file, too ?", "Delete User Preset ?",
316                 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
317         {
318           pfile.delete();
319         }
320       }
321     } catch (Exception e)
322     {
323       Cache.error(
324               "Exception when trying to delete webservice user preset: ",
325               e);
326     }
327   }
328
329   @Override
330   public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
331   {
332     paramparsers.put(hosturl, paramdataStore);
333   }
334
335 }