JAL-3032 Dialogs Color picker -- untested commit
[jalview.git] / src / jalview / ws / dbsources / das / datamodel / JalviewSource.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.dbsources.das.datamodel;
22
23 import jalview.util.MessageManager;
24 import jalview.ws.dbsources.das.api.jalviewSourceI;
25 import jalview.ws.seqfetcher.DbSourceProxy;
26
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.biodas.jdas.client.threads.MultipleConnectionPropertyProviderI;
35 import org.biodas.jdas.dassources.Capabilities;
36 import org.biodas.jdas.dassources.utils.DasTimeFormat;
37 import org.biodas.jdas.schema.sources.CAPABILITY;
38 import org.biodas.jdas.schema.sources.COORDINATES;
39 import org.biodas.jdas.schema.sources.MAINTAINER;
40 import org.biodas.jdas.schema.sources.PROP;
41 import org.biodas.jdas.schema.sources.SOURCE;
42 import org.biodas.jdas.schema.sources.VERSION;
43
44 public class JalviewSource implements jalviewSourceI
45 {
46   SOURCE source;
47
48   MultipleConnectionPropertyProviderI connprov;
49
50   public JalviewSource(SOURCE local2,
51           MultipleConnectionPropertyProviderI connprov, boolean local)
52   {
53     this.connprov = connprov;
54     this.local = local;
55     source = local2;
56   }
57
58   @Override
59   public String getTitle()
60   {
61     return source.getTitle();
62   }
63
64   @Override
65   public VERSION getVersion()
66   {
67
68     return getVersionFor(source);
69   }
70
71   @Override
72   public String getDocHref()
73   {
74     return source.getDocHref();
75   }
76
77   @Override
78   public String getDescription()
79   {
80     return source.getDescription();
81   }
82
83   @Override
84   public String getUri()
85   {
86     return source.getUri();
87   }
88
89   @Override
90   public MAINTAINER getMAINTAINER()
91   {
92     return source.getMAINTAINER();
93   }
94
95   @Override
96   public String getEmail()
97   {
98     return (local) ? null : source.getMAINTAINER().getEmail();
99   }
100
101   boolean local = false;
102
103   @Override
104   public boolean isLocal()
105   {
106     return local;
107   }
108
109   @Override
110   public boolean isSequenceSource()
111   {
112     String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
113     for (String cp : getCapabilityList(getVersionFor(source)))
114     {
115       if (cp.equals(seqcap))
116       {
117         return true;
118
119       }
120     }
121     return false;
122   }
123
124   @Override
125   public boolean isFeatureSource()
126   {
127     String seqcap = "das1:" + Capabilities.FEATURES.getName();
128     for (String cp : getCapabilityList(getVersionFor(source)))
129     {
130       if (cp.equals(seqcap))
131       {
132         return true;
133
134       }
135     }
136     return false;
137   }
138
139   private VERSION getVersionFor(SOURCE ds)
140   {
141     VERSION latest = null;
142     for (VERSION v : ds.getVERSION())
143     {
144       if (latest == null
145               || isLaterThan(latest.getCreated(), v.getCreated()))
146       {
147         // TODO: das 1.6 - should just get the first version - ignore other
148         // versions since not specified how to construct URL from version's URI
149         // + source URI
150         latest = v;
151       }
152     }
153     return latest;
154   }
155
156   /**
157    * compare date strings. null or unparseable dates are assumed to be oldest
158    * 
159    * @param ref
160    * @param newer
161    * @return true iff ref comes before newer
162    */
163   private boolean isLaterThan(String ref, String newer)
164   {
165     Date refdate = null, newdate = null;
166     if (ref != null && ref.trim().length() > 0)
167     {
168       try
169       {
170         refdate = DasTimeFormat.fromDASString(ref.trim());
171
172       } catch (ParseException x)
173       {
174       }
175     }
176     if (newer != null && newer.trim().length() > 0)
177     {
178       try
179       {
180         newdate = DasTimeFormat.fromDASString(newer);
181       } catch (ParseException e)
182       {
183       }
184     }
185     if (refdate != null)
186     {
187       if (newdate != null)
188       {
189         return refdate.before(newdate);
190       }
191       return false;
192     }
193     if (newdate != null)
194     {
195       return true;
196     }
197     // assume first instance of source is newest in list. - TODO: check if
198     // natural ordering of source versions is newest first or oldest first
199     return false;
200   }
201
202   public String[] getLabelsFor(VERSION v)
203   {
204     ArrayList<String> labels = new ArrayList<String>();
205     for (PROP p : v.getPROP())
206     {
207       if (p.getName().equalsIgnoreCase("LABEL"))
208       {
209         labels.add(p.getValue());
210       }
211     }
212     return labels.toArray(new String[0]);
213   }
214
215   private CAPABILITY getCapability(Capabilities capability)
216   {
217     for (CAPABILITY p : getVersion().getCAPABILITY())
218     {
219       if (p.getType().equalsIgnoreCase(capability.getName()) || p.getType()
220               .equalsIgnoreCase("das1:" + capability.getName()))
221       {
222         return p;
223       }
224     }
225     return null;
226   }
227
228   public String[] getCapabilityList(VERSION v)
229   {
230
231     ArrayList<String> labels = new ArrayList<String>();
232     for (CAPABILITY p : v.getCAPABILITY())
233     {
234       // TODO: work out what to do with namespace prefix
235       // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
236       // for moment, just show all capabilities...
237       if (p.getType().startsWith("das1:"))
238       {
239         labels.add(p.getType());
240       }
241     }
242     return labels.toArray(new String[0]);
243   }
244
245   @Override
246   public List<DbSourceProxy> getSequenceSourceProxies()
247   {
248     if (!isSequenceSource())
249     {
250       return null;
251     }
252     ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
253     if (!local)
254     {
255       VERSION v = getVersion();
256       Map<String, COORDINATES> latestc = new Hashtable<String, COORDINATES>();
257       for (COORDINATES cs : v.getCOORDINATES())
258       {
259         COORDINATES ltst = latestc.get(cs.getUri());
260         if (ltst == null || ltst.getVersion() == null
261                 || (ltst.getVersion() != null && cs.getVersion() != null
262                         && isLaterThan(ltst.getVersion(), cs.getVersion())))
263         {
264           latestc.put(cs.getUri(), cs);
265         }
266       }
267       for (COORDINATES cs : latestc.values())
268       {
269         DasSequenceSource ds;
270         /*
271          * if (css == null || css.length == 0) { // TODO: query das source
272          * directly to identify coordinate system... or // have to make up a
273          * coordinate system css = new DasCoordinateSystem[] { new
274          * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
275          * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
276          * css.length; c++) {
277          */
278         try
279         {
280           seqsources.add(ds = new DasSequenceSource(
281                   getTitle() + " (" + cs.getAuthority() + " "
282                           + cs.getSource()
283                           + (cs.getVersion() != null ? " " + cs.getVersion()
284                                   : "")
285                           + ")",
286                   cs.getAuthority(), source, v, cs, connprov));
287           if (seqsources.size() > 1)
288           {
289             System.err.println("Added another sequence DB source for "
290                     + getTitle() + " (" + ds.getDbName() + ")");
291           }
292         } catch (Exception e)
293         {
294           System.err.println("Ignoring sequence coord system " + cs + " ("
295                   + cs.getContent() + ") for source " + getTitle()
296                   + "- threw exception when constructing fetcher.\n");
297           e.printStackTrace();
298         }
299       }
300     }
301     else
302     {
303       try
304       {
305         seqsources.add(new DasSequenceSource(getTitle(), getTitle(), source,
306                 getVersion(), null, connprov));
307       } catch (Exception e)
308       {
309         // TODO Auto-generated catch block
310         e.printStackTrace();
311       }
312
313     }
314     if (seqsources.size() > 1)
315     {
316       // sort by name
317       DbSourceProxy[] tsort = seqsources.toArray(new DasSequenceSource[0]);
318       String[] nm = new String[tsort.length];
319       for (int i = 0; i < nm.length; i++)
320       {
321         nm[i] = tsort[i].getDbName().toLowerCase();
322       }
323       jalview.util.QuickSort.sort(nm, tsort);
324       seqsources.clear();
325       for (DbSourceProxy ssrc : tsort)
326       {
327         seqsources.add(ssrc);
328       }
329     }
330     return seqsources;
331   }
332
333   @Override
334   public String getSourceURL()
335   {
336     try
337     {
338       // kind of dumb, since
339       // org.biodas.jdas.dassources.utils.VersionAdapter.getSourceUriFromQueryUri()
340       // does this,
341       // but this way, we can access non DAS 1.6 compliant sources (which have
342       // to have a URL like <sourcename>/das/ and cause a validation exception)
343
344       for (CAPABILITY cap : getVersion().getCAPABILITY())
345       {
346         String capname = cap.getType()
347                 .substring(cap.getType().indexOf(":") + 1);
348         int p = cap.getQueryUri().lastIndexOf(capname);
349         if (p < -1)
350         {
351           throw new Exception(MessageManager.formatMessage(
352                   "exception.invalid_das_source", new String[]
353                   { source.getUri() }));
354         }
355         if (cap.getQueryUri().charAt(p) == '/')
356         {
357           p--;
358         }
359         return cap.getQueryUri().substring(0, p);
360       }
361     } catch (Exception x)
362     {
363       System.err.println("Serious: Couldn't get the URL for source "
364               + source.getTitle());
365       x.printStackTrace();
366     }
367     return null;
368   }
369
370   @Override
371   public boolean isNewerThan(jalviewSourceI other)
372   {
373     return isLaterThan(getVersion().getCreated(),
374             other.getVersion().getCreated());
375   }
376
377   @Override
378   public boolean isReferenceSource()
379   {
380     // TODO check source object for indication that we are the primary for a DAS
381     // coordinate system
382     return false;
383   }
384 }