JAL-2316 First pass identifiers.org downloading working
[jalview.git] / src / jalview / urls / IdentifiersUrlProvider.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
22 package jalview.urls;
23
24 import static jalview.util.UrlConstants.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
27
28 import java.io.BufferedInputStream;
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.URL;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Vector;
39
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.json.simple.parser.ParseException;
44
45 /**
46  * 
47  * Implements the UrlProviderI interface for a UrlProvider object which serves
48  * URLs from identifiers.org
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class IdentifiersUrlProvider extends UrlProviderImpl
54 {
55
56   // map of string ids to urls
57   private HashMap<String, HashMap<String, String>> urls;
58
59   // list of selected urls
60   private ArrayList<String> selectedUrls;
61
62   public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
63   {
64     try
65     {
66       // File idFile = getIdentifiers();
67       urls = readIdentifiers(new FileReader(idFileName));
68       checkSelectionMatchesUrls(cachedUrlList);
69
70     } catch (IOException e)
71     {
72
73     }
74   }
75
76   private File getIdentifiers() throws IOException
77   {
78     String identifiersorgUrl = "http://identifiers.org/rest/collections/";
79     String outfile = "identifiers.json";
80     int BUFFER_SIZE = 4096;
81     
82     URL url = new URL(identifiersorgUrl);
83     InputStream is = new BufferedInputStream(url.openStream());
84     FileOutputStream os = new FileOutputStream(outfile);
85     byte[] buffer = new byte[BUFFER_SIZE];
86     int bytesRead = -1;
87     while ((bytesRead = is.read(buffer)) != -1)
88     {
89       os.write(buffer, 0, bytesRead);
90     }
91     os.close();
92     is.close();
93     
94     return new File(outfile);
95   }
96
97   private HashMap<String, HashMap<String, String>> readIdentifiers(
98           FileReader reader)
99   {
100     JSONParser parser = new JSONParser();
101     HashMap<String, HashMap<String, String>> idData = new HashMap<String, HashMap<String, String>>();
102
103     try
104     {
105       JSONArray jsonarray = (JSONArray) parser.parse(reader);
106
107       // loop over each entry in JSON array and build HashMap entry
108       for (int i = 0; i < jsonarray.size(); i++)
109       {
110         JSONObject item = (JSONObject) jsonarray.get(i);
111
112         HashMap<String, String> idEntry = new HashMap<String, String>();
113         idEntry.put("name", (String) item.get("name"));
114         idEntry.put("url", (String) item.get("url"));
115         idData.put((String) item.get("id"), idEntry);
116       }
117     } catch (FileNotFoundException e)
118     {
119       e.printStackTrace();
120     } catch (IOException e)
121     {
122       e.printStackTrace();
123     } catch (ParseException e)
124     {
125       e.printStackTrace();
126     }
127     return idData;
128   }
129
130   private void checkSelectionMatchesUrls(String cachedUrlList)
131   {
132     selectedUrls = new ArrayList<String>();
133     String[] prevSelected = cachedUrlList.split("\\" + SEP);
134     for (String id : prevSelected)
135     {
136       if (urls.containsKey(id))
137       {
138         selectedUrls.add(id);
139       }
140     }
141
142     // reset defaultUrl in case it is no longer selected
143     setDefaultUrl(defaultUrl);
144   }
145
146   private void checkSelectionMatchesUrls(Vector<String> idList)
147   {
148     selectedUrls = new ArrayList<String>();
149     String[] prevSelected = new String[idList.size()];
150     idList.toArray(prevSelected);
151     for (String id : prevSelected)
152     {
153       if (urls.containsKey(id))
154       {
155         selectedUrls.add(id);
156       }
157     }
158
159     // reset defaultUrl in case it is no longer selected
160     setDefaultUrl(defaultUrl);
161   }
162
163   @Override
164   public String getDefaultUrl()
165   {
166     return defaultUrl;
167   }
168
169   @Override
170   public boolean setDefaultUrl(String id)
171   {
172     if (selectedUrls.contains(id))
173     {
174       defaultUrl = id;
175     }
176     else
177     {
178       defaultUrl = null;
179     }
180     return selectedUrls.contains(id);
181   }
182
183   @Override
184   public String writeUrlsAsString()
185   {
186     StringBuffer links = new StringBuffer();
187     for (String k : selectedUrls)
188     {
189       links.append(k);
190     }
191     return links.toString();
192   }
193
194   @Override
195   public Vector<String> getLinksForDisplay()
196   {
197     Vector<String> links = new Vector<String>();
198     for (String key : selectedUrls)
199     {
200       links.add(key + SEP + urls.get(key).get("url") + "/" + DELIM
201               + DB_ACCESSION + DELIM);
202     }
203     return links;
204   }
205
206   @Override
207   public String getDefaultUrl(String seqid)
208   {
209     return urls.get(defaultUrl).get("url") + "/" + seqid;
210   }
211
212   @Override
213   public String getDefaultTarget(String seqid)
214   {
215     // TODO Auto-generated method stub
216     return null;
217   }
218
219   @Override
220   public void setUrlLinks(Vector<String> names, Vector<String> urls)
221   {
222     // ignores urls, only uses names (as ids)
223     checkSelectionMatchesUrls(names);
224   }
225
226   @Override
227   public String chooseDefaultUrl()
228   {
229     // TODO Auto-generated method stub
230     return null;
231   }
232
233 }