JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ws / jws1 / SeqSearchWSClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.jws1;
22
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.Desktop;
27 import jalview.gui.WebserviceInfo;
28 import jalview.util.MessageManager;
29
30 import java.awt.Component;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.util.Enumeration;
34 import java.util.Hashtable;
35 import java.util.StringTokenizer;
36 import java.util.Vector;
37
38 import javax.swing.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.JOptionPane;
41
42 import ext.vamsas.SeqSearchI;
43 import ext.vamsas.SeqSearchServiceLocator;
44 import ext.vamsas.SeqSearchServiceSoapBindingStub;
45 import ext.vamsas.ServiceHandle;
46
47 /**
48  * DOCUMENT ME!
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class SeqSearchWSClient extends WS1Client
54 {
55   /**
56    * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
57    */
58   ext.vamsas.SeqSearchI server;
59
60   AlignFrame alignFrame;
61
62   /**
63    * Creates a new MsaWSClient object that uses a service given by an externally
64    * retrieved ServiceHandle
65    * 
66    * @param sh
67    *          service handle of type AbstractName(MsaWS)
68    * @param altitle
69    *          DOCUMENT ME!
70    * @param msa
71    *          DOCUMENT ME!
72    * @param submitGaps
73    *          DOCUMENT ME!
74    * @param preserveOrder
75    *          DOCUMENT ME!
76    */
77
78   public SeqSearchWSClient(ext.vamsas.ServiceHandle sh, String altitle,
79           jalview.datamodel.AlignmentView msa, String db,
80           Alignment seqdataset, AlignFrame _alignFrame)
81   {
82     super();
83     alignFrame = _alignFrame;
84     // can generalise the two errors below for metadata mapping from interface
85     // name to service client name
86     if (!sh.getAbstractName().equals(this.getServiceActionKey()))
87     {
88       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
89               .formatMessage(
90                       "label.service_called_is_not_seq_search_service",
91                       new String[] { sh.getName() }), MessageManager
92               .getString("label.internal_jalview_error"),
93               JOptionPane.WARNING_MESSAGE);
94
95       return;
96     }
97
98     if ((wsInfo = setWebService(sh)) == null)
99     {
100       JOptionPane.showMessageDialog(Desktop.desktop, MessageManager
101               .formatMessage("label.seq_search_service_is_unknown",
102                       new String[] { sh.getName() }), MessageManager
103               .getString("label.internal_jalview_error"),
104               JOptionPane.WARNING_MESSAGE);
105
106       return;
107     }
108     startSeqSearchClient(altitle, msa, db, seqdataset);
109
110   }
111
112   /**
113    * non-process web service interaction - use this for calling HEADLESS
114    * synchronous service methods
115    * 
116    * @param sh
117    */
118   public SeqSearchWSClient(ServiceHandle sh)
119   {
120     setWebService(sh, true);
121   }
122
123   public SeqSearchWSClient()
124   {
125
126     super();
127     // add a class reference to the list
128   }
129
130   private void startSeqSearchClient(String altitle, AlignmentView msa,
131           String db, Alignment seqdataset)
132   {
133     if (!locateWebService())
134     {
135       return;
136     }
137     String visdb = (db == null || db == "") ? "default" : db; // need a visible
138     // name for a
139     // sequence db
140     boolean profileSearch = msa.getSequences().length > 2 ? true : false;
141     // single sequence or profile from alignment view
142     wsInfo.setProgressText("Searching "
143             + visdb
144             + (!profileSearch ? " with sequence "
145                     + msa.getSequences()[0].getRefSeq().getName()
146                     : " with profile") + " from " + altitle
147             + "\nJob details\n");
148
149     String jobtitle = WebServiceName
150             + ((WebServiceName.indexOf("earch") > -1) ? " " : " search ")
151             + " of "
152             + visdb
153             + (!profileSearch ? " with sequence "
154                     + msa.getSequences()[0].getRefSeq().getName()
155                     : " with profile") + " from " + altitle;
156     SeqSearchWSThread ssthread = new SeqSearchWSThread(server, WsURL,
157             wsInfo, alignFrame, WebServiceName, jobtitle, msa, db,
158             seqdataset);
159     wsInfo.setthisService(ssthread);
160     ssthread.start();
161   }
162
163   /**
164    * Initializes the server field with a valid service implementation.
165    * 
166    * @return true if service was located.
167    */
168   private boolean locateWebService()
169   {
170     // this can be abstracted using reflection
171     // TODO: MuscleWS transmuted to generic MsaWS client
172     SeqSearchServiceLocator loc = new SeqSearchServiceLocator(); // Default
173
174     try
175     {
176       this.server = (SeqSearchI) loc.getSeqSearchService(new java.net.URL(
177               WsURL));
178       ((SeqSearchServiceSoapBindingStub) this.server).setTimeout(60000); // One
179       // minute
180       // timeout
181     } catch (Exception ex)
182     {
183       wsInfo.setProgressText("Serious! " + WebServiceName
184               + " Service location failed\nfor URL :" + WsURL + "\n"
185               + ex.getMessage());
186       wsInfo.setStatus(WebserviceInfo.ERROR);
187       ex.printStackTrace();
188
189       return false;
190     }
191
192     loc.getEngine().setOption("axis", "1");
193
194     return true;
195   }
196
197   protected String getServiceActionKey()
198   {
199     return "SeqSearch";
200   }
201
202   protected String getServiceActionDescription()
203   {
204     return "Sequence Database Search";
205   }
206
207   // simple caching of db parameters for each service endpoint
208   private static Hashtable dbParamsForEndpoint;
209   static
210   {
211     dbParamsForEndpoint = new Hashtable();
212   }
213
214   public String[] getSupportedDatabases() throws Exception
215   {
216
217     // check that we haven't already been to this service endpoint
218     if (dbParamsForEndpoint.containsKey(WsURL))
219     {
220       return (String[]) dbParamsForEndpoint.get(WsURL);
221     }
222     if (!locateWebService())
223     {
224       throw new Exception(MessageManager.formatMessage(
225               "exception.cannot_contact_service_endpoint_at",
226               new String[] { WsURL }));
227     }
228     String database = server.getDatabase();
229     if (database == null)
230     {
231       dbParamsForEndpoint.put(WsURL, new String[] {});
232       return null;
233     }
234     StringTokenizer en = new StringTokenizer(database.trim(), ",| ");
235     String[] dbs = new String[en.countTokens()];
236     for (int i = 0; i < dbs.length; i++)
237     {
238       dbs[i++] = en.nextToken().trim();
239     }
240     dbParamsForEndpoint.put(WsURL, dbs);
241     return dbs;
242   }
243
244   public void attachWSMenuEntry(JMenu wsmenu, final ServiceHandle sh,
245           final AlignFrame af)
246   {
247     // look for existing database service submenus on wsmenu
248     Hashtable dbsrchs = new Hashtable();
249     Vector newdbsrch = new Vector();
250     Component entries[] = wsmenu.getComponents();
251     for (int i = 0; entries != null && i < entries.length; i++)
252     {
253       if (entries[i] instanceof JMenu)
254       {
255         dbsrchs.put(entries[i].getName(), entries[i]);
256       }
257     }
258     JMenu defmenu = (JMenu) dbsrchs.get("Default Database");
259     if (defmenu == null)
260     {
261       dbsrchs.put("Default Database", defmenu = new JMenu(
262               "Default Database"));
263       newdbsrch.addElement(defmenu);
264     }
265
266     String dbs[] = null;
267     try
268     {
269       dbs = new jalview.ws.jws1.SeqSearchWSClient(sh)
270               .getSupportedDatabases();
271     } catch (Exception e)
272     {
273       jalview.bin.Cache.log.warn(
274               "Database list request failed, so disabling SeqSearch Service client "
275                       + sh.getName() + " at " + sh.getEndpointURL(), e);
276       return;
277     }
278     JMenuItem method;
279     // do default entry
280     defmenu.add(method = new JMenuItem(sh.getName()));
281     method.setToolTipText(sh.getEndpointURL());
282     method.addActionListener(new ActionListener()
283     {
284       public void actionPerformed(ActionEvent e)
285       {
286         // use same input gatherer as for secondary structure prediction
287         // we could actually parameterise the gatherer method here...
288         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
289         new jalview.ws.jws1.SeqSearchWSClient(sh, af.getTitle(), msa, null,
290                 af.getViewport().getAlignment().getDataset(), af);
291       }
292     });
293     // add entry for each database the service supports
294     for (int db = 0; dbs != null && db < dbs.length; db++)
295     {
296       JMenu dbmenu = (JMenu) dbsrchs.get(dbs[db]);
297       if (dbmenu == null)
298       {
299         dbsrchs.put(dbs[db], dbmenu = new JMenu(dbs[db]));
300         newdbsrch.addElement(dbmenu);
301       }
302       // add the client handler code for this service
303       dbmenu.add(method = new JMenuItem(sh.getName()));
304       method.setToolTipText(sh.getEndpointURL());
305       final String searchdb = dbs[db];
306       method.addActionListener(new ActionListener()
307       {
308         public void actionPerformed(ActionEvent e)
309         {
310           AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
311           new jalview.ws.jws1.SeqSearchWSClient(sh, af.getTitle(), msa,
312                   searchdb, af.getViewport().getAlignment().getDataset(),
313                   af);
314         }
315       });
316     }
317     // add the databases onto the seqsearch menu
318     Enumeration e = newdbsrch.elements();
319     while (e.hasMoreElements())
320     {
321       Object el = e.nextElement();
322       if (el instanceof JMenu)
323       {
324         wsmenu.add((JMenu) el);
325       }
326       else
327       {
328         wsmenu.add((JMenuItem) el);
329       }
330     }
331
332   }
333 }