JAL-972 - correct name when constructing db-source from DAS source
[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.List;
7
8 import org.biodas.jdas.dassources.Capabilities;
9 import org.biodas.jdas.dassources.utils.DasTimeFormat;
10 import org.biodas.jdas.dassources.utils.RegistrySourceAdapter;
11 import org.biodas.jdas.schema.sources.CAPABILITY;
12 import org.biodas.jdas.schema.sources.COORDINATES;
13 import org.biodas.jdas.schema.sources.MAINTAINER;
14 import org.biodas.jdas.schema.sources.PROP;
15 import org.biodas.jdas.schema.sources.SOURCE;
16 import org.biodas.jdas.schema.sources.VERSION;
17
18 import jalview.ws.dbsources.das.api.jalviewSourceI;
19 import jalview.ws.seqfetcher.DbSourceProxy;
20
21 public class JalviewSource implements jalviewSourceI
22 {
23   SOURCE source;
24
25   public JalviewSource(SOURCE local2, boolean local)
26   {
27     this.local = local;
28     source = local2;
29   }
30
31   @Override
32   public String getTitle()
33   {
34     return source.getTitle();
35   }
36
37   @Override
38   public VERSION getVersion()
39   {
40     
41     return getVersionFor(source);
42   }
43
44   @Override
45   public String getDocHref()
46   {
47     return source.getDocHref();
48   }
49
50   @Override
51   public String getDescription()
52   {
53     return source.getDescription();
54   }
55
56   @Override
57   public String getUri()
58   {
59     return source.getUri();
60   }
61
62   @Override
63   public MAINTAINER getMAINTAINER()
64   {
65     return source.getMAINTAINER();
66   }
67
68   @Override
69   public String getEmail()
70   {
71     return (local) ? null: source.getMAINTAINER().getEmail();
72   }
73
74   boolean local = false;
75
76   @Override
77   public boolean isLocal()
78   {
79     return local;
80   }
81
82   @Override
83   public boolean isSequenceSource()
84   {
85     String seqcap = "das1:" + Capabilities.SEQUENCE.getName();
86     for (String cp : getCapabilityList(getVersionFor(source)))
87     {
88       if (cp.equals(seqcap))
89       {
90         return true;
91
92       }
93     }
94     return false;
95   }
96   @Override
97   public boolean isFeatureSource()
98   {
99     String seqcap = "das1:" + Capabilities.FEATURES.getName();
100     for (String cp : getCapabilityList(getVersionFor(source)))
101     {
102       if (cp.equals(seqcap))
103       {
104         return true;
105
106       }
107     }
108     return false;
109   }
110
111   private VERSION getVersionFor(SOURCE ds)
112   {
113     VERSION latest = null;
114     for (VERSION v : ds.getVERSION())
115     {
116       if (latest == null
117               || isLaterThan(latest.getCreated(), v.getCreated()))
118       {
119         // TODO: das 1.6 - should just get the first version - ignore other
120         // versions since not specified how to construct URL from version's URI
121         // + source URI
122         latest = v;
123       }
124     }
125     return latest;
126   }
127
128   private boolean isLaterThan(String ref, String newer)
129   {
130     Date refdate = null, newdate = null;
131     try
132     {
133       refdate = DasTimeFormat.fromDASString(ref);
134
135     } catch (ParseException x)
136     {
137       return false;
138     }
139     try
140     {
141       newdate = DasTimeFormat.fromDASString(newer);
142     } catch (ParseException e)
143     {
144       // TODO: handle exception
145     }
146     if (refdate != null)
147     {
148       if (newdate != null)
149       {
150         return refdate.before(newdate);
151       }
152       return false;
153     }
154     if (newdate != null)
155     {
156       return true;
157     }
158     // assume first instance of source is newest in list. - TODO: check if
159     // natural ordering of source versions is newest first or oldest first
160     return false;
161   }
162
163   public String[] getLabelsFor(VERSION v)
164   {
165     ArrayList<String> labels = new ArrayList<String>();
166     for (PROP p : v.getPROP())
167     {
168       if (p.getName().equalsIgnoreCase("LABEL"))
169       {
170         labels.add(p.getValue());
171       }
172     }
173     return labels.toArray(new String[0]);
174   }
175
176   private CAPABILITY getCapability(Capabilities capability) {
177     for (CAPABILITY p: getVersion().getCAPABILITY()) {
178       if (p.getType().equalsIgnoreCase(capability.getName()) || p.getType().equalsIgnoreCase("das1:"+capability.getName()))
179       {
180         return p;
181       }
182     }
183     return null;
184   }
185   public String[] getCapabilityList(VERSION v)
186   {
187
188     ArrayList<String> labels = new ArrayList<String>();
189     for (CAPABILITY p : v.getCAPABILITY())
190     {
191       // TODO: work out what to do with namespace prefix
192       // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ?
193       // for moment, just show all capabilities...
194       if (p.getType().startsWith("das1:"))
195       {
196         labels.add(p.getType());
197       }
198     }
199     return labels.toArray(new String[0]);
200   }
201
202   @Override
203   public List<DbSourceProxy> getSequenceSourceProxies()
204   {
205     if (!isSequenceSource())
206     {
207       return null;
208     }
209     ArrayList<DbSourceProxy> seqsources = new ArrayList<DbSourceProxy>();
210     if (!local)
211     {
212     VERSION v = getVersion();
213     for (COORDINATES cs : v.getCOORDINATES())
214     {
215
216       /*
217        * if (css == null || css.length == 0) { // TODO: query das source
218        * directly to identify coordinate system... or // have to make up a
219        * coordinate system css = new DasCoordinateSystem[] { new
220        * DasCoordinateSystem() }; css[0].setName(d1s.getNickname());
221        * css[0].setUniqueId(d1s.getNickname()); } for (int c = 0; c <
222        * css.length; c++) {
223        */
224       try
225       {
226         seqsources.add(new DasSequenceSource("das:" + getTitle() + " ("
227                 + cs.getAuthority()+" "+cs.getSource() + ")", cs.getAuthority(), source, v, cs));
228       } catch (Exception e)
229       {
230         System.err.println("Ignoring sequence coord system " + cs + " ("
231                 + cs.getContent() + ") for source " + getTitle()
232                 + "- threw exception when constructing fetcher.\n");
233         e.printStackTrace();
234       }
235     }
236     } else {
237       try
238       {
239         seqsources.add(new DasSequenceSource("das:"+getTitle(), getTitle(), source, getVersion(), null));
240       } catch (Exception e)
241       {
242         // TODO Auto-generated catch block
243         e.printStackTrace();
244       }
245       
246     }
247     return seqsources;
248   }
249   @Override
250   public String getSourceURL()
251   {
252     try {
253     String url = new RegistrySourceAdapter(source).getOriginalDataSourceUri();
254     return url;
255     }
256     catch (Exception x)
257     {
258       System.err.println("Serious: Couldn't get the URL for source "+source.getTitle());
259       x.printStackTrace();
260     }
261     return null;
262   }
263 }