JAL-2110 JAL-2131 JAL-1765 remove reference to jalview.datamodel.Alignment from Align...
[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(
176               WsURL));
177       ((SeqSearchServiceSoapBindingStub) this.server).setTimeout(60000); // One
178       // minute
179       // timeout
180     } catch (Exception ex)
181     {
182       wsInfo.setProgressText("Serious! " + WebServiceName
183               + " Service location failed\nfor URL :" + WsURL + "\n"
184               + ex.getMessage());
185       wsInfo.setStatus(WebserviceInfo.ERROR);
186       ex.printStackTrace();
187
188       return false;
189     }
190
191     loc.getEngine().setOption("axis", "1");
192
193     return true;
194   }
195
196   protected String getServiceActionKey()
197   {
198     return "SeqSearch";
199   }
200
201   protected String getServiceActionDescription()
202   {
203     return "Sequence Database Search";
204   }
205
206   // simple caching of db parameters for each service endpoint
207   private static Hashtable dbParamsForEndpoint;
208   static
209   {
210     dbParamsForEndpoint = new Hashtable();
211   }
212
213   public String[] getSupportedDatabases() throws Exception
214   {
215
216     // check that we haven't already been to this service endpoint
217     if (dbParamsForEndpoint.containsKey(WsURL))
218     {
219       return (String[]) dbParamsForEndpoint.get(WsURL);
220     }
221     if (!locateWebService())
222     {
223       throw new Exception(MessageManager.formatMessage(
224               "exception.cannot_contact_service_endpoint_at",
225               new String[] { WsURL }));
226     }
227     String database = server.getDatabase();
228     if (database == null)
229     {
230       dbParamsForEndpoint.put(WsURL, new String[] {});
231       return null;
232     }
233     StringTokenizer en = new StringTokenizer(database.trim(), ",| ");
234     String[] dbs = new String[en.countTokens()];
235     for (int i = 0; i < dbs.length; i++)
236     {
237       dbs[i++] = en.nextToken().trim();
238     }
239     dbParamsForEndpoint.put(WsURL, dbs);
240     return dbs;
241   }
242
243   @Override
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       @Override
285       public void actionPerformed(ActionEvent e)
286       {
287         // use same input gatherer as for secondary structure prediction
288         // we could actually parameterise the gatherer method here...
289         AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
290         new jalview.ws.jws1.SeqSearchWSClient(sh, af.getTitle(), msa, null,
291                 af.getViewport().getAlignment().getDataset(), af);
292       }
293     });
294     // add entry for each database the service supports
295     for (int db = 0; dbs != null && db < dbs.length; db++)
296     {
297       JMenu dbmenu = (JMenu) dbsrchs.get(dbs[db]);
298       if (dbmenu == null)
299       {
300         dbsrchs.put(dbs[db], dbmenu = new JMenu(dbs[db]));
301         newdbsrch.addElement(dbmenu);
302       }
303       // add the client handler code for this service
304       dbmenu.add(method = new JMenuItem(sh.getName()));
305       method.setToolTipText(sh.getEndpointURL());
306       final String searchdb = dbs[db];
307       method.addActionListener(new ActionListener()
308       {
309         @Override
310         public void actionPerformed(ActionEvent e)
311         {
312           AlignmentView msa = af.gatherSeqOrMsaForSecStrPrediction();
313           new jalview.ws.jws1.SeqSearchWSClient(sh, af.getTitle(), msa,
314                   searchdb, af.getViewport().getAlignment().getDataset(),
315                   af);
316         }
317       });
318     }
319     // add the databases onto the seqsearch menu
320     Enumeration e = newdbsrch.elements();
321     while (e.hasMoreElements())
322     {
323       Object el = e.nextElement();
324       if (el instanceof JMenu)
325       {
326         wsmenu.add((JMenu) el);
327       }
328       else
329       {
330         wsmenu.add((JMenuItem) el);
331       }
332     }
333
334   }
335 }