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