plugins/base2/uk.ac.scri.batchimporter/trunk/src/sbrn/base/ProgressMonitorThread.java

Code
Comments
Other
Rev Date Author Line
226 09 Jan 07 mbayer 1 /*
226 09 Jan 07 mbayer 2 Copyright (C) Authors contributing to this file. 
226 09 Jan 07 mbayer 3
226 09 Jan 07 mbayer 4 This program is free software; you can redistribute it and/or modify
226 09 Jan 07 mbayer 5 it under the terms of the GNU General Public License as published by
226 09 Jan 07 mbayer 6 the Free Software Foundation; either version 2 of the License, or
226 09 Jan 07 mbayer 7 (at your option) any later version.
226 09 Jan 07 mbayer 8
226 09 Jan 07 mbayer 9 This program is distributed in the hope that it will be useful,
226 09 Jan 07 mbayer 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
226 09 Jan 07 mbayer 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
226 09 Jan 07 mbayer 12 GNU General Public License for more details.
226 09 Jan 07 mbayer 13
226 09 Jan 07 mbayer 14 You should have received a copy of the GNU General Public License along
226 09 Jan 07 mbayer 15 with this program; if not, write to the Free Software Foundation, Inc.,
226 09 Jan 07 mbayer 16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
226 09 Jan 07 mbayer 17
226 09 Jan 07 mbayer 18 See also http://www.gnu.org/licenses/gpl.txt. 
226 09 Jan 07 mbayer 19 */
226 09 Jan 07 mbayer 20 package sbrn.base;
226 09 Jan 07 mbayer 21
226 09 Jan 07 mbayer 22 import net.sf.basedb.core.Job;
226 09 Jan 07 mbayer 23 import net.sf.basedb.core.ProgressReporter;
226 09 Jan 07 mbayer 24 import net.sf.basedb.core.SimpleProgressReporter;
226 09 Jan 07 mbayer 25
226 09 Jan 07 mbayer 26 /**
226 09 Jan 07 mbayer 27 This thread class monitors the progress of a BatchDataImport into BASE2. It works out the overall job progress
226 09 Jan 07 mbayer 28  based on the number of individual file imports that make up this batch import and their respective status. 
226 09 Jan 07 mbayer 29
226 09 Jan 07 mbayer 30 @author Micha Bayer - Plant Bioinformatics Group, Scottish Crop Research Institute
226 09 Jan 07 mbayer 31   email: sbrn@scri.ac.uk, web http://www.scri.ac.uk
226 09 Jan 07 mbayer 32  */
226 09 Jan 07 mbayer 33 public class ProgressMonitorThread extends Thread
226 09 Jan 07 mbayer 34 {
226 09 Jan 07 mbayer 35   
226 09 Jan 07 mbayer 36 //=========================================vars===============================  
226 09 Jan 07 mbayer 37   
226 09 Jan 07 mbayer 38   //the progress reporter for the overall  batch import
226 09 Jan 07 mbayer 39   private ProgressReporter progress = null;
226 09 Jan 07 mbayer 40   
226 09 Jan 07 mbayer 41   //the current file count
226 09 Jan 07 mbayer 42   private int count;   
226 09 Jan 07 mbayer 43   
226 09 Jan 07 mbayer 44   //the total number of files to be imported
226 09 Jan 07 mbayer 45   private double numFiles;
226 09 Jan 07 mbayer 46   
226 09 Jan 07 mbayer 47   //the BatchDataImport object that started this thread
226 09 Jan 07 mbayer 48   private BatchDataImport bdi = null;
226 09 Jan 07 mbayer 49   
226 09 Jan 07 mbayer 50   //the progress reporter for the current subjob
226 09 Jan 07 mbayer 51   private SimpleProgressReporter pr = null;
226 09 Jan 07 mbayer 52     
226 09 Jan 07 mbayer 53 //  =========================================c'tor===============================    
226 09 Jan 07 mbayer 54
226 09 Jan 07 mbayer 55   public ProgressMonitorThread(ProgressReporter progress,double numFiles,BatchDataImport bdi)
226 09 Jan 07 mbayer 56   {
226 09 Jan 07 mbayer 57     this.progress = progress;
226 09 Jan 07 mbayer 58     this.numFiles = numFiles;
226 09 Jan 07 mbayer 59     this.bdi = bdi;
226 09 Jan 07 mbayer 60   }
226 09 Jan 07 mbayer 61   
226 09 Jan 07 mbayer 62 //  ==================================methods===============================  
226 09 Jan 07 mbayer 63   /**
226 09 Jan 07 mbayer 64   Starts the progress monitoring thread. Completes when the job has run to completion or a subjob has failed.
226 09 Jan 07 mbayer 65   */
226 09 Jan 07 mbayer 66   public void run()
226 09 Jan 07 mbayer 67   {    
226 09 Jan 07 mbayer 68     int overallPercentComplete = 0;
226 09 Jan 07 mbayer 69     boolean finished = false;
226 09 Jan 07 mbayer 70     
226 09 Jan 07 mbayer 71     //update the progress every 5 seconds
226 09 Jan 07 mbayer 72     while(!finished)
226 09 Jan 07 mbayer 73     {
226 09 Jan 07 mbayer 74       //check current job status
226 09 Jan 07 mbayer 75       Job currentJob = bdi.getCurrentJob();
226 09 Jan 07 mbayer 76       if(currentJob!=null)
226 09 Jan 07 mbayer 77       {  
226 09 Jan 07 mbayer 78         //calculate and display the current percent complete
226 09 Jan 07 mbayer 79         overallPercentComplete = getCurrentPercentComplete();        
226 09 Jan 07 mbayer 80         progress.display(overallPercentComplete, null);
226 09 Jan 07 mbayer 81         
226 09 Jan 07 mbayer 82         //check the status of the current job
226 09 Jan 07 mbayer 83         Job.Status status = currentJob.getStatus();
226 09 Jan 07 mbayer 84         if((overallPercentComplete==100) || status.equals(Job.Status.DONE) || status.equals(Job.Status.ERROR))
226 09 Jan 07 mbayer 85         {
226 09 Jan 07 mbayer 86           finished = true;
226 09 Jan 07 mbayer 87         }
226 09 Jan 07 mbayer 88       }
226 09 Jan 07 mbayer 89       
226 09 Jan 07 mbayer 90       try{Thread.sleep(5000);}catch(InterruptedException x){}    
226 09 Jan 07 mbayer 91     }
226 09 Jan 07 mbayer 92   }
226 09 Jan 07 mbayer 93   
226 09 Jan 07 mbayer 94 //----------------------------------------------------------------------------------------------------------------------------------  
226 09 Jan 07 mbayer 95   /**
226 09 Jan 07 mbayer 96   Works out the overall precentage complete of this batch data import.
226 09 Jan 07 mbayer 97   @return the current overall precent complete for the entire batch import (not an individual subjob)
226 09 Jan 07 mbayer 98   */
226 09 Jan 07 mbayer 99   private int getCurrentPercentComplete()
226 09 Jan 07 mbayer 100   {
226 09 Jan 07 mbayer 101     //the percentage each file import contributes to the overall job duration
226 09 Jan 07 mbayer 102     //assumes they're all roughly the same size
226 09 Jan 07 mbayer 103     double filePercentage = 100/numFiles;
226 09 Jan 07 mbayer 104     
226 09 Jan 07 mbayer 105     //the percent complete of the current file import
226 09 Jan 07 mbayer 106     double currentJobPercentComplete = 0;
226 09 Jan 07 mbayer 107     if(pr!=null)
226 09 Jan 07 mbayer 108     {
226 09 Jan 07 mbayer 109       currentJobPercentComplete = pr.getPercent();
226 09 Jan 07 mbayer 110     }
226 09 Jan 07 mbayer 111
226 09 Jan 07 mbayer 112     //the overall progress contributed by all files finished so far
226 09 Jan 07 mbayer 113     double done = count*filePercentage;      
226 09 Jan 07 mbayer 114     
226 09 Jan 07 mbayer 115     //the progress attributable to the currently running file import
226 09 Jan 07 mbayer 116     double currentPogress = (currentJobPercentComplete/100)*filePercentage;
226 09 Jan 07 mbayer 117     
226 09 Jan 07 mbayer 118     //the total progress of this batch import so far
226 09 Jan 07 mbayer 119     int overallPercentComplete = (int)(done+currentPogress);  
226 09 Jan 07 mbayer 120     if(numFiles == count)
226 09 Jan 07 mbayer 121     {
226 09 Jan 07 mbayer 122       overallPercentComplete = 100;
226 09 Jan 07 mbayer 123     }  
226 09 Jan 07 mbayer 124     
226 09 Jan 07 mbayer 125     return overallPercentComplete;
226 09 Jan 07 mbayer 126   }
226 09 Jan 07 mbayer 127   
226 09 Jan 07 mbayer 128 //  ==================================accessors===============================    
226 09 Jan 07 mbayer 129
226 09 Jan 07 mbayer 130   public void setCount(int count)
226 09 Jan 07 mbayer 131   {
226 09 Jan 07 mbayer 132     this.count = count;
226 09 Jan 07 mbayer 133   }
226 09 Jan 07 mbayer 134
226 09 Jan 07 mbayer 135   public void setPr(SimpleProgressReporter pr)
226 09 Jan 07 mbayer 136   {
226 09 Jan 07 mbayer 137     this.pr = pr;
226 09 Jan 07 mbayer 138   }
226 09 Jan 07 mbayer 139
226 09 Jan 07 mbayer 140 //  ----------------------------------------------------------------------------------------------------------------------------------  
226 09 Jan 07 mbayer 141
226 09 Jan 07 mbayer 142 }//end class