45d3863fd19ef61a4e65566d379b2a3b59eeef2c
[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 jalview.util.UrlLink;
29
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.StringTokenizer;
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, UrlLink> urls;
58
59   // list of selected urls
60   private ArrayList<String> selectedUrls;
61
62   public IdentifiersUrlProvider(String cachedUrlList)
63   {
64     urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
65     selectedUrls = new ArrayList<String>();
66     checkSelectionMatchesUrls(cachedUrlList);
67   }
68
69   /**
70    * Read data from an identifiers.org download file
71    * 
72    * @param idFileName
73    *          name of identifiers.org download file
74    * @return hashmap of identifiers.org data, keyed by MIRIAM id
75    */
76   private HashMap<String, UrlLink> readIdentifiers(
77           String idFileName)
78   {
79     JSONParser parser = new JSONParser();
80
81     // identifiers.org data
82     HashMap<String, UrlLink> idData = new HashMap<String, UrlLink>();
83
84     try
85     {
86       FileReader reader = new FileReader(idFileName);
87
88       JSONArray jsonarray = (JSONArray) parser.parse(reader);
89
90       // loop over each entry in JSON array and build HashMap entry
91       for (int i = 0; i < jsonarray.size(); i++)
92       {
93         JSONObject item = (JSONObject) jsonarray.get(i);
94
95         String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
96                 + DELIM;
97         UrlLink link = new UrlLink((String) item.get("name") + SEP + url);
98         idData.put((String) item.get("id"), link);
99       }
100     } catch (FileNotFoundException e)
101     {
102       e.printStackTrace();
103     } catch (IOException e)
104     {
105       e.printStackTrace();
106     } catch (ParseException e)
107     {
108       e.printStackTrace();
109     }
110     return idData;
111   }
112
113   private void checkSelectionMatchesUrls(String cachedUrlList)
114   {
115     StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
116     while (st.hasMoreElements())
117     {
118       String id = st.nextToken();
119
120       if (isMiriamId(id))
121       {
122         // this is an identifiers.org MIRIAM id
123         if (urls.containsKey(id))
124         {
125           selectedUrls.add(id);
126         }
127       }
128     }
129
130     // reset defaultUrl in case it is no longer selected
131     setDefaultUrl(defaultUrl);
132   }
133
134   @Override
135   public boolean setDefaultUrl(String id)
136   {
137     if (selectedUrls.contains(id))
138     {
139       defaultUrl = id;
140     }
141     else
142     {
143       defaultUrl = null;
144     }
145     return selectedUrls.contains(id);
146   }
147
148   @Override
149   public String writeUrlsAsString(boolean selected)
150   {
151     if (!selected)
152     {
153       return ""; // we don't cache unselected identifiers.org urls
154     }
155
156     StringBuffer links = new StringBuffer();
157     if (!selectedUrls.isEmpty())
158     {
159       for (String k : selectedUrls)
160       {
161         links.append(k);
162         links.append(SEP);
163       }
164       // remove last SEP
165       links.setLength(links.length() - 1);
166     }
167     return links.toString();
168   }
169
170   @Override
171   public Vector<String> getLinksForMenu()
172   {
173     Vector<String> links = new Vector<String>();
174     for (String key : selectedUrls)
175     {
176       links.add(urls.get(key).toString());
177     }
178     return links;
179   }
180
181   @Override
182   public List<UrlLinkDisplay> getLinksForTable()
183   {
184     return super.getLinksForTable(urls, selectedUrls, false);
185   }
186
187   @Override
188   public void setUrlData(List<UrlLinkDisplay> links)
189   {
190     selectedUrls = new ArrayList<String>();
191
192     Iterator<UrlLinkDisplay> it = links.iterator();
193     while (it.hasNext())
194     {
195       UrlLinkDisplay link = it.next();
196
197       // Handle links with MIRIAM ids only
198       if (isMiriamId(link.getId()))
199       {
200         // select/deselect links accordingly and set default url
201
202         if (link.getIsSelected())
203         {
204           if (urls.containsKey(link.getId()))
205           {
206             selectedUrls.add(link.getId());
207           }
208           if (link.getIsDefault())
209           {
210             setDefaultUrl(link.getId());
211           }
212
213         }
214       }
215
216     }
217   }
218
219   @Override
220   public String getDefaultUrl(String seqid)
221   {
222     return super.getDefaultUrl(seqid, urls);
223   }
224
225   @Override
226   public String getDefaultUrlId()
227   {
228     return defaultUrl;
229   }
230
231   @Override
232   public String getDefaultTarget(String seqid)
233   {
234     // TODO Auto-generated method stub
235     return null;
236   }
237
238   @Override
239   public String chooseDefaultUrl()
240   {
241     // TODO Auto-generated method stub
242     return null;
243   }
244
245 }