JAL-2316 First pass identifiers.org downloading working
[jalview.git] / src / jalview / urls / IdentifiersUrlProvider.java
index 4b88356..7819136 100644 (file)
 
 package jalview.urls;
 
-import java.util.Map;
+import static jalview.util.UrlConstants.DB_ACCESSION;
+import static jalview.util.UrlConstants.DELIM;
+import static jalview.util.UrlConstants.SEP;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Vector;
 
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
 /**
  * 
  * Implements the UrlProviderI interface for a UrlProvider object which serves
@@ -35,48 +53,160 @@ import java.util.Vector;
 public class IdentifiersUrlProvider extends UrlProviderImpl
 {
 
+  // map of string ids to urls
+  private HashMap<String, HashMap<String, String>> urls;
+
+  // list of selected urls
+  private ArrayList<String> selectedUrls;
+
   public IdentifiersUrlProvider(String cachedUrlList, String idFileName)
   {
+    try
+    {
+      // File idFile = getIdentifiers();
+      urls = readIdentifiers(new FileReader(idFileName));
+      checkSelectionMatchesUrls(cachedUrlList);
+
+    } catch (IOException e)
+    {
+
+    }
+  }
+
+  private File getIdentifiers() throws IOException
+  {
+    String identifiersorgUrl = "http://identifiers.org/rest/collections/";
+    String outfile = "identifiers.json";
+    int BUFFER_SIZE = 4096;
+    
+    URL url = new URL(identifiersorgUrl);
+    InputStream is = new BufferedInputStream(url.openStream());
+    FileOutputStream os = new FileOutputStream(outfile);
+    byte[] buffer = new byte[BUFFER_SIZE];
+    int bytesRead = -1;
+    while ((bytesRead = is.read(buffer)) != -1)
+    {
+      os.write(buffer, 0, bytesRead);
+    }
+    os.close();
+    is.close();
+    
+    return new File(outfile);
+  }
 
+  private HashMap<String, HashMap<String, String>> readIdentifiers(
+          FileReader reader)
+  {
+    JSONParser parser = new JSONParser();
+    HashMap<String, HashMap<String, String>> idData = new HashMap<String, HashMap<String, String>>();
+
+    try
+    {
+      JSONArray jsonarray = (JSONArray) parser.parse(reader);
+
+      // loop over each entry in JSON array and build HashMap entry
+      for (int i = 0; i < jsonarray.size(); i++)
+      {
+        JSONObject item = (JSONObject) jsonarray.get(i);
+
+        HashMap<String, String> idEntry = new HashMap<String, String>();
+        idEntry.put("name", (String) item.get("name"));
+        idEntry.put("url", (String) item.get("url"));
+        idData.put((String) item.get("id"), idEntry);
+      }
+    } catch (FileNotFoundException e)
+    {
+      e.printStackTrace();
+    } catch (IOException e)
+    {
+      e.printStackTrace();
+    } catch (ParseException e)
+    {
+      e.printStackTrace();
+    }
+    return idData;
   }
 
-  public IdentifiersUrlProvider(Map<String, String> urlList,
-          String idFileName)
+  private void checkSelectionMatchesUrls(String cachedUrlList)
   {
+    selectedUrls = new ArrayList<String>();
+    String[] prevSelected = cachedUrlList.split("\\" + SEP);
+    for (String id : prevSelected)
+    {
+      if (urls.containsKey(id))
+      {
+        selectedUrls.add(id);
+      }
+    }
+
+    // reset defaultUrl in case it is no longer selected
+    setDefaultUrl(defaultUrl);
+  }
 
+  private void checkSelectionMatchesUrls(Vector<String> idList)
+  {
+    selectedUrls = new ArrayList<String>();
+    String[] prevSelected = new String[idList.size()];
+    idList.toArray(prevSelected);
+    for (String id : prevSelected)
+    {
+      if (urls.containsKey(id))
+      {
+        selectedUrls.add(id);
+      }
+    }
+
+    // reset defaultUrl in case it is no longer selected
+    setDefaultUrl(defaultUrl);
   }
 
   @Override
   public String getDefaultUrl()
   {
-    // TODO Auto-generated method stub
-    return null;
+    return defaultUrl;
   }
 
   @Override
   public boolean setDefaultUrl(String id)
   {
-    return false;
+    if (selectedUrls.contains(id))
+    {
+      defaultUrl = id;
+    }
+    else
+    {
+      defaultUrl = null;
+    }
+    return selectedUrls.contains(id);
   }
 
   @Override
   public String writeUrlsAsString()
   {
-    return "";
+    StringBuffer links = new StringBuffer();
+    for (String k : selectedUrls)
+    {
+      links.append(k);
+    }
+    return links.toString();
   }
 
   @Override
   public Vector<String> getLinksForDisplay()
   {
-    // TODO Auto-generated method stub
-    return null;
+    Vector<String> links = new Vector<String>();
+    for (String key : selectedUrls)
+    {
+      links.add(key + SEP + urls.get(key).get("url") + "/" + DELIM
+              + DB_ACCESSION + DELIM);
+    }
+    return links;
   }
 
   @Override
   public String getDefaultUrl(String seqid)
   {
-    // TODO Auto-generated method stub
-    return null;
+    return urls.get(defaultUrl).get("url") + "/" + seqid;
   }
 
   @Override
@@ -89,8 +219,8 @@ public class IdentifiersUrlProvider extends UrlProviderImpl
   @Override
   public void setUrlLinks(Vector<String> names, Vector<String> urls)
   {
-    // deliberately left empty
-    // TODO throw exception if called
+    // ignores urls, only uses names (as ids)
+    checkSelectionMatchesUrls(names);
   }
 
   @Override