mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/ease/EASEUpdateManager.java

Code
Comments
Other
Rev Date Author Line
2 26 Feb 07 jari 1 /*
2 26 Feb 07 jari 2 Copyright @ 1999-2006, The Institute for Genomic Research (TIGR).
2 26 Feb 07 jari 3 All rights reserved.
2 26 Feb 07 jari 4  */
2 26 Feb 07 jari 5 /*
2 26 Feb 07 jari 6  * EaseUpdateManager.java
2 26 Feb 07 jari 7  *
2 26 Feb 07 jari 8  * Created on January 19, 2005, 2:53 PM
2 26 Feb 07 jari 9  */
2 26 Feb 07 jari 10 package org.tigr.microarray.mev.cluster.gui.impl.ease;
2 26 Feb 07 jari 11
2 26 Feb 07 jari 12 import java.awt.event.ActionEvent;
2 26 Feb 07 jari 13 import java.awt.event.WindowEvent;
2 26 Feb 07 jari 14 import java.awt.event.WindowListener;
2 26 Feb 07 jari 15 import java.io.BufferedInputStream;
2 26 Feb 07 jari 16 import java.io.BufferedOutputStream;
2 26 Feb 07 jari 17 import java.io.BufferedReader;
2 26 Feb 07 jari 18 import java.io.File;
2 26 Feb 07 jari 19 import java.io.FileOutputStream;
2 26 Feb 07 jari 20 import java.io.IOException;
2 26 Feb 07 jari 21 import java.io.InputStream;
2 26 Feb 07 jari 22 import java.io.InputStreamReader;
2 26 Feb 07 jari 23 import java.net.URL;
2 26 Feb 07 jari 24 import java.net.URLConnection;
2 26 Feb 07 jari 25 import java.util.Enumeration;
2 26 Feb 07 jari 26 import java.util.Hashtable;
2 26 Feb 07 jari 27 import java.util.StringTokenizer;
2 26 Feb 07 jari 28 import java.util.Vector;
2 26 Feb 07 jari 29 import java.util.zip.ZipEntry;
2 26 Feb 07 jari 30 import java.util.zip.ZipFile;
2 26 Feb 07 jari 31
2 26 Feb 07 jari 32 import javax.swing.JFileChooser;
2 26 Feb 07 jari 33 import javax.swing.JFrame;
2 26 Feb 07 jari 34 import javax.swing.JOptionPane;
2 26 Feb 07 jari 35
2 26 Feb 07 jari 36 import org.tigr.microarray.mev.TMEV;
2 26 Feb 07 jari 37 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.DialogListener;
2 26 Feb 07 jari 38 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.HTMLMessageFileChooser;
2 26 Feb 07 jari 39 import org.tigr.microarray.mev.cluster.gui.impl.dialogs.Progress;
2 26 Feb 07 jari 40
2 26 Feb 07 jari 41 import ftp.FtpBean;
2 26 Feb 07 jari 42 import ftp.FtpListResult;
2 26 Feb 07 jari 43 import ftp.FtpObserver;
2 26 Feb 07 jari 44
2 26 Feb 07 jari 45 /**
2 26 Feb 07 jari 46  *
2 26 Feb 07 jari 47  * @author  braisted
2 26 Feb 07 jari 48  */
2 26 Feb 07 jari 49 public class EASEUpdateManager {
2 26 Feb 07 jari 50
2 26 Feb 07 jari 51   private String FTP_CONFIG_URL = "http://www.tm4.org/ease/ftp_config.txt";
2 26 Feb 07 jari 52
2 26 Feb 07 jari 53     private String FTP_SERVER;    
2 26 Feb 07 jari 54     private String REPOSITORY_ROOT;
2 26 Feb 07 jari 55     
2 26 Feb 07 jari 56   //private String ORGANISIM_LIST_FILE = "kingdom_species_cloneset_list.txt";
2 26 Feb 07 jari 57   private String UNIVERSAL_IMPLIES_FILE = "Implies_and_URL_data.zip";
2 26 Feb 07 jari 58     
2 26 Feb 07 jari 59     private JFrame frame;
2 26 Feb 07 jari 60     private Hashtable animalHash;
2 26 Feb 07 jari 61     private Hashtable plantHash;
2 26 Feb 07 jari 62     private Vector animalKeys;
2 26 Feb 07 jari 63     private Vector plantKeys;
2 26 Feb 07 jari 64   private String destLoc;
2 26 Feb 07 jari 65     private int BUFFERSIZE = 1024;
2 26 Feb 07 jari 66     private byte [] buffer;
2 26 Feb 07 jari 67     private Progress progress;
2 26 Feb 07 jari 68     private boolean needSeparateImpliesZip = true;
2 26 Feb 07 jari 69     private boolean okStatus = true;
2 26 Feb 07 jari 70     
2 26 Feb 07 jari 71     private ProgressListener listener;
2 26 Feb 07 jari 72     
2 26 Feb 07 jari 73     /** Creates a new instance of EaseUpdateManager */
2 26 Feb 07 jari 74     public EASEUpdateManager(JFrame parent) {        
2 26 Feb 07 jari 75         frame = parent;
2 26 Feb 07 jari 76         listener = new ProgressListener();
2 26 Feb 07 jari 77         
2 26 Feb 07 jari 78         //Initialize the progress bar
2 26 Feb 07 jari 79         progress = new Progress(parent, "Ease File Update Progress", listener);                
2 26 Feb 07 jari 80     }
2 26 Feb 07 jari 81     
2 26 Feb 07 jari 82     public void updateFiles() {
2 26 Feb 07 jari 83         //build the species and arrays lists using the supplied file.
2 26 Feb 07 jari 84         //these hashes will be used to build the selection dialog.
2 26 Feb 07 jari 85       
2 26 Feb 07 jari 86         try {
2 26 Feb 07 jari 87           
2 26 Feb 07 jari 88           //prepare progress to show repository config progress
2 26 Feb 07 jari 89             progress.setDescription("Retreving Repository Information");
2 26 Feb 07 jari 90             progress.setUnits(2);
2 26 Feb 07 jari 91             progress.show();
2 26 Feb 07 jari 92           
2 26 Feb 07 jari 93           //construct a vector of repository information hashes
2 26 Feb 07 jari 94             //each hash on the vector is a tab and repository
2 26 Feb 07 jari 95             //and contains properties for that repository
2 26 Feb 07 jari 96             //(sets progress 1 and 2 during execution)
2 26 Feb 07 jari 97             Vector tabPropertyHashes = getRepositoryInfo();
2 26 Feb 07 jari 98
2 26 Feb 07 jari 99             //prepare to visit repositories
2 26 Feb 07 jari 100             progress.setDescription("Visiting Repsoitory for Resource Checks");
2 26 Feb 07 jari 101             progress.setUnits(tabPropertyHashes.size());
2 26 Feb 07 jari 102
2 26 Feb 07 jari 103             //go to the repository, get a list of directories and
2 26 Feb 07 jari 104             //files under each directory
2 26 Feb 07 jari 105             //(This uses the list held by the repository (taxon-file)
2 26 Feb 07 jari 106             populateMenuHashes(tabPropertyHashes);
2 26 Feb 07 jari 107             
2 26 Feb 07 jari 108             progress.dispose();
2 26 Feb 07 jari 109             
2 26 Feb 07 jari 110             //if we have repository information and repository content
2 26 Feb 07 jari 111             //information (to populate the dialog), we are ready
2 26 Feb 07 jari 112             //to construct the dialog and get selected files
2 26 Feb 07 jari 113             
2 26 Feb 07 jari 114             if(okStatus) {          
2 26 Feb 07 jari 115               
2 26 Feb 07 jari 116               //construct dialog given properties for each dialog tab
2 26 Feb 07 jari 117               EASEFileUpdateDialog dialog = new EASEFileUpdateDialog(this.frame, tabPropertyHashes);
2 26 Feb 07 jari 118
2 26 Feb 07 jari 119               //if ok
2 26 Feb 07 jari 120               if(dialog.showModal() == JOptionPane.OK_OPTION) {
2 26 Feb 07 jari 121                 
2 26 Feb 07 jari 122                 //get the selected server, repository root, and implies zip name
2 26 Feb 07 jari 123                 this.FTP_SERVER = (String)(dialog.getRepositoryProperties().get("ftp-server"));
2 26 Feb 07 jari 124                 this.REPOSITORY_ROOT = (String)(dialog.getRepositoryProperties().get("base-dir"));
2 26 Feb 07 jari 125                 this.UNIVERSAL_IMPLIES_FILE = (String)(dialog.getRepositoryProperties().get("implies-zip"));
2 26 Feb 07 jari 126                 
2 26 Feb 07 jari 127                 //if implies files are already inside ease zip or does not exist clear boolean
2 26 Feb 07 jari 128                 if(this.UNIVERSAL_IMPLIES_FILE.equalsIgnoreCase("none"))
2 26 Feb 07 jari 129                   this.needSeparateImpliesZip = false;
2 26 Feb 07 jari 130                 
2 26 Feb 07 jari 131                 //start the thead to get the files based on species (**Folder name)
2 26 Feb 07 jari 132                 //and array name (file name)
2 26 Feb 07 jari 133                 //Note that we have already set the repository server name,
2 26 Feb 07 jari 134                 //the repository file root for ease files, and the optional implies zip file name
2 26 Feb 07 jari 135                 updateEaseFiles(dialog.getSpeciesName(), dialog.getArrayName());
2 26 Feb 07 jari 136               }
2 26 Feb 07 jari 137             }
2 26 Feb 07 jari 138             
2 26 Feb 07 jari 139         } catch (Exception e) {
2 26 Feb 07 jari 140           e.printStackTrace();
2 26 Feb 07 jari 141           System.out.print("Message"+e.getMessage());
2 26 Feb 07 jari 142           JOptionPane.showMessageDialog(frame, "<html>An error occurred when retrieving information on" +
2 26 Feb 07 jari 143             "available<br>species and clone set files.  Update request cannot be fulfilled.", "EASE Update Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 144             okStatus = false;
2 26 Feb 07 jari 145             progress.dispose();
2 26 Feb 07 jari 146         }      
2 26 Feb 07 jari 147     }
2 26 Feb 07 jari 148     
2 26 Feb 07 jari 149
2 26 Feb 07 jari 150     /**
2 26 Feb 07 jari 151      * Pulls the config file and parses the repository information
2 26 Feb 07 jari 152      * into a vector of repository properties Hashtables
2 26 Feb 07 jari 153      * @return
2 26 Feb 07 jari 154      */
2 26 Feb 07 jari 155     private Vector getRepositoryInfo() {
2 26 Feb 07 jari 156         //get the repository information from the repository config File            
2 26 Feb 07 jari 157       Vector repHashes = new Vector();
2 26 Feb 07 jari 158       
2 26 Feb 07 jari 159       try {
2 26 Feb 07 jari 160         URLConnection conn = new URL(FTP_CONFIG_URL).openConnection();                
2 26 Feb 07 jari 161         progress.setValue(1);
2 26 Feb 07 jari 162         
2 26 Feb 07 jari 163         //add repository property hashes to the vector
2 26 Feb 07 jari 164         repHashes = parseConfig(conn.getInputStream());      
2 26 Feb 07 jari 165
2 26 Feb 07 jari 166       } catch (Exception e) {
2 26 Feb 07 jari 167         e.printStackTrace();
2 26 Feb 07 jari 168       }
2 26 Feb 07 jari 169       
2 26 Feb 07 jari 170       //return the vector of repository hashes
2 26 Feb 07 jari 171       return repHashes;      
2 26 Feb 07 jari 172     }
2 26 Feb 07 jari 173     
2 26 Feb 07 jari 174     
2 26 Feb 07 jari 175     /**
2 26 Feb 07 jari 176      * Populates a vector of Hashtables that contain properties
2 26 Feb 07 jari 177      * about the availible ease repositories and how the dialog
2 26 Feb 07 jari 178      * should be convfigured
2 26 Feb 07 jari 179      * 
2 26 Feb 07 jari 180      * @param is the InputStream from the HTTP connection and config file
2 26 Feb 07 jari 181      * @return returns a Vector of Hashtables
2 26 Feb 07 jari 182      * @throws IOException
2 26 Feb 07 jari 183      */
2 26 Feb 07 jari 184     private Vector parseConfig(InputStream is) throws IOException{
2 26 Feb 07 jari 185       Vector hashVector = new Vector();
2 26 Feb 07 jari 186       
2 26 Feb 07 jari 187       BufferedReader br = new BufferedReader(new InputStreamReader(is));
2 26 Feb 07 jari 188       String [] keyValue;
2 26 Feb 07 jari 189       int numTabs = 0;
2 26 Feb 07 jari 190       
2 26 Feb 07 jari 191       Hashtable currHash = null;
2 26 Feb 07 jari 192       String line;
2 26 Feb 07 jari 193
2 26 Feb 07 jari 194       //loop through the file to parse into 
2 26 Feb 07 jari 195       while((line = br.readLine())!= null) {
2 26 Feb 07 jari 196         
2 26 Feb 07 jari 197         //comment line
2 26 Feb 07 jari 198         if(line.startsWith("#"))
2 26 Feb 07 jari 199           continue;
2 26 Feb 07 jari 200
2 26 Feb 07 jari 201         keyValue = line.split("\t");
2 26 Feb 07 jari 202
2 26 Feb 07 jari 203         //"tab-label" starts a new record
2 26 Feb 07 jari 204         if(keyValue[0].equals("tab-label")) {
2 26 Feb 07 jari 205           
2 26 Feb 07 jari 206           //on second or higher tab, add current hash to vector
2 26 Feb 07 jari 207           //else it's the first
2 26 Feb 07 jari 208           if(numTabs > 0)
2 26 Feb 07 jari 209             hashVector.add(currHash);
2 26 Feb 07 jari 210           currHash = new Hashtable();
2 26 Feb 07 jari 211           numTabs++;
2 26 Feb 07 jari 212         }
2 26 Feb 07 jari 213         //add the current property
2 26 Feb 07 jari 214         currHash.put(keyValue[0], keyValue[1]);
2 26 Feb 07 jari 215       }
2 26 Feb 07 jari 216       //add the last currHash to vector
2 26 Feb 07 jari 217       hashVector.add(currHash);
2 26 Feb 07 jari 218
2 26 Feb 07 jari 219       progress.setValue(2);
2 26 Feb 07 jari 220       
2 26 Feb 07 jari 221       return hashVector;
2 26 Feb 07 jari 222     }
2 26 Feb 07 jari 223
2 26 Feb 07 jari 224     /**
2 26 Feb 07 jari 225      * Go to each repository (or tab in the dialog) and get menu information
2 26 Feb 07 jari 226      * @param tabHashes Vector of Repository Properties
2 26 Feb 07 jari 227      */
2 26 Feb 07 jari 228     private void populateMenuHashes(Vector tabHashes) {      
2 26 Feb 07 jari 229       for(int i  = 0; i < tabHashes.size(); i++) {
2 26 Feb 07 jari 230         getMenuInfo((Hashtable)tabHashes.get(i));
2 26 Feb 07 jari 231         progress.setValue(i+1);
2 26 Feb 07 jari 232       }      
2 26 Feb 07 jari 233     }
2 26 Feb 07 jari 234     
2 26 Feb 07 jari 235     /**
2 26 Feb 07 jari 236      * Vist repository servers and get information about availible
2 26 Feb 07 jari 237      * directories and files under each directory
2 26 Feb 07 jari 238      * @param tabHash a set of parameters for that repository
2 26 Feb 07 jari 239      * (equivalent to a 'tab' on the dialog, hense 'tabHash)
2 26 Feb 07 jari 240      */
2 26 Feb 07 jari 241     private void getMenuInfo(Hashtable tabHash) {
2 26 Feb 07 jari 242       String server = (String)tabHash.get("ftp-server");
2 26 Feb 07 jari 243       String baseLoc = (String)tabHash.get("base-dir");
2 26 Feb 07 jari 244       String orgFile = (String)tabHash.get("taxon-file");
2 26 Feb 07 jari 245       String label = (String)tabHash.get("tab-label");
2 26 Feb 07 jari 246       String text;
2 26 Feb 07 jari 247       
2 26 Feb 07 jari 248       //to construct directory keys
2 26 Feb 07 jari 249       Vector upperLevelKeys = new Vector();
2 26 Feb 07 jari 250       //for each directory there will be a set of files
2 26 Feb 07 jari 251       //entries in this hash map directory key to a vector of file names
2 26 Feb 07 jari 252       Hashtable upperToLowerHash = new Hashtable();
2 26 Feb 07 jari 253       
2 26 Feb 07 jari 254       try {
2 26 Feb 07 jari 255         
2 26 Feb 07 jari 256         //connect, grab the small content
2 26 Feb 07 jari 257         //(No listener due to the small size, no need for progress)
2 26 Feb 07 jari 258         FtpBean ftp = new FtpBean();            
2 26 Feb 07 jari 259         ftp.ftpConnect(server, "anonymous");
2 26 Feb 07 jari 260         byte [] content = ftp.getBinaryFile(baseLoc+orgFile);
2 26 Feb 07 jari 261         text = new String(content);
2 26 Feb 07 jari 262         ftp.close();
2 26 Feb 07 jari 263         
2 26 Feb 07 jari 264         //break on lines
2 26 Feb 07 jari 265         StringTokenizer stok = new StringTokenizer(text, "\n");
2 26 Feb 07 jari 266         StringTokenizer stok2;
2 26 Feb 07 jari 267         
2 26 Feb 07 jari 268         String tabName, upperLabel, lowerLabel;
2 26 Feb 07 jari 269         while(stok.hasMoreElements()) {
2 26 Feb 07 jari 270
2 26 Feb 07 jari 271           //break lines on tabs
2 26 Feb 07 jari 272           stok2 = new StringTokenizer((String)(stok.nextElement()), "\t");
2 26 Feb 07 jari 273           
2 26 Feb 07 jari 274           //make sure you have enough tokens to play!!! :)
2 26 Feb 07 jari 275           if(stok2.countTokens() == 3) {
2 26 Feb 07 jari 276             tabName = (String)stok2.nextToken();
2 26 Feb 07 jari 277             
2 26 Feb 07 jari 278             //ignore if not for this tab... wasteful but simple to do
2 26 Feb 07 jari 279             if(!tabName.equalsIgnoreCase(label))
2 26 Feb 07 jari 280               continue;
2 26 Feb 07 jari 281             
2 26 Feb 07 jari 282             //grab directory and file (upper and lower level in folder...)
2 26 Feb 07 jari 283             upperLabel = (String)stok2.nextToken();
2 26 Feb 07 jari 284             lowerLabel = (String)stok2.nextToken();
2 26 Feb 07 jari 285             
2 26 Feb 07 jari 286             //solve dos2unix problem if it exists
2 26 Feb 07 jari 287             lowerLabel = lowerLabel.trim();
2 26 Feb 07 jari 288
2 26 Feb 07 jari 289             //if it's a new directory add it
2 26 Feb 07 jari 290             if(!upperLevelKeys.contains(upperLabel))
2 26 Feb 07 jari 291               upperLevelKeys.add(upperLabel);
2 26 Feb 07 jari 292             
2 26 Feb 07 jari 293             //if the hash already has the directory grab its file vector
2 26 Feb 07 jari 294             //and add, else make a new vector, add file, put vector into hash
2 26 Feb 07 jari 295             if(upperToLowerHash.containsKey(upperLabel)) {
2 26 Feb 07 jari 296               ((Vector)(upperToLowerHash.get(upperLabel))).add(lowerLabel);
2 26 Feb 07 jari 297             } else {
2 26 Feb 07 jari 298               Vector fileVector = new Vector();
2 26 Feb 07 jari 299               fileVector.add(lowerLabel);
2 26 Feb 07 jari 300               upperToLowerHash.put(upperLabel, fileVector);
2 26 Feb 07 jari 301             }
2 26 Feb 07 jari 302             
2 26 Feb 07 jari 303           } else {
2 26 Feb 07 jari 304             continue;
2 26 Feb 07 jari 305           }
2 26 Feb 07 jari 306         }
2 26 Feb 07 jari 307         
2 26 Feb 07 jari 308         //add keys and hash to the tab hash 
2 26 Feb 07 jari 309         tabHash.put("main-keys", upperLevelKeys);
2 26 Feb 07 jari 310         tabHash.put("menu-hash", upperToLowerHash);
2 26 Feb 07 jari 311         
2 26 Feb 07 jari 312       } catch (Exception e) {
2 26 Feb 07 jari 313         e.printStackTrace();
2 26 Feb 07 jari 314         System.out.print("Message"+e.getMessage());
2 26 Feb 07 jari 315         JOptionPane.showMessageDialog(frame, "<html>An error occurred when retrieving information on" +
2 26 Feb 07 jari 316             "available<br>species and clone set files.  Update request cannot be fulfilled.", "EASE Update Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 317         okStatus = false;
2 26 Feb 07 jari 318         progress.dispose();
2 26 Feb 07 jari 319       }
2 26 Feb 07 jari 320       
2 26 Feb 07 jari 321     }
2 26 Feb 07 jari 322     
2 26 Feb 07 jari 323     
2 26 Feb 07 jari 324     
2 26 Feb 07 jari 325     /** Kicks off the thread to update the file system given species and array
2 26 Feb 07 jari 326      */ 
2 26 Feb 07 jari 327     private void updateEaseFiles(String species, String array) {
2 26 Feb 07 jari 328         Thread thread = new Thread(new Runner(species, array));
2 26 Feb 07 jari 329         thread.start();
2 26 Feb 07 jari 330     }
2 26 Feb 07 jari 331     
2 26 Feb 07 jari 332     
2 26 Feb 07 jari 333     /** retrievs the File object to receive the file system download
2 26 Feb 07 jari 334      */
2 26 Feb 07 jari 335     private File getOutputFileLocation() {
2 26 Feb 07 jari 336         File file = null;
2 26 Feb 07 jari 337         
2 26 Feb 07 jari 338         String msg = "<html><center><h1>EASE Destination Selection</h1></center>";
2 26 Feb 07 jari 339         msg += "Use this interface to select the location for zip file download and extraction. ";
2 26 Feb 07 jari 340         msg += "Note that the ease directory will be labeled as \"ease_\" followed by the array name.<br><br>";
2 26 Feb 07 jari 341         msg += "When running EASE please use the button on the first dialog page to specify this folder as the base file ";
2 26 Feb 07 jari 342         msg += "system for EASE analysis. ";
2 26 Feb 07 jari 343         msg += "This will become the default location for EASE annoation information.";
2 26 Feb 07 jari 344         msg += "</html>";
2 26 Feb 07 jari 345         
2 26 Feb 07 jari 346         HTMLMessageFileChooser chooser = new HTMLMessageFileChooser(frame, "EASE File Update Location", msg, TMEV.getFile("data"), true);
2 26 Feb 07 jari 347         JFileChooser baseChooser = chooser.getFileChooser();
2 26 Feb 07 jari 348         baseChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
2 26 Feb 07 jari 349         chooser.setSize(550, 550);
2 26 Feb 07 jari 350         if(chooser.showModal() == JOptionPane.OK_OPTION) {
2 26 Feb 07 jari 351             file = chooser.getSelectedFile();
2 26 Feb 07 jari 352         }
2 26 Feb 07 jari 353         return file;
2 26 Feb 07 jari 354     }
2 26 Feb 07 jari 355     
2 26 Feb 07 jari 356     /** Controls the update process by calling for downloads and extractions
2 26 Feb 07 jari 357      */
2 26 Feb 07 jari 358     private void getBaseFiles(String species, String array) {
2 26 Feb 07 jari 359    
2 26 Feb 07 jari 360       boolean pass = true;
2 26 Feb 07 jari 361         
2 26 Feb 07 jari 362         File outputFile = getOutputFileLocation();
2 26 Feb 07 jari 363         if(outputFile == null)
2 26 Feb 07 jari 364             return;
2 26 Feb 07 jari 365         
2 26 Feb 07 jari 366         File baseDir = new File(outputFile.getAbsolutePath()+"/ease_"+array);
2 26 Feb 07 jari 367         baseDir.mkdir();
2 26 Feb 07 jari 368         
2 26 Feb 07 jari 369         outputFile = new File(baseDir.getAbsolutePath()+"/"+array+"_EASE.zip");
2 26 Feb 07 jari 370         
2 26 Feb 07 jari 371         progress.setTitle("EASE File Download");
2 26 Feb 07 jari 372         progress.setDescription("Download Base Zip File");        
2 26 Feb 07 jari 373         progress.show();
2 26 Feb 07 jari 374         pass = downloadFile(species, array+"_EASE.zip", outputFile);
2 26 Feb 07 jari 375         if(pass)
2 26 Feb 07 jari 376             pass = extractZipFile(outputFile);
2 26 Feb 07 jari 377         
2 26 Feb 07 jari 378         if(this.needSeparateImpliesZip) {
2 26 Feb 07 jari 379           String classDir = baseDir+"/Data/Class/Implies_and_URL_data.zip";
2 26 Feb 07 jari 380           outputFile = new File(classDir);
2 26 Feb 07 jari 381           
2 26 Feb 07 jari 382           progress.setDescription("Download Implies and URL Data File");
2 26 Feb 07 jari 383           progress.setValue(0);
2 26 Feb 07 jari 384           if(pass)
2 26 Feb 07 jari 385             pass = downloadFile("", UNIVERSAL_IMPLIES_FILE, outputFile);
2 26 Feb 07 jari 386           if(pass)
2 26 Feb 07 jari 387             pass = extractZipFile(outputFile);          
2 26 Feb 07 jari 388         }
2 26 Feb 07 jari 389
2 26 Feb 07 jari 390         progress.dispose();
2 26 Feb 07 jari 391         
2 26 Feb 07 jari 392         if(pass)
2 26 Feb 07 jari 393             JOptionPane.showMessageDialog(frame, "The EASE file system update is complete.", "EASE File System Update", JOptionPane.INFORMATION_MESSAGE);
2 26 Feb 07 jari 394         else
2 26 Feb 07 jari 395             JOptionPane.showMessageDialog(frame, "The EASE file system update was terminated due to the reported error.", "EASE File System Update", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 396     }
2 26 Feb 07 jari 397
2 26 Feb 07 jari 398     
2 26 Feb 07 jari 399     /** Downloads the file at sourceURL to output file (dest), returns true if successful
2 26 Feb 07 jari 400      */
2 26 Feb 07 jari 401     private boolean downloadFile(String species, String slide, File dest) {//String sourceURL, File dest) {
2 26 Feb 07 jari 402         BufferedInputStream bis;
2 26 Feb 07 jari 403         BufferedOutputStream bos;
2 26 Feb 07 jari 404         
2 26 Feb 07 jari 405         //buffer = new byte [BUFFERSIZE];
2 26 Feb 07 jari 406         int length = 0;
2 26 Feb 07 jari 407         
2 26 Feb 07 jari 408         try {
2 26 Feb 07 jari 409
2 26 Feb 07 jari 410             int overallLength = 0;            
2 26 Feb 07 jari 411       int currentLength = 0;            
2 26 Feb 07 jari 412       progress.setValue(0);
2 26 Feb 07 jari 413
2 26 Feb 07 jari 414
2 26 Feb 07 jari 415           FtpBean ftp = new FtpBean();
2 26 Feb 07 jari 416           ftp.ftpConnect(FTP_SERVER, "anonymous");
2 26 Feb 07 jari 417           ftp.setDirectory(REPOSITORY_ROOT+"/"+species);          
2 26 Feb 07 jari 418           FtpListResult list = ftp.getDirectoryContent();
2 26 Feb 07 jari 419           
2 26 Feb 07 jari 420           while(list.next()) {    
2 26 Feb 07 jari 421             if(list.getName().equals(slide)) {
2 26 Feb 07 jari 422               overallLength = (int)list.getSize();
2 26 Feb 07 jari 423             }
2 26 Feb 07 jari 424           }
2 26 Feb 07 jari 425
2 26 Feb 07 jari 426           progress.setUnits(overallLength);
2 26 Feb 07 jari 427           listener.reset();
2 26 Feb 07 jari 428           listener.setMax(overallLength);
2 26 Feb 07 jari 429           
2 26 Feb 07 jari 430             bos = new BufferedOutputStream(new FileOutputStream(dest));          
2 26 Feb 07 jari 431             //get binary file to byte array, use listener
2 26 Feb 07 jari 432             bos.write(ftp.getBinaryFile(slide, listener), 0, overallLength);
2 26 Feb 07 jari 433                 
2 26 Feb 07 jari 434             bos.flush();
2 26 Feb 07 jari 435             bos.close();
2 26 Feb 07 jari 436             ftp.close();
2 26 Feb 07 jari 437         } catch (Exception ioe) {
2 26 Feb 07 jari 438             progress.dispose();
2 26 Feb 07 jari 439             ioe.printStackTrace();
2 26 Feb 07 jari 440             JOptionPane.showMessageDialog(frame, "<html>An Error occured when downloading "+species+".<br>The update request cannot be fulfilled.</html>", "EASE Update Download Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 441             return false;
2 26 Feb 07 jari 442         }
2 26 Feb 07 jari 443         return true;
2 26 Feb 07 jari 444     }
2 26 Feb 07 jari 445     
2 26 Feb 07 jari 446     /** Extracts the specified zip file, returns true is successful
2 26 Feb 07 jari 447      */
2 26 Feb 07 jari 448     private boolean extractZipFile(File outputFile) {
2 26 Feb 07 jari 449         BufferedInputStream bis;
2 26 Feb 07 jari 450         BufferedOutputStream bos;
2 26 Feb 07 jari 451
2 26 Feb 07 jari 452         progress.setTitle("Extracting zip file");
2 26 Feb 07 jari 453         progress.setDescription("Extracting zip file: "+outputFile.getAbsolutePath());
2 26 Feb 07 jari 454         
2 26 Feb 07 jari 455         try {
2 26 Feb 07 jari 456             ZipFile zipFile = new ZipFile(outputFile);
2 26 Feb 07 jari 457             progress.setUnits(zipFile.size());
2 26 Feb 07 jari 458             
2 26 Feb 07 jari 459             Enumeration entries = zipFile.entries();
2 26 Feb 07 jari 460             File baseDir = outputFile.getParentFile();
2 26 Feb 07 jari 461             byte [] buffer = new byte [BUFFERSIZE];
2 26 Feb 07 jari 462             int length = 0;
2 26 Feb 07 jari 463             int cnt = 0;
2 26 Feb 07 jari 464             
2 26 Feb 07 jari 465             while(entries.hasMoreElements()) {
2 26 Feb 07 jari 466                 
2 26 Feb 07 jari 467                 progress.setValue(cnt);
2 26 Feb 07 jari 468                 
2 26 Feb 07 jari 469                 ZipEntry entry = (ZipEntry)entries.nextElement();
2 26 Feb 07 jari 470                 
2 26 Feb 07 jari 471                 if(entry.isDirectory()) {
2 26 Feb 07 jari 472                     cnt++;
2 26 Feb 07 jari 473                     continue;
2 26 Feb 07 jari 474                 }
2 26 Feb 07 jari 475                 
2 26 Feb 07 jari 476                 String entryName = entry.getName();
2 26 Feb 07 jari 477                 String entryFolder = (new File(entryName)).getParent();
2 26 Feb 07 jari 478                 File entryDirectory = new File(baseDir.getAbsolutePath()+"/"+entryFolder);
2 26 Feb 07 jari 479
2 26 Feb 07 jari 480                 if(!entryDirectory.exists()) {
2 26 Feb 07 jari 481                     entryDirectory.mkdirs();
2 26 Feb 07 jari 482                 }
2 26 Feb 07 jari 483
2 26 Feb 07 jari 484                 bos = new BufferedOutputStream(new FileOutputStream(baseDir.getAbsolutePath()+"/"+entry.getName()));
2 26 Feb 07 jari 485                 bis = new BufferedInputStream(zipFile.getInputStream(entry));
2 26 Feb 07 jari 486                 
2 26 Feb 07 jari 487                 while( (length = bis.read(buffer, 0, BUFFERSIZE)) > 0 ) {
2 26 Feb 07 jari 488                     bos.write(buffer, 0, length);
2 26 Feb 07 jari 489                 }
2 26 Feb 07 jari 490                 
2 26 Feb 07 jari 491                 cnt++;
2 26 Feb 07 jari 492                 bos.flush();
2 26 Feb 07 jari 493                 bos.close();
2 26 Feb 07 jari 494                 bis.close();
2 26 Feb 07 jari 495             }
2 26 Feb 07 jari 496         } catch (Exception e) {
2 26 Feb 07 jari 497             progress.dispose();
2 26 Feb 07 jari 498             e.printStackTrace();
2 26 Feb 07 jari 499             JOptionPane.showMessageDialog(frame, "<html>An Error occured when extracting "+outputFile.getAbsolutePath()+".<br>The update request cannot be fulfilled.</html>", "EASE Update Download Error", JOptionPane.ERROR_MESSAGE);
2 26 Feb 07 jari 500             return false;
2 26 Feb 07 jari 501         }
2 26 Feb 07 jari 502         return true;
2 26 Feb 07 jari 503     }
2 26 Feb 07 jari 504     
2 26 Feb 07 jari 505     public static void main(String [] args) {
2 26 Feb 07 jari 506         EASEUpdateManager manager = new EASEUpdateManager(new JFrame());
2 26 Feb 07 jari 507         manager.updateFiles();
2 26 Feb 07 jari 508     }
2 26 Feb 07 jari 509     
2 26 Feb 07 jari 510     private class Runner implements Runnable {
2 26 Feb 07 jari 511         private String species;
2 26 Feb 07 jari 512         private String array;
2 26 Feb 07 jari 513         
2 26 Feb 07 jari 514         public Runner(String species, String array) {
2 26 Feb 07 jari 515             this.species = species;
2 26 Feb 07 jari 516             this.array = array;
2 26 Feb 07 jari 517         }
2 26 Feb 07 jari 518         
2 26 Feb 07 jari 519         public void run() {
2 26 Feb 07 jari 520             getBaseFiles(species, array);
2 26 Feb 07 jari 521         }        
2 26 Feb 07 jari 522     }
2 26 Feb 07 jari 523     
2 26 Feb 07 jari 524     
2 26 Feb 07 jari 525     /**
2 26 Feb 07 jari 526      * The class to listen to progress, monitor and algorithms events.
2 26 Feb 07 jari 527      */
2 26 Feb 07 jari 528     private class ProgressListener extends DialogListener implements WindowListener, FtpObserver {
2 26 Feb 07 jari 529
2 26 Feb 07 jari 530       private int maxProgress = 0;
2 26 Feb 07 jari 531       private int currProgress = 0;
2 26 Feb 07 jari 532       
2 26 Feb 07 jari 533       public void setMax(int max) {
2 26 Feb 07 jari 534         maxProgress = 0;
2 26 Feb 07 jari 535       }
2 26 Feb 07 jari 536       
2 26 Feb 07 jari 537       public void reset() {
2 26 Feb 07 jari 538         maxProgress = 0;
2 26 Feb 07 jari 539         currProgress = 0;
2 26 Feb 07 jari 540       }
2 26 Feb 07 jari 541       
2 26 Feb 07 jari 542         public void actionPerformed(ActionEvent e) {
2 26 Feb 07 jari 543             String command = e.getActionCommand();
2 26 Feb 07 jari 544             if (command.equals("cancel-command")) {                
2 26 Feb 07 jari 545                 progress.dispose();                
2 26 Feb 07 jari 546             }
2 26 Feb 07 jari 547         }
2 26 Feb 07 jari 548         
2 26 Feb 07 jari 549         public void windowClosing(WindowEvent e) {
2 26 Feb 07 jari 550             progress.dispose();
2 26 Feb 07 jari 551         }
2 26 Feb 07 jari 552
2 26 Feb 07 jari 553     /* (non-Javadoc)
2 26 Feb 07 jari 554      * @see ftp.FtpObserver#byteRead(int)
2 26 Feb 07 jari 555      */
2 26 Feb 07 jari 556     public void byteRead(int bytes) {
2 26 Feb 07 jari 557       currProgress += bytes;      
2 26 Feb 07 jari 558       if( progress != null)
2 26 Feb 07 jari 559         progress.setValue(currProgress);
2 26 Feb 07 jari 560     
2 26 Feb 07 jari 561         
2 26 Feb 07 jari 562     }
2 26 Feb 07 jari 563
2 26 Feb 07 jari 564     /* (non-Javadoc)
2 26 Feb 07 jari 565      * @see ftp.FtpObserver#byteWrite(int)
2 26 Feb 07 jari 566      */
2 26 Feb 07 jari 567     public void byteWrite(int bytes) {
2 26 Feb 07 jari 568       currProgress += bytes;
2 26 Feb 07 jari 569       if( progress != null)
2 26 Feb 07 jari 570         progress.setValue(bytes);      
2 26 Feb 07 jari 571     }        
2 26 Feb 07 jari 572     }
2 26 Feb 07 jari 573
2 26 Feb 07 jari 574
2 26 Feb 07 jari 575 }