2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import static jalview.util.UrlConstants.DB_ACCESSION;
25 import static jalview.util.UrlConstants.DELIM;
26 import static jalview.util.UrlConstants.SEP;
28 import jalview.util.JSONUtils;
29 import jalview.util.UrlLink;
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;
38 import java.util.StringTokenizer;
40 import org.json.simple.parser.ParseException;
44 * Implements the UrlProviderI interface for a UrlProvider object which serves
45 * URLs from identifiers.org
50 public class IdentifiersUrlProvider extends UrlProviderImpl
53 private static final String LOCAL_KEY = "Local";
55 private static final String ID_ORG_KEY = "identifiers.org";
57 // map of string ids to urls
58 private HashMap<String, UrlLink> urls;
60 // list of selected urls
61 private ArrayList<String> selectedUrls;
63 public IdentifiersUrlProvider(String cachedUrlList)
65 urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
66 selectedUrls = new ArrayList<>();
67 checkSelectionMatchesUrls(cachedUrlList);
71 * Read data from an identifiers.org download file
74 * name of identifiers.org download file
75 * @return hashmap of identifiers.org data, keyed by MIRIAM id
77 @SuppressWarnings("unchecked")
78 private HashMap<String, UrlLink> readIdentifiers(String idFileName)
80 // identifiers.org data
81 HashMap<String, UrlLink> idData = new HashMap<>();
83 String errorMessage = null;
86 // NOTE: THIS WILL FAIL IN SWINGJS BECAUSE IT INVOLVES A FILE READER
88 FileReader reader = new FileReader(idFileName);
90 Map<String, Object> obj = (Map<String, Object>) JSONUtils
92 if (obj.containsKey(ID_ORG_KEY))
96 else if (obj.containsKey(LOCAL_KEY))
102 jalview.bin.Console.outPrintln(
103 "Unexpected key returned from identifiers jalview service");
107 List<Object> jsonarray = (List<Object>) obj.get(key);
109 // loop over each entry in JSON array and build HashMap entry
110 for (int i = 0; i < jsonarray.size(); i++)
112 Map<String, Object> item = (Map<String, Object>) jsonarray.get(i);
114 String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
116 UrlLink link = new UrlLink((String) item.get("name"), url,
117 (String) item.get("prefix"));
118 idData.put((String) item.get("id"), link);
120 } catch (IOException | ParseException e)
122 // unnecessary e.printStackTrace();
123 // Note how in JavaScript we can grab the first bytes from any file
125 // Typical report here is "NetworkError" because the file does not exist.
126 // "https://." is coming from System.getProperty("user.home"), but this
128 // be set by the page developer to anything, of course.
129 errorMessage = e.toString();
132 // BH 2018 -- added more valuable report
133 if (errorMessage != null)
135 jalview.bin.Console.errPrintln("IdentifiersUrlProvider: cannot read " + idFileName
136 + ": " + errorMessage);
141 private void checkSelectionMatchesUrls(String cachedUrlList)
143 StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
144 while (st.hasMoreElements())
146 String id = st.nextToken();
150 // this is an identifiers.org MIRIAM id
151 if (urls.containsKey(id))
153 selectedUrls.add(id);
158 // reset defaultUrl in case it is no longer selected
159 setPrimaryUrl(primaryUrl);
163 public boolean setPrimaryUrl(String id)
165 if (urls.containsKey(id))
174 return urls.containsKey(id);
178 public String writeUrlsAsString(boolean selected)
182 return ""; // we don't cache unselected identifiers.org urls
185 StringBuffer links = new StringBuffer();
186 if (!selectedUrls.isEmpty())
188 for (String k : selectedUrls)
194 links.setLength(links.length() - 1);
196 return links.toString();
200 public List<String> getLinksForMenu()
202 List<String> links = new ArrayList<>();
203 for (String key : selectedUrls)
205 links.add(urls.get(key).toStringWithTarget());
211 public List<UrlLinkDisplay> getLinksForTable()
213 return super.getLinksForTable(urls, selectedUrls, false);
217 public void setUrlData(List<UrlLinkDisplay> links)
219 selectedUrls = new ArrayList<>();
221 Iterator<UrlLinkDisplay> it = links.iterator();
224 UrlLinkDisplay link = it.next();
226 // Handle links with MIRIAM ids only
227 if (isMiriamId(link.getId()))
229 // select/deselect links accordingly and set default url
230 if (urls.containsKey(link.getId()))
232 if (link.getIsSelected())
234 selectedUrls.add(link.getId());
236 if (link.getIsPrimary())
238 setPrimaryUrl(link.getId());
246 public String getPrimaryUrl(String seqid)
248 return super.getPrimaryUrl(seqid, urls);
252 public String getPrimaryUrlId()
258 public String getPrimaryTarget(String seqid)
264 public String choosePrimaryUrl()
270 public boolean contains(String id)
272 return (urls.containsKey(id));