JAL-1740 implemented auto-update of BioJsMSA, introduced FTP client for downloading...
[jalview.git] / src / jalview / io / BioJsHTMLOutput.java
index 669054d..9c15991 100644 (file)
@@ -1,21 +1,49 @@
 package jalview.io;
 
 import jalview.exceptions.NoFileSelectedException;
+import jalview.ftp.FtpClient;
 import jalview.gui.AlignViewport;
 import jalview.gui.AlignmentPanel;
 import jalview.gui.FeatureRenderer;
 import jalview.util.MessageManager;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Objects;
+import java.util.TreeMap;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPFile;
+
 
 public class BioJsHTMLOutput
 {
   private AlignViewport av;
 
+  private static File currentBJSTemplateFile;
+
+  private static TreeMap<String, File> bioJsMSAVersions;
+
+  public static final String BJS_TEMPLATES_LOCAL_DIRECTORY = jalview.bin.Cache
+          .getDefault("biojs_template_directory", "/biojs_templates/");
+
+  public static final String BJS_FTP_USER = jalview.bin.Cache.getDefault(
+          "biojs_ftp_user", "test");
+
+  public static final String BJS_FTP_PWD = jalview.bin.Cache.getDefault(
+          "biojs_ftp_pwd", "test");
+
+  public static final String BJS_FTP_PORT = jalview.bin.Cache.getDefault(
+          "biojs_ftp_port", "22");
+
+  public static final String BJS_FTP_SERVER = jalview.bin.Cache.getDefault(
+          "biojs_ftp_server", "localhost");
 
   public BioJsHTMLOutput(AlignmentPanel ap,
           FeatureRenderer fr1)
@@ -36,7 +64,7 @@ public class BioJsHTMLOutput
     {
       String outputFile = getOutputFile();
       String jalviewAlignmentJson = JSONFile.getJSONData(av);
-      String bioJSTemplateString = getBioJsTemplateAsString(this);
+      String bioJSTemplateString = getBioJsTemplateAsString();
       String generatedBioJsWithJalviewAlignmentAsJson = bioJSTemplateString
               .replaceAll(
 "#sequenceData#", jalviewAlignmentJson)
@@ -87,14 +115,16 @@ public class BioJsHTMLOutput
   }
 
 
-  public static String getBioJsTemplateAsString(Object currentObj)
+  public static String getBioJsTemplateAsString()
           throws IOException
   {
     InputStreamReader isReader = null;
     BufferedReader buffReader = null;
     StringBuilder sb = new StringBuilder();
-    URL url = currentObj.getClass().getResource(
-            "/templates/BioJSTemplate.txt");
+    Objects.requireNonNull(getCurrentBJSTemplateFile(),
+            "BioJsTemplate File not initialized!");
+    @SuppressWarnings("deprecation")
+    URL url = getCurrentBJSTemplateFile().toURL();
     if (url != null)
     {
       try
@@ -126,4 +156,107 @@ public class BioJsHTMLOutput
     }
     return sb.toString();
   }
+
+  public TreeMap<String, File> updateBioJSVersionsInfo(String dirName)
+          throws URISyntaxException
+  {
+    URL url = getClass().getResource(dirName);
+    File directory = new File(url.toURI());
+    Objects.requireNonNull(dirName, "dirName MUST not be null!");
+    Objects.requireNonNull(directory, "directory MUST not be null!");
+    TreeMap<String, File> versionFileMap = new TreeMap<String, File>();
+
+    for (File file : directory.listFiles())
+    {
+      if (file.isFile())
+      {
+        String fileName = file.getName().substring(0,
+                file.getName().lastIndexOf("."));
+        String fileMeta[] = fileName.split("_");
+        if (fileMeta.length > 2)
+        {
+          setCurrentBJSTemplateFile(file);
+          versionFileMap.put(fileMeta[2], file);
+        }
+        else if (fileMeta.length > 1)
+        {
+          versionFileMap.put(fileMeta[1], file);
+        }
+      }
+    }
+    if (getCurrentBJSTemplateFile() == null && versionFileMap.size() > 0)
+    {
+      setCurrentBJSTemplateFile(versionFileMap.lastEntry().getValue());
+    }
+    return versionFileMap;
+  }
+
+  public void updateBioJS()
+  {
+    TreeMap<String, File> versionLists = null;
+    try
+    {
+      // downlaodNewBioJsTemplates(BJS_TEMPLATES_LOCAL_DIRECTORY);
+      versionLists = updateBioJSVersionsInfo(BJS_TEMPLATES_LOCAL_DIRECTORY);
+      setBioJsMSAVersions(versionLists);
+    } catch (URISyntaxException e)
+    {
+      e.printStackTrace();
+    }
+  }
+
+  public void downlaodNewBioJsTemplates(String localDirectory)
+  {
+    FTPClient client = FtpClient.getFtpClient(BJS_FTP_SERVER);
+    if (FtpClient.authenticateUser(client, BJS_FTP_USER, BJS_FTP_PWD))
+    {
+      client.enterLocalPassiveMode();
+      try
+      {
+        client.setFileType(FTP.BINARY_FILE_TYPE);
+        for (FTPFile fFile : client.listFiles())
+        {
+          String localFileName = BJS_TEMPLATES_LOCAL_DIRECTORY
+                  + fFile.getName();
+          String remoteFileName = fFile.getName();
+          FtpClient.downloadFile(client, remoteFileName, localFileName);
+        }
+      } catch (IOException e)
+      {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  // public static void main(String[] args) throws IOException
+  // {
+  // Document doc = Jsoup.connect("http://howto.unixdev.net").get();
+  // for (Element file : doc.select("td.right td a"))
+  // {
+  // System.out.println(file.attr("href"));
+  // }
+  // }
+
+  public static File getCurrentBJSTemplateFile()
+  {
+    return currentBJSTemplateFile;
+  }
+
+  public static void setCurrentBJSTemplateFile(File currentBJSTemplateFile)
+  {
+    BioJsHTMLOutput.currentBJSTemplateFile = currentBJSTemplateFile;
+  }
+
+  public static TreeMap<String, File> getBioJsMSAVersions()
+  {
+    return bioJsMSAVersions;
+  }
+
+  public static void setBioJsMSAVersions(
+          TreeMap<String, File> bioJsMSAVersions)
+  {
+    BioJsHTMLOutput.bioJsMSAVersions = bioJsMSAVersions;
+  }
+
+
 }