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