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