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