JAL-2418 source formatting
[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
39 import org.json.simple.JSONArray;
40 import org.json.simple.JSONObject;
41 import org.json.simple.parser.JSONParser;
42 import org.json.simple.parser.ParseException;
43
44 /**
45  * 
46  * Implements the UrlProviderI interface for a UrlProvider object which serves
47  * URLs from identifiers.org
48  * 
49  * @author $author$
50  * @version $Revision$
51  */
52 public class IdentifiersUrlProvider extends UrlProviderImpl
53 {
54
55   private static final String LOCAL_KEY = "Local";
56
57   private static final String ID_ORG_KEY = "identifiers.org";
58
59   // map of string ids to urls
60   private HashMap<String, UrlLink> urls;
61
62   // list of selected urls
63   private ArrayList<String> selectedUrls;
64
65   public IdentifiersUrlProvider(String cachedUrlList)
66   {
67     urls = readIdentifiers(IdOrgSettings.getDownloadLocation());
68     selectedUrls = new ArrayList<String>();
69     checkSelectionMatchesUrls(cachedUrlList);
70   }
71
72   /**
73    * Read data from an identifiers.org download file
74    * 
75    * @param idFileName
76    *          name of identifiers.org download file
77    * @return hashmap of identifiers.org data, keyed by MIRIAM id
78    */
79   private HashMap<String, UrlLink> readIdentifiers(String idFileName)
80   {
81     JSONParser parser = new JSONParser();
82
83     // identifiers.org data
84     HashMap<String, UrlLink> idData = new HashMap<String, UrlLink>();
85
86     try
87     {
88       FileReader reader = new FileReader(idFileName);
89       String key = "";
90       JSONObject obj = (JSONObject) parser.parse(reader);
91       if (obj.containsKey(ID_ORG_KEY))
92       {
93         key = ID_ORG_KEY;
94       }
95       else if (obj.containsKey(LOCAL_KEY))
96       {
97         key = LOCAL_KEY;
98       }
99       else
100       {
101         System.out.println(
102                 "Unexpected key returned from identifiers jalview service");
103         return idData;
104       }
105
106       JSONArray jsonarray = (JSONArray) obj.get(key);
107
108       // loop over each entry in JSON array and build HashMap entry
109       for (int i = 0; i < jsonarray.size(); i++)
110       {
111         JSONObject item = (JSONObject) jsonarray.get(i);
112
113         String url = (String) item.get("url") + "/" + DELIM + DB_ACCESSION
114                 + DELIM;
115         UrlLink link = new UrlLink((String) item.get("name"), url,
116                 (String) item.get("prefix"));
117         idData.put((String) item.get("id"), link);
118       }
119     } catch (FileNotFoundException e)
120     {
121       e.printStackTrace();
122       idData.clear();
123     } catch (IOException e)
124     {
125       e.printStackTrace();
126       idData.clear();
127     } catch (ParseException e)
128     {
129       e.printStackTrace();
130       idData.clear();
131     }
132     return idData;
133   }
134
135   private void checkSelectionMatchesUrls(String cachedUrlList)
136   {
137     StringTokenizer st = new StringTokenizer(cachedUrlList, SEP);
138     while (st.hasMoreElements())
139     {
140       String id = st.nextToken();
141
142       if (isMiriamId(id))
143       {
144         // this is an identifiers.org MIRIAM id
145         if (urls.containsKey(id))
146         {
147           selectedUrls.add(id);
148         }
149       }
150     }
151
152     // reset defaultUrl in case it is no longer selected
153     setPrimaryUrl(primaryUrl);
154   }
155
156   @Override
157   public boolean setPrimaryUrl(String id)
158   {
159     if (urls.containsKey(id))
160     {
161       primaryUrl = id;
162     }
163     else
164     {
165       primaryUrl = null;
166     }
167
168     return urls.containsKey(id);
169   }
170
171   @Override
172   public String writeUrlsAsString(boolean selected)
173   {
174     if (!selected)
175     {
176       return ""; // we don't cache unselected identifiers.org urls
177     }
178
179     StringBuffer links = new StringBuffer();
180     if (!selectedUrls.isEmpty())
181     {
182       for (String k : selectedUrls)
183       {
184         links.append(k);
185         links.append(SEP);
186       }
187       // remove last SEP
188       links.setLength(links.length() - 1);
189     }
190     return links.toString();
191   }
192
193   @Override
194   public List<String> getLinksForMenu()
195   {
196     List<String> links = new ArrayList<String>();
197     for (String key : selectedUrls)
198     {
199       links.add(urls.get(key).toStringWithTarget());
200     }
201     return links;
202   }
203
204   @Override
205   public List<UrlLinkDisplay> getLinksForTable()
206   {
207     return super.getLinksForTable(urls, selectedUrls, false);
208   }
209
210   @Override
211   public void setUrlData(List<UrlLinkDisplay> links)
212   {
213     selectedUrls = new ArrayList<String>();
214
215     Iterator<UrlLinkDisplay> it = links.iterator();
216     while (it.hasNext())
217     {
218       UrlLinkDisplay link = it.next();
219
220       // Handle links with MIRIAM ids only
221       if (isMiriamId(link.getId()))
222       {
223         // select/deselect links accordingly and set default url
224         if (urls.containsKey(link.getId()))
225         {
226           if (link.getIsSelected())
227           {
228             selectedUrls.add(link.getId());
229           }
230           if (link.getIsPrimary())
231           {
232             setPrimaryUrl(link.getId());
233           }
234         }
235       }
236     }
237   }
238
239   @Override
240   public String getPrimaryUrl(String seqid)
241   {
242     return super.getPrimaryUrl(seqid, urls);
243   }
244
245   @Override
246   public String getPrimaryUrlId()
247   {
248     return primaryUrl;
249   }
250
251   @Override
252   public String getPrimaryTarget(String seqid)
253   {
254     return null;
255   }
256
257   @Override
258   public String choosePrimaryUrl()
259   {
260     return null;
261   }
262
263   @Override
264   public boolean contains(String id)
265   {
266     return (urls.containsKey(id));
267   }
268 }