Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

GeneralUtilities.java

00001 package ProdDatabase;
00002 import java.util.*;
00003 import java.sql.*;
00004 import java.util.zip.*;
00005 import java.util.regex.*;
00006 import java.io.*;
00007 import Preferences.*;
00008 
00009 public class GeneralUtilities implements SCTDBInfo {
00010     public static Hashtable generalHash;
00011     public static Vector testNumberList;
00012 
00013 // *****************************************************************************************************************
00014     public static void itemExists(String sn1,String sn2,boolean resetHash) throws Exception {
00015       if(resetHash || generalHash==null) generalHash = new Hashtable();
00016       Vector itemList = new Vector();
00017       StringBuffer sqlStat = new StringBuffer("SELECT ");
00018       sqlStat.append("ser_no,ctype,locn_name FROM items WHERE ser_no >="+sn1+" AND ser_no <="+sn2);
00019       sqlStat.append(" ORDER BY ser_no");
00020 //      System.out.println(sqlStat.toString());
00021 
00022       SCTDBInterface db = SCTDBInterface.getInstance();
00023 
00024       Statement statement = db.connection.createStatement();
00025       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00026       int recordCount=0;
00027       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00028          recordCount++;
00029          String serno = resultSet.getString(1);
00030           String ctype = resultSet.getString(2);
00031           String location = resultSet.getString(3);
00032           generalHash.put((String)serno,(String)ctype);
00033           System.out.println(serno+"("+ctype+") already exists, located at "+location);
00034           }
00035       statement.close();
00036       return;
00037       }
00038   public static boolean itemExists(String sn1) throws Exception {
00039        return itemExists(sn1,true);
00040        }
00041 //************************************************************
00042   public static boolean itemExists(String sn1, boolean showDialog) throws Exception {
00043      boolean itExists=false;
00044      String cType=null;
00045      StringBuffer sqlStat = new StringBuffer("SELECT ctype FROM items WHERE ser_no="+sn1);
00046       Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00047       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00048       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00049          cType=resultSet.getString(1);
00050          if(cType.indexOf("Module")!=-1) itExists=true;
00051          }
00052       statement.close();
00053       if(!itExists && showDialog) {
00054          if(cType==null) javax.swing.JOptionPane.showMessageDialog(null,sn1+" does not exist in the database!","No such device",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00055          else  javax.swing.JOptionPane.showMessageDialog(null,sn1+" is a "+cType+" (not a module)","Not a module",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00056          }
00057       return itExists;
00058       }
00059          
00060 
00061 // *****************************************************************************************************************
00062     public static String validateLocation(String theLocation) throws Exception {
00063 // get location name where argument = ulocation name OR institute username OR institute user number
00064       String validatedLocation="unknown";
00065       boolean isNumber = true;
00066       try {
00067          int locationNumber = Integer.parseInt(theLocation);
00068       }catch(Exception nException) {isNumber=false;}
00069 
00070       StringBuffer sqlStat = new StringBuffer("SELECT locn_name FROM LOCNS");
00071       if(isNumber) sqlStat.append(" WHERE locn="+theLocation);
00072       else sqlStat.append(" WHERE (locn_name='"+theLocation+"' OR username='"+theLocation+"')");
00073 
00074       SCTDBInterface db = SCTDBInterface.getInstance();
00075       Statement statement = db.connection.createStatement();
00076       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00077       int recordCount=0;
00078       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00079            validatedLocation = resultSet.getString(1);
00080             }
00081 //      System.out.println("Retrieved "+recordCount+" records in total.");
00082       statement.close();
00083       return validatedLocation;
00084       }
00085 // *****************************************************************************************************************
00086     public static String validatePersonInitial(String theUser) throws Exception {
00087 
00088       String person="unknown";
00089       StringBuffer sqlStat = new StringBuffer("SELECT initls FROM persons WHERE initls='"+theUser+"'");
00090 
00091       SCTDBInterface db = SCTDBInterface.getInstance();
00092       Statement statement = db.connection.createStatement();
00093       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00094       resultSet = statement.executeQuery(sqlStat.toString());
00095       int recordCount=0;
00096       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00097            person = resultSet.getString(1);
00098             }
00099 //      System.out.println("Retrieved "+recordCount+" records in total.");
00100       statement.close();
00101       return person;
00102       }
00103 
00104 // ******************************************************************************************************************
00105     public static Vector getNameList(String location) throws Exception {
00106      StringBuffer sqlStat = new StringBuffer("SELECT initls FROM persons");
00107      sqlStat.append(" WHERE persons.locn_name LIKE '"+location+"'");
00108 
00109       SCTDBInterface db = SCTDBInterface.getInstance();
00110       Statement statement = db.connection.createStatement();
00111       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00112 
00113      int recordCount=0;
00114      Vector itemList = new Vector();
00115      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00116           recordCount++;
00117           itemList.addElement(resultSet.getString(1));  
00118             }
00119 //      System.out.println("Retrieved "+recordCount+" names in total.");
00120 
00121       statement.close();
00122 
00123       if(itemList.size()>0) return itemList;
00124 // no users at this location? If so, return all users
00125 //      System.out.println("As there are no users defined at "+location+" I will return all users in the database.");
00126      sqlStat = new StringBuffer("SELECT initls FROM persons ORDER BY initls");
00127 
00128      statement = db.connection.createStatement();
00129      resultSet = statement.executeQuery(sqlStat.toString());
00130      recordCount=0;
00131      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00132           recordCount++;
00133           itemList.addElement(resultSet.getString(1));   // serial no
00134             }
00135       statement.close();
00136 //      System.out.println("Retrieved "+recordCount+" names in total.");
00137 
00138       return itemList;
00139      }
00140 
00141 
00142 //****************************************************************************
00143 public static double get500VData(String testno) throws Exception {
00144      java.util.regex.Pattern i500Pattern = java.util.regex.Pattern.compile(".*500[\\.\\d]*\\s*([\\.\\d]+).*");
00145      java.util.regex.Pattern lastCharPattern = java.util.regex.Pattern.compile("[0-9\\.]");
00146 
00147      Vector thisData = getRawData(testno);
00148      if(thisData.size()==0 || !isSctDaqData(testno)) return 0.;
00149      String[] theDataString = ((String)thisData.elementAt(1)).split("[\\r\\n]");
00150      for(int i=theDataString.length-1;i>=0;i--) {
00151          java.util.regex.Matcher matcher = i500Pattern.matcher(theDataString[i]);
00152          if(matcher.matches()) {
00153             String data500 = theDataString[i].substring(matcher.start(1),matcher.end(1));
00154             return Double.parseDouble(data500);
00155             }
00156          }
00157       return 0.;
00158 }
00159 //*********************************************************************
00160     public static boolean isSctDaqData(String testno) throws Exception {
00161      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00162      ResultSet resultSet = statement.executeQuery("SELECT version FROM SCT_TSTDAQINFO where test_no="+testno);
00163      String version=null;
00164      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00165           version = resultSet.getString(1);
00166             }
00167      statement.close();
00168      if(version==null) return false;
00169      return version.matches("\\d+\\.\\d+");
00170      }
00171 
00172 //**********************************************************************
00173     public static Vector getLocationList() throws Exception {
00174      System.out.println("Retrieving list of valid institute names from database ...");
00175      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00176      ResultSet resultSet = statement.executeQuery("SELECT locn_name FROM locns ORDER BY locn_name");
00177      Vector itemList = new Vector();
00178      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00179           itemList.addElement(resultSet.getString(1));   // serial no
00180             }
00181 //      System.out.println("Retrieved "+recordCount+" names in total.");
00182       statement.close();
00183       return itemList;
00184      }
00185 //*************************************************************************
00186       public static void downloadPSFile(javax.swing.JFrame parent, java.net.URL theURL) {
00187 
00188       File savedFile;
00189       String theFileName;
00190       String baseName=null;
00191       String tryAgainURL=null;
00192 
00193         theFileName = theURL.getFile();
00194         baseName = theURL.toString();
00195         System.out.println("Downloading "+baseName);
00196         baseName = baseName.substring(0,baseName.indexOf(theFileName));
00197 //        System.out.println("Downloading "+baseName);
00198 
00199         String theQuery = theURL.getQuery();
00200 //        if(theQuery!=null) System.out.println("theQuery: "+theQuery);
00201         theFileName = (theQuery!=null) ? theQuery : theURL.getFile();
00202         int lastEquals = theFileName.lastIndexOf("=");
00203         if(lastEquals!=-1) theFileName = theFileName.substring(lastEquals+1);
00204         int lastSlash = theFileName.lastIndexOf("/");
00205         if(lastSlash!=-1) theFileName = theFileName.substring(lastSlash+1);
00206 
00207         if(!theFileName.endsWith(".ps")) theFileName+=".ps";
00208         savedFile = new File(PreferencesInterface.getInstance().getPreference(PreferencesInterface.SCRATCH_DIR),theFileName);
00209 // execute query here
00210        try {
00211         downloadURL(savedFile,theURL,theFileName);
00212         }catch(Exception e1) {
00213             String eString = e1.toString();
00214             int nprot = eString.indexOf("no protocol: ");
00215             if(nprot==-1) {
00216                    System.out.println("Error downloading from URL: "+eString);
00217                    return;
00218                    }
00219             String theSubURL = eString.substring(nprot+13);
00220             tryAgainURL=baseName+theSubURL;
00221         }
00222      try {
00223      if(tryAgainURL!=null) {
00224         System.out.println("Invalid URL - maybe relative pathname - reconstructing new URL from relative pathname...");
00225         theURL = new java.net.URL(tryAgainURL);
00226         theFileName = theURL.getFile();
00227         lastSlash = theFileName.lastIndexOf("/");
00228         if(lastSlash!=-1) theFileName = theFileName.substring(lastSlash+1);
00229         int gzIndex = theFileName.indexOf(".gz");
00230         if(gzIndex!=-1) theFileName=theFileName.substring(0,gzIndex);
00231         savedFile = new File(PreferencesInterface.getInstance().getPreference(PreferencesInterface.SCRATCH_DIR),theFileName);
00232           
00233 
00234         downloadURL(savedFile,theURL,theFileName);
00235         }
00236       }catch(Exception e2) {
00237          System.out.println("Exception downloading from relative pathname in URL: "+e2.toString());
00238          return;
00239          }
00240 //     theFrame.psFileList.addElement(savedFile);   //save it for deletion when application closes
00241 
00242 
00243      String psExecutable = PreferencesInterface.getInstance().getPreference(PreferencesInterface.PSVIEWER);
00244      if(psExecutable.equals("Not defined")) {
00245                    Preference preference = new Preference(parent,PreferencesInterface.PSVIEWER);
00246                    psExecutable = PreferencesInterface.getInstance().getPreference(PreferencesInterface.PSVIEWER);
00247                    }
00248      if(psExecutable.equals("Not defined")) return; // fluffed the second chance!         
00249 
00250      try {
00251          File psExec = new File(psExecutable);
00252          if(!psExec.exists()) return;
00253          String command = psExecutable+" "+savedFile.getAbsolutePath();
00254          System.out.println(command);
00255          Process p = Runtime.getRuntime().exec( command );
00256      } catch(Exception e2) {System.out.println("Exception invoking ps viewer: "+e2.toString());}
00257    }
00258 //********************
00259       public static void downloadURL(File savedFile, java.net.URL theURL, String theFileName) throws Exception {
00260       InputStream in;              
00261       byte[] buffer = new byte[128000]; // max size is 128kByte per image (I think!)
00262       FileOutputStream fos;
00263       int nByteCount=0;
00264 
00265        fos = new FileOutputStream(savedFile);       // open the file output stream
00266        in  =  theURL.openStream();
00267 // read the data - will stop when reach end of stream or error, hopefully!
00268 //       System.out.println("Downloading postscript file "+theFileName+" Please be patient.");
00269        for(int nbytes=in.read(buffer); nbytes>0; nbytes=in.read(buffer)){
00270            nByteCount += nbytes;
00271            fos.write(buffer,0,nbytes);
00272             }
00273        fos.close();
00274        in.close();
00275        System.out.println("Wrote "+nByteCount+" bytes to "+savedFile.getAbsolutePath());
00276        guiUtilities.HTMLViewer.getInstance().addImageFile(savedFile.getAbsolutePath());
00277 } 
00278 //************************************************
00279        public static Hashtable getSignoffDataTestNo(String sn, String location, String signoffType) throws Exception {
00280          Hashtable thisHash = new Hashtable();
00281          StringBuffer sqlStat = new StringBuffer("SELECT test_no FROM tests");
00282          sqlStat.append(" WHERE ser_no="+sn+" AND run_no='"+signoffType+"' AND test_name='bmMOD_RData'");
00283          if(!location.equals("Any")) sqlStat.append(" AND locn_name='"+location+"'");
00284          sqlStat.append(" ORDER BY test_date DESC, test_no DESC");
00285          Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00286          ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00287          for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00288             if(thisHash.containsKey(location)) continue;
00289             thisHash.put(location,resultSet.getString(1));
00290             break;
00291             }
00292 //      System.out.println("Retrieved "+recordCount+" names in total.");
00293       statement.close();
00294       return thisHash;
00295       }
00296 //************************************************
00297        public static Hashtable getSignoffDataTestNo(String sn, String signoffType) throws Exception {
00298 // return vector of test numbers
00299          Hashtable locationHash = new Hashtable();
00300          StringBuffer sqlStat = new StringBuffer("SELECT locn_name,test_no FROM tests");
00301          sqlStat.append(" WHERE ser_no="+sn+" AND run_no='"+signoffType+"' AND test_name='bmMOD_RData'");
00302          sqlStat.append(" ORDER BY test_date DESC, test_no DESC");
00303          Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00304          ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00305          for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00306             String location = resultSet.getString(1);
00307             String testno = resultSet.getString(2);
00308             if(locationHash.containsKey(location)) continue;
00309             locationHash.put(location,testno);
00310             if(signoffType.equals("FINALSIGNOFF")) break;
00311             }
00312 //      System.out.println("Retrieved "+recordCount+" names in total.");
00313       statement.close();
00314       return locationHash;
00315       }
00316 //************************************************
00317        public static Vector getSignoffDataTestNos(String sn1, String sn2, String currLocn, String institute, String signoffType, boolean latestOnly) throws Exception {
00318 // return vector of test numbers
00319          Set snHash = new HashSet();
00320          Vector snList = new Vector();
00321          StringBuffer sqlStat = new StringBuffer("SELECT ser_no, test_no, locn_name, run_no FROM tests");
00322  //        if(!currLocn.equals("Anywhere")) sqlStat.append(",items");
00323          sqlStat.append(" WHERE ser_no>="+sn1+" AND ser_no<="+sn2+" AND (run_no='SIGNOFF' OR run_no='FINALSIGNOFF') AND test_name='bmMOD_RData'");
00324          if(!institute.equals("Anywhere")) sqlStat.append(" AND locn_name='"+institute+"'");
00325          if(!currLocn.equals("Anywhere")) sqlStat.append(" AND tests.ser_no IN (SELECT items.ser_no FROM items WHERE items.locn_name='"+currLocn+"')");
00326          String ordering = latestOnly ? "DESC" : "";
00327          sqlStat.append(" ORDER BY ser_no, test_date "+ordering+", test_no "+ordering);
00328 //         System.out.println(sqlStat.toString());
00329          Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00330          ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00331          int recordCount=0;
00332          for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00333             String sn = resultSet.getString(1);
00334             String testno = resultSet.getString(2);
00335             String locn = resultSet.getString(3);
00336             String runno = resultSet.getString(4);
00337             boolean isUKUpload = (locn.equals("Cambridge") || locn.equals("RAL") || locn.equals("Birmingham"));
00338             if(!isUKUpload && !signoffType.equals(runno)) continue;   // non-UK uploads both SIGNOFF and FINALSIGNOFF
00339             if(isUKUpload && runno.equals("SIGNOFF")) continue; // UK uploads FINALSIGNOFF only
00340             if(latestOnly && snHash.contains(sn)) continue;
00341             snList.addElement(testno);
00342             snHash.add(sn);
00343             recordCount++;
00344             if(recordCount%100==0) System.out.println("Retrieved "+recordCount+" tests so far...");
00345             }
00346       System.out.println("Retrieved "+recordCount+" tests in total, now downloading and extracting raw data");
00347       statement.close();
00348       return snList;
00349       }
00350 //*************************************************
00351     public static String getUploaderInfo(String testno) throws Exception {
00352          StringBuffer sqlStat = new StringBuffer("SELECT locn_name,test_date,initls FROM tests");
00353          sqlStat.append(" WHERE test_no="+testno);
00354          Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00355          ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00356          String returnString=null;
00357          for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00358             String location = resultSet.getString(1);
00359             String testdate = guiUtilities.DaveUtils.extractDate(resultSet.getString(2));
00360             String initials = resultSet.getString(3);
00361             returnString = "Uploaded by "+initials+" from "+location+" on "+testdate;
00362             }
00363       statement.close();
00364       return returnString;
00365       }
00366 //**********************************************************
00367   public static Hashtable testComments(String testno_subQuery) throws Exception {
00368      return testComments(testno_subQuery,false);
00369      }
00370    public static Hashtable testComments(String testno_subQuery, boolean isHTML) throws Exception {
00371       String nl = isHTML ? "<br>" : " ";
00372       Hashtable commentHash = new Hashtable();
00373       StringBuffer sqlStat = new StringBuffer("SELECT test_no,cmnt_text FROM test_cmnts WHERE test_no IN (SELECT tests.test_no FROM tests ");
00374       sqlStat.append(testno_subQuery+")");
00375 //      System.out.println(sqlStat.toString());
00376      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00377      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00378      int ncomments=0;
00379      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00380           ncomments++;
00381           if(ncomments%100 ==0) System.out.println("Retrieved "+ncomments+" comments so far...");
00382           String testno = resultSet.getString(1);
00383           String comment = resultSet.getString(2);
00384           // beware of more than one comment in a given testno !
00385           String newComment = (commentHash.containsKey(testno)) ? (String)commentHash.get(testno)+nl+comment : comment;
00386           commentHash.put(resultSet.getString(1),newComment);
00387             }
00388       statement.close();
00389       return commentHash;
00390       }
00391 
00392 
00393 
00394 
00395    public static Hashtable testImages(String testno_subQuery) throws Exception {
00396 // has contains a vector, and the vector contains title, then url, then title, then url etc
00397       Hashtable testImageHash = new Hashtable();
00398 //      System.out.println("Checking for test images...");
00399       StringBuffer sqlStat = new StringBuffer("SELECT test_no,title,name FROM test_images WHERE test_no IN (SELECT tests.test_no FROM tests ");      
00400       sqlStat.append(testno_subQuery+")");
00401 
00402      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00403      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00404      int ncomments=0;
00405      Vector imageList;
00406      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00407           ncomments++;
00408           if(ncomments%100 ==0) System.out.println("Retrieved "+ncomments+" test images so far...");
00409           String testno = resultSet.getString(1);
00410           String title = resultSet.getString(2);
00411           String name = resultSet.getString(3);
00412           // beware of more than one image in a given testno !
00413           if(testImageHash.containsKey(testno)) imageList = (Vector)testImageHash.get(testno);
00414           else imageList = new Vector();
00415           imageList.addElement(title);
00416           imageList.addElement(name);
00417           testImageHash.put(testno,imageList);
00418             }
00419 //      System.out.println("Retrieved "+ncomments+" test images in total.");
00420       statement.close();
00421       return testImageHash;
00422       }
00423 
00424 //***********************************************************************************************
00425    public static void saveImage(String testno, String fileName, File theFile) throws Exception {
00426       InputStream in;   
00427       FileOutputStream fos = new FileOutputStream(theFile);               
00428       byte[] buffer = new byte[128000]; // max size is 128kByte per image (I think!)
00429 
00430      String sqlQuery = "SELECT data FROM test_images WHERE test_no="+testno+" AND name='"+fileName+"'";
00431      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00432      ResultSet resultSet = statement.executeQuery(sqlQuery);
00433 
00434 // execute query here
00435       System.out.println("Retrieving "+fileName+" ...");
00436      int nByteCount=0;
00437 
00438      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00439          in  = resultSet.getBinaryStream(1);           // open the data stream
00440 
00441 // read the data - will stop when reach end of stream or error, hopefully!
00442          for(int nbytes=in.read(buffer); nbytes>0; nbytes=in.read(buffer)){
00443 
00444             // System.out.print("Nbytes read : " + nbytes+". ");
00445             nByteCount += nbytes;
00446             fos.write(buffer,0,nbytes);  // write the data to the file output
00447             }
00448          fos.close();  // close the file output
00449          System.out.println("Saved image "+fileName+" with size "+nByteCount+" bytes...");
00450          guiUtilities.HTMLViewer.getInstance().addImageFile(theFile.getAbsolutePath());
00451 
00452          }
00453      statement.close();
00454      return;
00455 }
00456 // *****************************************************************************************************************
00457     public static Vector getItemVector(String serialNo) throws Exception {
00458       Vector itemList = new Vector();
00459       StringBuffer sqlStat = new StringBuffer("SELECT ");
00460       sqlStat.append("mfr,mfr_ser_no,ctype,locn_name,assembled,trashed FROM items WHERE ser_no="+serialNo);
00461       sqlStat.append(" ORDER BY ser_no");
00462 
00463      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00464 //      System.out.println("Interrogating items table ...");
00465       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00466       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00467           itemList.addElement(resultSet.getString(1));   // mfr name
00468           itemList.addElement(resultSet.getString(2));   // mfr serial no
00469           itemList.addElement(resultSet.getString(3));   // type
00470           itemList.addElement(resultSet.getString(4));   // location name
00471           itemList.addElement(resultSet.getString(5));   // assembled flag
00472           itemList.addElement(resultSet.getString(6));   // trashed flag
00473             }
00474       statement.close();
00475       return itemList;
00476       }
00477 
00478 // ******************************************************************************************************
00479     public static Vector getVisualTestList(String ser_no1, String ser_no2, String location, String currLocn,String orderString) throws Exception {
00480      StringBuffer sqlStat = new StringBuffer("SELECT ");
00481      sqlStat.append("tests.test_no,tests.ser_no,tests.test_date,tests.locn_name,tests.problem,tests.pass");
00482      sqlStat.append(" FROM tests");
00483      sqlStat.append(" WHERE tests.ser_no >= "+ser_no1+" AND tests.ser_no <= "+ser_no2);
00484      if(!location.equals("Anywhere")) sqlStat.append(" AND tests.locn_name='"+location+"'");
00485      if(!currLocn.equals("Anywhere")) sqlStat.append(" AND tests.ser_no IN (SELECT items.ser_no FROM items WHERE items.locn_name='"+currLocn+"')");
00486      sqlStat.append(" AND tests.test_name='Visual_Inspection'");
00487      sqlStat.append(" ORDER BY "+orderString);
00488      return createVisualTestTable(sqlStat.toString());
00489      }
00490 // ******************************************************************************************************
00491     public static Vector getVisualTestList(String ser_no1, String ser_no2, String location, String currLocn) throws Exception {
00492       return getVisualTestList(ser_no1,ser_no2,location,currLocn,"tests.ser_no,tests.test_date,tests.test_no");
00493      }
00494 //***************************************************************8
00495     public static Vector createVisualTestTable(String theQuery) throws Exception {
00496       Hashtable testnoHash = new Hashtable();
00497       Vector testnoList = new Vector();
00498       Vector itemList = new Vector();
00499       Vector theLine = new Vector();
00500       String snKey="SNKEY";
00501       String dateKey="DATEKEY";
00502       String locationKey="LOCATIONKEY";
00503       String flagKey="FLAGKEY";
00504 
00505      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00506 
00507      System.out.println("Interrogating tests and tstdetivs tables ...");
00508      ResultSet resultSet = statement.executeQuery(theQuery);
00509      int recordCount=0;
00510      testNumberList = new Vector();
00511      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00512           Hashtable thisHash = new Hashtable();
00513           recordCount++;
00514           if(recordCount%100 == 0) System.out.println("Retrieved "+recordCount+" records so far...");
00515           String testno = resultSet.getString(1);
00516           thisHash.put(snKey,resultSet.getString(2));
00517           String date = resultSet.getString(3);
00518           thisHash.put(dateKey,guiUtilities.DaveUtils.extractDate(date));
00519           thisHash.put(locationKey,resultSet.getString(4));
00520           String problemFlag = resultSet.getString(5);
00521           String passFlag = resultSet.getString(6);
00522           String statusFlag; 
00523           if(passFlag.equals("YES")) {
00524              statusFlag = (problemFlag.equals("YES")) ? "PROBLEM" : "Ok";
00525              }
00526           else statusFlag = "FAIL";
00527           thisHash.put(flagKey,statusFlag);
00528           testnoHash.put(testno,thisHash);   
00529           testnoList.addElement(testno);  
00530 
00531             }
00532       System.out.println("Retrieved "+recordCount+" records in total.");
00533       statement.close();
00534       if(recordCount==0) return itemList;
00535 
00536 // now get the comments, if any
00537       String subQuery = theQuery;
00538       subQuery = subQuery.substring(subQuery.indexOf("WHERE"),subQuery.indexOf("ORDER")-1);
00539       Hashtable commentsHash = testComments(subQuery);
00540       Hashtable testImagesHash = testImages(subQuery);
00541 
00542       theLine.addElement("Serial Number");
00543       theLine.addElement("Date");
00544       theLine.addElement("Location");
00545       theLine.addElement("Status");
00546       theLine.addElement("Remarks");
00547       theLine.addElement("Pictures");
00548       itemList.addElement(theLine);
00549 
00550       for(int i=0;i<testnoList.size();i++) {
00551             theLine = new Vector();
00552             String testno = (String)testnoList.elementAt(i); // this contains the testno's, ordered by serial no
00553             Hashtable tempHash = (Hashtable)testnoHash.get(testno);
00554             theLine.addElement(tempHash.get(snKey));                                      // first column is serial number
00555 
00556             theLine.addElement(tempHash.get(dateKey));                                    // 2nd column is the date
00557             theLine.addElement(tempHash.get(locationKey));              
00558             theLine.addElement(tempHash.get(flagKey));
00559             if(commentsHash.containsKey(testno)) theLine.addElement(commentsHash.get(testno));
00560             else theLine.addElement("");
00561             if(testImagesHash.containsKey(testno)) {
00562                Vector imageList = (Vector)testImagesHash.get(testno);
00563 
00564                for(int j=0;j<imageList.size()/2; j++) {
00565                    if(j==0) {
00566 
00567                               theLine.addElement((String)imageList.elementAt(0));
00568                               testNumberList.addElement(testno);
00569                               itemList.addElement(theLine);
00570                               }
00571                    else {
00572 
00573                               theLine = new Vector();
00574                               theLine.addElement(tempHash.get(snKey));
00575                               theLine.addElement("  ''");
00576                               theLine.addElement("  ''");
00577                               theLine.addElement("  ''");
00578                               theLine.addElement("  ''");
00579                               theLine.addElement((String)imageList.elementAt(2*j));  
00580                               testNumberList.addElement(testno);
00581                               itemList.addElement(theLine);                     
00582                           }
00583 
00584                       }
00585             }
00586             else {theLine.addElement(""); testNumberList.addElement(testno);itemList.addElement(theLine);}
00587             }
00588       return itemList;
00589 
00590 
00591       }
00592 //
00593 //***********************************************************************************************
00594    public static Vector getImages(String testno,String headerTitle) throws Exception {
00595       InputStream in;   
00596  //     FileOutputStream fos;               
00597       byte[] buffer = new byte[128000]; // max size is 128kByte per image (I think!)
00598 
00599      Vector imageList = new Vector();
00600 
00601      String sqlQuery = "SELECT data,title,name FROM test_images WHERE test_no="+testno;
00602      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00603      ResultSet resultSet = statement.executeQuery(sqlQuery);
00604 
00605 // execute query here
00606       System.out.println("Interrogating test_images table for images ...");
00607      int nByteCount=0;
00608      int noImages=0;
00609 
00610      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00611          noImages++;
00612          in  = resultSet.getBinaryStream(1);           // open the data stream
00613 
00614 // read the data - will stop when reach end of stream or error, hopefully!
00615          for(int nbytes=in.read(buffer); nbytes>0; nbytes=in.read(buffer)){
00616             nByteCount += nbytes;
00617             }
00618          javax.swing.ImageIcon thisImage;
00619          try {
00620            thisImage = new javax.swing.ImageIcon(buffer);
00621          } catch (Exception e) {thisImage=null;} // just in case 
00622          String title = resultSet.getString(2);
00623          String name = resultSet.getString(3);
00624          System.out.println("Retrieved image "+name+" with size "+nByteCount+" bytes...");
00625          imageList.addElement(new guiUtilities.Photo(thisImage,title,name,headerTitle,testno));
00626          }
00627      System.out.println("Retrieved "+noImages+" images in total for test number "+testno);
00628      statement.close();
00629 //     storedImages.put(testno,imageList); // store the image list vector, in case we want to look again
00630      return imageList;
00631 }
00632 
00633 //***************************************************************************************************
00634  public static Map getRawDataLists(String sn1, String sn2, String location, int testType, int temperature, String runNo, int testRequest) throws Exception {
00635       Map m = new HashMap();
00636       StringBuffer sqlStat = new StringBuffer("SELECT tests.test_no,tests.ser_no,tests.test_date,tests.locn_name,");
00637       if(testType>=0) sqlStat.append("test_rawdata.filename,");
00638       sqlStat.append("SCT_TSTDCSINFO.T0 FROM tests,");
00639       if(testType>=0) sqlStat.append("test_rawdata,");
00640       sqlStat.append("SCT_TSTDCSINFO ");
00641 
00642       if(testRequest>=0) sqlStat.append("WHERE tests.test_name='"+sctdaqDBTestNames[testRequest]+"'");
00643       else {
00644         switch(testType) {
00645          case SCTDBInfo.SCTDB_TEST_TIMEWALK: 
00646 // xml data
00647       sqlStat.append(" WHERE ( (tests.test_name = 'HybTWalk' AND tests.locn_name !='Oxford') OR (tests.test_name = 'HybNoise' AND tests.locn_name = 'Oxford'))");
00648           break;
00649          default:
00650           sqlStat.append("WHERE tests.test_name='"+sctdaqDBTestNames[testType]+"'");
00651           }
00652        }
00653      sqlStat.append(" AND tests.ser_no >= "+sn1+" AND tests.ser_no <= "+sn2);
00654      if(runNo!=null) sqlStat.append(" AND tests.run_no LIKE '"+runNo+"'");
00655      if(testType>=0)sqlStat.append(" AND tests.test_no=test_rawdata.test_no");
00656      sqlStat.append(" AND SCT_TSTDCSINFO.test_no=tests.test_no");
00657      if(!location.equals("Anywhere")) sqlStat.append(" AND tests.locn_name='"+location+"'");
00658      switch(temperature) {
00659        case 1:
00660          sqlStat.append(" AND SCT_TSTDCSINFO.T0<=10");
00661          break;
00662        case 2:
00663          sqlStat.append(" AND SCT_TSTDCSINFO.T0>10");
00664          break;
00665        default:
00666        }
00667      sqlStat.append(" ORDER BY tests.ser_no,tests.test_date,tests.test_no");
00668 
00669 //     System.out.println(sqlStat.toString());
00670      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00671      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00672 
00673      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00674 
00675            SCTDBTestInfoHolder testInfo = new SCTDBTestInfoHolder();
00676            testInfo.put(SCTDBTestInfoHolder.TESTNUMBER,resultSet.getString(1));
00677            Long sn = new Long(resultSet.getLong(2));
00678            testInfo.put(SCTDBTestInfoHolder.SN,sn.toString());
00679            testInfo.put(SCTDBTestInfoHolder.DATE,guiUtilities.DaveUtils.extractDate(resultSet.getString(3)));
00680            testInfo.put(SCTDBTestInfoHolder.LOCATION,resultSet.getString(4));
00681            int lastIndex = (testType>=0) ? 6 : 5;
00682            testInfo.put(SCTDBTestInfoHolder.TEMPERATURE,resultSet.getString(lastIndex));
00683            m.put(sn,testInfo);
00684        }
00685      statement.close();
00686      return m;
00687      }
00688    public static Vector getRawData(String testno) throws Exception {
00689      return getRawData(testno,PreferencesInterface.getInstance().getPreference(PreferencesInterface.SCRATCH_DIR));
00690      }
00691 
00692 // ****************************************************************************************************************
00693    public static Vector getRawData(String testno, String saveDir) throws Exception {
00694 // get the raw data - if its a zip file, save it to disk with same filename
00695 // otherwise just download as a string
00696       Vector returnData = new Vector();
00697 
00698       // first get filename of the raw data file of this testno
00699 
00700      String sqlQuery = "SELECT filename from test_rawdata WHERE test_no="+testno;
00701      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00702      ResultSet resultSet = statement.executeQuery(sqlQuery);
00703      String thisFname = null;
00704      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00705           thisFname = resultSet.getString(1);
00706           }
00707      statement.close();
00708      if(thisFname==null) {
00709           return returnData;
00710           }
00711 // now download that file
00712      File savedFile = new File(saveDir,thisFname);
00713      if(downloadFile(testno,savedFile)==0) return returnData;
00714 // now read in the downloaded file - firat assume it is zipped...
00715      try {
00716        returnData = unZipFile(savedFile);
00717        }
00718      catch(Exception ee) {
00719 // not zipped, so assume its just text
00720        returnData = getFileContent(savedFile);
00721        File tempFile = new File(thisFname); // get just the last part, not a pathname
00722        returnData.insertElementAt((String)tempFile.getName(),0);
00723        }
00724      boolean deleted = savedFile.delete(); // delete the downloaded file
00725 // check if its an .xml file, if so, assume it is an sctdaq xml config file
00726 // in which case, we want to lookup and susbtitute the calibrate correction factors
00727      returnData = insertCalFactors(returnData);
00728 // index0 : filename
00729 // index1 : content as a String
00730 // ..etc for index2 and 3 etc
00731      return returnData;
00732      }
00733 //***********************************************************************8
00734 public static Vector getFileContent(File theFile) throws Exception {
00735     // get file content as a String
00736     Vector returnData = new Vector();
00737     BufferedReader in = new BufferedReader(new FileReader(theFile));
00738     String line;
00739     StringBuffer stringBuffer = new StringBuffer();
00740     boolean firstLine=true;
00741     while((line=in.readLine())!=null) {
00742       if(!firstLine) stringBuffer.append("\n");
00743       stringBuffer.append(line);
00744       firstLine=false;
00745       }
00746     in.close();
00747     returnData.addElement(stringBuffer.toString());
00748     return returnData;
00749     }
00750 
00751 //********************************************
00752 public static int downloadFile(String testno, File savedFile) throws Exception {
00753 
00754       InputStream in;         
00755       String theData = "";      
00756       byte[] buffer = new byte[128000]; // max size is 128kByte per image (I think!)
00757     FileOutputStream fos;
00758 // execute query here
00759      String sqlQuery = "SELECT raw_data FROM test_rawdata WHERE test_no="+testno; 
00760      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00761      ResultSet resultSet = statement.executeQuery(sqlQuery);
00762 
00763      int nByteCount=0;
00764      fos = new FileOutputStream(savedFile);       // open the file output stream
00765      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00766          in  = resultSet.getBinaryStream(1);           // open the data stream
00767 
00768 // read the data - will stop when reach end of stream or error, hopefully!
00769          for(int nbytes=in.read(buffer); nbytes>0; nbytes=in.read(buffer)){
00770            nByteCount += nbytes;
00771            fos.write(buffer,0,nbytes);
00772             }
00773          }
00774      fos.close();
00775      statement.close();
00776      if(nByteCount==0) System.out.println("No raw data available for test "+testno);
00777 //     else System.out.println("Downloaded "+savedFile.getName()+", size "+nByteCount+" bytes.");
00778      return nByteCount;
00779 }
00780 //*************************************************************************
00781 public static Vector unZipFile(File savedFile) throws Exception{
00782       Vector returnData = new Vector();
00783       byte[] buffer = new byte[128000]; // max size is 128kByte per image (I think!)
00784 
00785 // now extract the zip file...
00786      final int BUFFER=2048;
00787      ZipFile zFile = new ZipFile(savedFile);
00788      FileInputStream fis = new FileInputStream(savedFile);
00789      ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
00790      ZipEntry entry;
00791      while((entry = zis.getNextEntry()) != null) {
00792          returnData.addElement((String)entry.getName());   // first the data filename
00793          int count;
00794          byte[] data = new byte[BUFFER];
00795          StringBuffer thisData = new StringBuffer();
00796          while((count=zis.read(data,0,BUFFER)) != -1) {
00797             thisData.append(new String(data,0,count)); 
00798             }
00799          returnData.addElement((String)thisData.toString());    // then the filename content as a string
00800         }
00801      zis.close();
00802      return returnData;
00803 }
00804 public static Vector insertCalFactors(Vector rawdata) throws Exception {
00805     String lineTerminator = "\\s*\\n";
00806     Pattern modulePattern = Pattern.compile("<module>");
00807     Pattern locationPattern = Pattern.compile(".*<location>(.*)</location>.*");
00808     Pattern snPattern = Pattern.compile("\\s*<sn>(\\d{14})</sn>");
00809     Pattern chipIDPattern = Pattern.compile("\\s*<chip id=\"(\\d{2})\".*");
00810     Pattern cFactorPattern = Pattern.compile("(\\s*)<c_factor>.*</c_factor>");
00811     Pattern runNoPattern = Pattern.compile(".*<run>(\\d+)</run><scan>(\\d+)</scan>.*");
00812     Pattern rcFitPattern = Pattern.compile("(.*)<rc_function.*</rc_function>");
00813     String moduleSerialNo = null;
00814     String location=null;
00815     String thisChipPosition=null;
00816     Hashtable chipPositionHash = new Hashtable();
00817     Hashtable rcFitHash = null;
00818 
00819     boolean xmlfile=false;
00820     for(int i=0;i<rawdata.size();i++) {
00821 
00822         if(i%2==0) {
00823             if (((String)rawdata.elementAt(i)).indexOf(".xml")!=-1) xmlfile=true;
00824             else xmlfile=false;
00825             continue;
00826             }
00827 
00828         if(!xmlfile) continue;
00829         String[] lines = ((String)rawdata.elementAt(i)).split(lineTerminator);
00830 
00831         Matcher matcher = modulePattern.matcher(lines[0]);
00832         if(!matcher.matches()) return rawdata;     // first line of xml file is expected to be <module>
00833 
00834         StringBuffer newString = new StringBuffer("<module>");
00835         for(int line=1;line<lines.length;line++) {
00836             String thisLine = lines[line];
00837 //            System.out.println("Checking line "+line+": "+thisLine);
00838             matcher = snPattern.matcher(thisLine);
00839             if(matcher.matches()) {
00840                  moduleSerialNo = thisLine.substring(matcher.start(1),matcher.end(1));
00841                  chipPositionHash = getChipPositionHash(moduleSerialNo);               
00842                  }
00843             matcher = locationPattern.matcher(thisLine);
00844             if(matcher.matches()) location = thisLine.substring(matcher.start(1),matcher.end(1));
00845 
00846             matcher = runNoPattern.matcher(thisLine);
00847             if(matcher.matches()) {
00848                    rcFitHash = getRCFits(moduleSerialNo,location,thisLine.substring(matcher.start(1),matcher.end(1)),thisLine.substring(matcher.start(2),matcher.end(2)));
00849                    if(rcFitHash.size()!=12) System.err.println("Failed to extract RC fit parameters for module config file");
00850                    }
00851 
00852             matcher = rcFitPattern.matcher(thisLine);
00853             if(matcher.matches() && thisChipPosition!=null && rcFitHash!=null) {
00854               String padding = thisLine.substring(matcher.start(1),matcher.end(1));
00855               if(rcFitHash.containsKey(thisChipPosition)) {
00856                  lines[line] = padding+(String)rcFitHash.get(thisChipPosition);
00857                  }
00858               }
00859 
00860             matcher = chipIDPattern.matcher(thisLine);
00861             if(matcher.matches()) {
00862                  thisChipPosition = thisLine.substring(matcher.start(1),matcher.end(1));
00863 //                 System.out.println("Found chip position "+thisChipPosition);
00864                  }
00865             matcher = cFactorPattern.matcher(thisLine);
00866             if(matcher.matches() && thisChipPosition!=null) {
00867                  String spaces = thisLine.substring(matcher.start(1),matcher.end(1));
00868                  if(chipPositionHash.containsKey(thisChipPosition)) lines[line] = spaces+"<c_factor>"+(String)chipPositionHash.get(thisChipPosition)+"</c_factor>";
00869                  else System.err.println("Cal Correction factors are not available for "+moduleSerialNo);
00870                 }
00871             newString.append("\n"+lines[line]);
00872 
00873             }
00874          rawdata.setElementAt((String)newString.toString(),i);
00875 
00876         }
00877 
00878    return rawdata;
00879 }
00880 public static Hashtable getChipPositionHash(String moduleSN) throws Exception {
00881    Pattern lotNoPattern = Pattern.compile("(Z\\d+-W\\d+).*"); // miss out the non-LOT info, eg Z39993-W12 X1 Y2
00882    Hashtable thisHash = new Hashtable();
00883    String sn=null;
00884 // first get the hybrid serial no
00885      StringBuffer sqlStat = new StringBuffer("SELECT ");
00886      sqlStat.append("ser_no from ASSM_ITEMS WHERE assm_ser_no="+moduleSN);
00887      sqlStat.append(" AND (ctype LIKE '%Hybrid%' OR ctype LIKE 'bmHASIC')");
00888 //     System.out.println("get hybrid serial no ...");
00889 //     System.out.println(sqlStat.toString());
00890      Statement statement;
00891      statement = SCTDBInterface.getInstance().connection.createStatement();
00892 
00893      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00894 
00895      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00896           sn = resultSet.getString(1);
00897           }
00898      statement.close();
00899      if(sn==null) return thisHash;  // no entry in assembly table - return empty hash
00900 //     else System.out.println("Hybrid is "+sn);
00901 
00902 // now get the chips in the assembly table
00903 // construct the hash with position as Key and LotNumber as value
00904      sqlStat = new StringBuffer("SELECT ");
00905      sqlStat.append("ASSM_ITEMS.posn,items.mfr_ser_no from ASSM_ITEMS,items WHERE assm_ser_no="+sn);
00906      sqlStat.append(" AND items.ser_no=assm_items.ser_no AND assm_items.ctype = 'chABCD3T'");
00907 
00908 //     System.out.println("now get chips ...");
00909 //     System.out.println(sqlStat.toString());
00910      statement = SCTDBInterface.getInstance().connection.createStatement();
00911      resultSet = statement.executeQuery(sqlStat.toString());
00912 
00913      int chipno=0;
00914      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00915           String posn = resultSet.getString(1);
00916           String mfr_sn = resultSet.getString(2);
00917           Matcher matcher = lotNoPattern.matcher(mfr_sn);
00918           int thePosition = Integer.parseInt(posn);
00919           thePosition--;
00920           posn = Integer.toString(thePosition);
00921 //          System.out.println("chipno="+chipno+", posn="+posn+", mfr_sn="+mfr_sn);
00922           if(matcher.matches()) {
00923             mfr_sn = mfr_sn.substring(matcher.start(1),matcher.end(1));
00924             if(posn.length()<2) posn="0"+posn;
00925             thisHash.put(posn,mfr_sn);
00926 //            System.out.println("chipno="+chipno+", posn="+posn+", mfr_sn="+mfr_sn);
00927             }
00928           chipno++;
00929           }
00930       statement.close();
00931      if(thisHash.size()!=12) {
00932         System.out.println("No asic assembly data for hybrid "+sn);
00933         return new Hashtable();
00934         }
00935 // ok, now go through the chips to get the cal correction factors
00936      Hashtable correctionHash = new Hashtable();
00937      int ifactors=0;
00938      for (Enumeration e = thisHash.keys() ; e.hasMoreElements() ;) {
00939          String thisPosn = (String)e.nextElement();
00940          String thisLotNo = (String)thisHash.get(thisPosn);
00941          if(!correctionHash.containsKey(thisLotNo)) {
00942               String factor = WaferUtilities.getCalCorrFactor(thisLotNo); 
00943               if(!factor.equals("")) correctionHash.put(thisLotNo,factor);
00944               else {
00945                   System.out.println("WARNING: CalCorr factors not available for wafer "+thisLotNo);
00946                   correctionHash.put(thisLotNo,"1.0");
00947                   }
00948               }
00949          if(correctionHash.containsKey(thisLotNo)) {
00950               String thisFactor = (String)correctionHash.get(thisLotNo);
00951               thisHash.put(thisPosn,thisFactor);
00952 //              System.out.println("Putting "+thisFactor+" into chip position "+thisPosn);
00953               ifactors++;
00954               }
00955          }
00956       ifactors = 12 - ifactors;
00957       if(ifactors>0) System.out.println("WARNING: CalCorr factors not found for "+ifactors+" chips");
00958     
00959       return thisHash;    
00960 
00961 }
00962    public static Hashtable getRCFits(String sn, String location, String runNo, String scanNo) throws Exception {
00963 
00964       Hashtable rcFitHash = new Hashtable();
00965       if(sn==null) {
00966          System.err.println("Null serialno in getRCFits");
00967          return rcFitHash;
00968          }
00969       if(location==null) {
00970          System.err.println("Null location in getRCFits");
00971          return rcFitHash;
00972          }
00973 
00974       int scanNumber= Integer.parseInt(scanNo);
00975 // the run-scan of the ResponseCurve is either 10 or 12 scans before the run-scan in the module config file
00976 // depending on whether it was uploaded with the TimeWalk or Noise test
00977       String rsString1 = runNo+"-"+Integer.toString(scanNumber-10);
00978       String rsString2 = runNo+"-"+Integer.toString(scanNumber-21);
00979 
00980 // first get the hybrid serial no
00981      StringBuffer sqlStat = new StringBuffer("SELECT tests.test_no");
00982 
00983      for(int i=0;i<12;i++) {
00984         for (int y=9;y<=12;y++) sqlStat.append(","+chipDBnames[i]+DBChipParameterNames[SCTDB_TEST_NPTGAIN][y]);
00985         }
00986      sqlStat.append(" FROM SCT_TSTHYBRC,tests WHERE SCT_TSTHYBRC.test_no=tests.test_no");
00987      sqlStat.append(" AND (tests.run_no='"+rsString1+"' OR tests.run_no='"+rsString2+"')");
00988      sqlStat.append(" AND tests.ser_no="+sn+" AND tests.locn_name='"+location+"'");
00989 
00990 //     System.out.println(sqlStat.toString());
00991 
00992      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00993      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
00994 
00995      int arg=1;
00996      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00997           String testno = resultSet.getString(arg++);
00998           for(int chipno=0;chipno<12;chipno++) {
00999               String line = "<rc_function type=\""+resultSet.getString(arg++)+"\">p0 "+resultSet.getString(arg++)+" p1 "+resultSet.getString(arg++)+" p2 "+resultSet.getString(arg++)+"</rc_function>";
01000               String posn = Integer.toString(chipno);
01001               if(posn.length()<2) posn="0"+posn;
01002               rcFitHash.put(posn,line);
01003               }
01004           }
01005       statement.close();
01006       return rcFitHash;
01007       }
01008 
01009 
01010 
01011      
01012 
01013 
01014 // ******************************************************************************************************************
01015     public static boolean confirmLocation(String location) throws Exception {
01016      boolean foundit=false;
01017      String sqlStat = new String("SELECT locn_name FROM locns WHERE locn_name='"+location+"'");
01018      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01019      ResultSet resultSet = statement.executeQuery(sqlStat);
01020      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01021            return true;
01022             }
01023       statement.close();
01024       return foundit;
01025      }
01026 //**************************************************************
01027      public static String getTestString(int testIndex,String testno) {
01028          Hashtable snHash = new Hashtable();
01029          Hashtable testnoHash = new Hashtable();
01030 
01031          StringBuffer result = new StringBuffer();
01032 
01033          StringBuffer sqlStat = new StringBuffer();
01034             sqlStat = new StringBuffer("SELECT tests.ser_no,tests.pass,tests.problem,tests.TEST_no,tests.locn_name,tests.TEST_date,tests.run_no,tests.test_name,SCT_TSTDCSINFO.T0");
01035             sqlStat.append(",SCT_TSTDAQINFO.VERSION,SCT_TSTDAQINFO.TEST_TIME");
01036             switch(testIndex) {
01037                     case SCTDB_TEST_RESET:
01038                     case SCTDB_TEST_REDUNDANCY:
01039                     case SCTDB_TEST_LONGTERM:
01040                     case SCTDB_TEST_IV:
01041                         break;
01042                     default:
01043                         for(int y=0;y<chipDBnames.length;y++) {
01044                            for(int paramIndex=0;paramIndex<DBChipParameterNames[testIndex].length;paramIndex++) {
01045                                   sqlStat.append(","+chipDBnames[y]+DBChipParameterNames[testIndex][paramIndex]);
01046                                   }
01047                           }
01048 
01049                      }
01050          sqlStat.append(" FROM "+sctdaqDBTableNames[testIndex]+",tests,SCT_TSTDCSINFO,SCT_TSTDAQINFO");
01051          sqlStat.append(" WHERE tests.test_no = "+testno);
01052          sqlStat.append(" AND tests.TEST_no = "+sctdaqDBTableNames[testIndex]+".TEST_no");
01053          sqlStat.append(" AND tests.TEST_no = SCT_TSTDCSINFO.TEST_no");
01054          sqlStat.append(" AND tests.TEST_no = SCT_TSTDAQINFO.TEST_no");
01055          sqlStat.append(" ORDER BY tests.TEST_date DESC,tests.TEST_no DESC");
01056 
01057          try {
01058 
01059          Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01060 //         System.out.println(sqlStat.toString());
01061          ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01062          for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01063             int rc=1;
01064 
01065             String sn = resultSet.getString(rc++);
01066             result.append("SERIALNO="+sn+"\n");
01067             if(snHash.containsKey(sn)) continue;
01068             int status = (resultSet.getString(rc++).equals("YES")) ? 0 : 2;
01069             if(resultSet.getString(rc++).equals("YES") && status==0) status = 1;
01070 
01071 
01072 
01073             result.append(sctdaqNormalTestNames[testIndex]+"\n");
01074             switch(status) {
01075               case 0:
01076                  result.append("PASS\n");
01077                  break;
01078               case 1:
01079                  result.append("PROBLEM\n");
01080                  break;
01081               default:
01082                  result.append("FAIL\n");
01083                  }
01084             result.append("TESTNO="+resultSet.getString(rc++)+"\n");  // testno
01085             result.append("LOCATION="+resultSet.getString(rc++)+"\n");  //date
01086             result.append("DATE="+guiUtilities.DaveUtils.extractDate(resultSet.getString(rc++))+"\n");
01087             result.append("RUN/SCAN="+resultSet.getString(rc++)+"\n");
01088             result.append("TESTNAME="+resultSet.getString(rc++)+"\n");
01089             result.append("TEMPERATURE="+resultSet.getString(rc++)+"\n");
01090            result.append("SCTDAQ_VERSION="+resultSet.getString(rc++)+"\n");
01091            result.append("TIME="+resultSet.getString(rc++)+"\n");
01092 
01093             result.append("Chip\t");
01094             for(int i=0;i<DBChipParameterNames[testIndex].length;i++) result.append(DBChipParameterNames[testIndex][i]+"\t");
01095             result.append("\n");
01096 
01097             for(int chip=0;chip<12;chip++) {
01098               result.append(chipDBnames[chip].substring(0,2)+"\t");
01099               for(int r=0;r<DBChipParameterNames[testIndex].length;r++) {
01100                  if(r>0) result.append("\t");
01101                  result.append(resultSet.getString(rc++));
01102                  }
01103               result.append("\n");
01104               }
01105 
01106             }
01107         statement.close();
01108 
01109         sqlStat = new StringBuffer();
01110         sqlStat.append("SELECT defects.defect_name,defects.chan_1st,defects.chan_last");
01111         sqlStat.append(" FROM defects");
01112         sqlStat.append(" WHERE defects.TEST_no = "+testno);
01113 
01114 
01115         statement = SCTDBInterface.getInstance().connection.createStatement();
01116 //         System.out.println(sqlStat.toString());
01117         resultSet = statement.executeQuery(sqlStat.toString());
01118         int ndefects=0;
01119         result.append("Defects:\n");
01120         for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){ 
01121             ndefects++;
01122             String defectName = resultSet.getString(1);
01123             String chan1 = resultSet.getString(2);
01124             String chanLast = resultSet.getString(3);
01125             result.append(chan1+"-"+chanLast+" : "+defectName+"\n");
01126             }
01127         statement.close();
01128         result.append("defectCount="+Integer.toString(ndefects)+"\n");
01129 
01130         }catch(Exception e) {System.out.println("Failed to publish SCTDB Data: "+e.toString());}
01131 
01132         return result.toString();
01133 }
01134 
01135 //****************************************************************************************8
01136      public static void removePreviousUploads(String testname, String runno, String locn, Map itemList) throws Exception {
01137 
01138      StringBuffer sqlStat = new StringBuffer("SELECT tests.ser_no,test_name,run_no,test_date,sct_tstdaqinfo.test_time FROM tests,sct_tstdaqinfo");
01139      sqlStat.append(" WHERE tests.test_name='"+testname+"' AND tests.locn_name='"+locn+"' AND tests.run_no='"+runno+"' AND sct_tstdaqinfo.test_no=tests.test_no");
01140      sqlStat.append(" AND sct_tstdaqinfo.version LIKE 'SctRodDaq%'");
01141      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01142 //     System.out.println(sqlStat.toString());
01143      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01144      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01145           StringBuffer thisSig = new StringBuffer(resultSet.getString(1));
01146           thisSig.append("_");
01147           thisSig.append(resultSet.getString(2));   // testname
01148           thisSig.append("_");
01149           thisSig.append(resultSet.getString(3));   // run_no
01150           thisSig.append("_");
01151           thisSig.append(guiUtilities.DaveUtils.extractSCTDAQDate(resultSet.getString(4)));   // date
01152           thisSig.append("_");
01153           thisSig.append(resultSet.getString(5));   // time
01154           String signature = thisSig.toString();
01155           if(itemList.containsKey(signature)) {
01156               System.out.println("Data for "+signature.substring(0,14)+" is already uploaded!");
01157               itemList.remove(signature);
01158               }
01159             }
01160       statement.close();
01161      }
01162 
01163 //*************************************************************
01164      public static String getUserName(String locn) throws Exception {
01165      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01166 //     System.out.println(sqlStat.toString());
01167      ResultSet resultSet = statement.executeQuery("SELECT username FROM locns WHERE locn_name='"+locn+"'");
01168      String username = null;
01169      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01170           username = resultSet.getString(1);
01171           }
01172      statement.close();
01173      return username;
01174      }
01175 //*************************************************************
01176      public static String getSerialNo(String testno) throws Exception {
01177      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01178 //     System.out.println(sqlStat.toString());
01179      ResultSet resultSet = statement.executeQuery("SELECT ser_no FROM tests WHERE test_no='"+testno+"'");
01180      String sn = null;
01181      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01182           sn = resultSet.getString(1);
01183           }
01184      statement.close();
01185      return sn;
01186      }
01187 //*************************************************************
01188      public static String getCType(String serno) throws Exception {
01189      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01190 //     System.out.println(sqlStat.toString());
01191      ResultSet resultSet = statement.executeQuery("SELECT ctype FROM items WHERE ser_no='"+serno+"'");
01192      String ctype = null;
01193      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01194           ctype = resultSet.getString(1);
01195           }
01196      statement.close();
01197      return ctype;
01198      }
01199 //**********************************************************
01200     public static List getTestList(int testIndex,String locn, String testMenuName) throws Exception {
01201      java.util.regex.Pattern runPattern = java.util.regex.Pattern.compile("(\\d+)-(\\d+)");
01202      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01203      StringBuffer sqlStat = new StringBuffer("SELECT DISTINCT test_date,run_no FROM tests,sct_tstdaqinfo WHERE locn_name='"+locn+"' AND test_name='"+sctdaqDBTestNames[testIndex]+"'");
01204      sqlStat.append(" AND tests.test_no=sct_tstdaqinfo.test_no AND sct_tstdaqinfo.version LIKE 'SctRodDaq%'");
01205      sqlStat.append(" ORDER BY tests.test_date DESC");
01206 //     System.out.println(sqlStat.toString());
01207      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01208      List itemList = new ArrayList();
01209      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01210           List row = new ArrayList();
01211           row.add(guiUtilities.DaveUtils.extractDate(resultSet.getString(1)));
01212           row.add(testMenuName);
01213           String runno = resultSet.getString(2);
01214           java.util.regex.Matcher matcher = runPattern.matcher(runno);
01215           if(!matcher.matches()) {
01216             System.err.println("Unrecognised runno "+runno);
01217             continue;
01218              }
01219           
01220           row.add(runno.substring(matcher.start(1),matcher.end(1)));
01221           row.add(runno.substring(matcher.start(2),matcher.end(2)));
01222           itemList.add(row);
01223 
01224           }
01225      statement.close();
01226      return itemList;
01227      }
01228 
01229 //******************************************************
01230    public static SCTDBTestInfoHolder getTestInfo(String test_no) throws Exception {
01231 
01232       SCTDBTestInfoHolder testInfo= new SCTDBTestInfoHolder();
01233 
01234       StringBuffer genTable = new StringBuffer();
01235       StringBuffer sqlStat = new StringBuffer("SELECT tests.test_name,tests.ser_no,tests.test_date,tests.locn_name,items.ctype FROM tests,items WHERE tests.test_no="+test_no);
01236       sqlStat.append(" AND items.ser_no=tests.ser_no");
01237 
01238 // submit query
01239      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01240      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01241    
01242      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01243           testInfo.put(SCTDBTestInfoHolder.TEST_NAME,resultSet.getString(1));
01244           testInfo.put(SCTDBTestInfoHolder.SN,resultSet.getString(2));
01245           testInfo.put(SCTDBTestInfoHolder.DATE,guiUtilities.DaveUtils.extractDate(resultSet.getString(3)));
01246           testInfo.put(SCTDBTestInfoHolder.LOCATION,resultSet.getString(4));
01247           testInfo.put(SCTDBTestInfoHolder.CTYPE,resultSet.getString(5));
01248           }
01249      testInfo.put(SCTDBTestInfoHolder.TESTNUMBER,test_no);
01250      statement.close();
01251      if(testInfo.isValid()) return testInfo;
01252      return null;
01253      }
01254 
01255 // **********************************************************************************************
01256     public static Vector getTestHistory(String serialNo) throws Exception {
01257      StringBuffer sqlStat = new StringBuffer("SELECT ");
01258      sqlStat.append("tests.ser_no,tests.locn_name,tests.test_date,tests.test_name,tests.problem,tests.pass");
01259      sqlStat.append(" FROM tests");
01260      sqlStat.append(" WHERE tests.ser_no = "+serialNo);
01261      sqlStat.append(" ORDER BY tests.test_date,tests.test_no");
01262      
01263 
01264       Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01265       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01266       Hashtable serNoHash = new Hashtable();
01267       Hashtable thisHash;
01268       Vector serNoList = new Vector();
01269       Vector theLine = new Vector();
01270       Vector itemList = new Vector();
01271       int recordCount=0;
01272       theLine.addElement("Serial Number");
01273       theLine.addElement("Test Location");
01274       theLine.addElement("Date");
01275       theLine.addElement("Test Name");
01276       theLine.addElement("Test Status");
01277       itemList.addElement(theLine);
01278       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01279           theLine = new Vector();
01280           theLine.addElement(resultSet.getString(1));
01281           theLine.addElement(resultSet.getString(2));
01282           theLine.addElement(guiUtilities.DaveUtils.extractDate(resultSet.getString(3)));
01283           theLine.addElement(resultSet.getString(4));
01284 
01285           String problemFlag = resultSet.getString(5);
01286           String passFlag = resultSet.getString(6);
01287           String statusFlag; 
01288           if(passFlag.equals("YES")) {
01289              statusFlag = (problemFlag.equals("YES")) ? "Problem" : "Pass";
01290              }
01291           else statusFlag="FAIL";
01292           theLine.addElement(statusFlag);
01293           itemList.addElement(theLine);
01294           recordCount++;
01295           if(recordCount%100 ==0) System.out.println("Retrieved "+recordCount+" records ...");
01296             }
01297        System.out.println("Retrieved "+recordCount+" records in total.");
01298 
01299        statement.close();
01300        return itemList;
01301 
01302      }
01303 
01304    public static Vector getShipmentHistory(String serialNo) throws Exception {
01305       Vector theLine = new Vector();
01306       Vector itemList = new Vector();
01307       StringBuffer sqlStat = new StringBuffer("SELECT ship_items.ser_no,ship_items.ship_no,ship.locn_name,ship.dest_locn_name,ship.ship_date,ship_items.recvd FROM ship,ship_items ");
01308       sqlStat.append("WHERE ship_items.ser_no="+serialNo+" AND ship_items.ship_no=ship.ship_no ");
01309       sqlStat.append("ORDER BY ship.ship_date,ship.ship_no"); 
01310  
01311       theLine.addElement("Serial Number");
01312       theLine.addElement("Shipment No");
01313       theLine.addElement("Sent from");
01314       theLine.addElement("Received By");
01315       theLine.addElement("Send Date");
01316       theLine.addElement("Received?");
01317       itemList.addElement(theLine);
01318       Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01319       ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01320       
01321       int recordCount=0;
01322       for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01323           theLine = new Vector();
01324           theLine.addElement(resultSet.getString(1));
01325           theLine.addElement(resultSet.getString(2));
01326           theLine.addElement(resultSet.getString(3));
01327           theLine.addElement(resultSet.getString(4));
01328           theLine.addElement(guiUtilities.DaveUtils.extractDate(resultSet.getString(5)));
01329           theLine.addElement(new Boolean(resultSet.getString(6).equals("YES")));
01330           itemList.addElement(theLine);
01331 
01332           recordCount++;
01333           if(recordCount%100 ==0) System.out.println("Retrieved "+recordCount+" shipment records ...");
01334             }
01335        System.out.println("Retrieved "+recordCount+" shipment records in total.");
01336        statement.close();
01337        return itemList;
01338        }
01339 
01340 
01341     public static List getRodList(String sn) throws Exception {
01342 
01343       StringBuffer sqlStat = new StringBuffer("SELECT ");
01344       sqlStat.append(" tests.test_no,tests.ser_no,tests.locn_name,tests.test_date,sct_tstdcsinfo.t0 FROM tests,test_rawdata,sct_tstdcsinfo");
01345       sqlStat.append(" WHERE tests.ser_no="+sn);
01346       sqlStat.append(" AND ( (tests.test_name = 'HybTWalk' AND tests.locn_name !='Oxford') OR (tests.test_name = 'HybNoise' AND tests.locn_name = 'Oxford'))");
01347 
01348       sqlStat.append(" AND tests.test_no = sct_tstdcsinfo.test_no AND tests.test_no=test_rawdata.test_no");
01349       sqlStat.append(" ORDER BY tests.ser_no,tests.test_date,tests.test_no");
01350 
01351      List itemList = new ArrayList();
01352 
01353      Statement statement = SCTDBInterface.getInstance().connection.createStatement();
01354      ResultSet resultSet = statement.executeQuery(sqlStat.toString());
01355      testNumberList = new Vector();
01356      for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
01357           testNumberList.addElement(resultSet.getString(1));
01358           List thisLine = new ArrayList();
01359           thisLine.add(resultSet.getString(2));  //sn
01360           thisLine.add(resultSet.getString(3)); // location
01361           thisLine.add(guiUtilities.DaveUtils.extractDate(resultSet.getString(4)));
01362           thisLine.add(resultSet.getString(5));
01363           itemList.add(thisLine);
01364          }
01365 
01366       statement.close();
01367 
01368       return itemList;
01369 
01370       }
01371 
01372         
01373      
01374 
01375 
01376 }

Generated on Fri Jan 14 12:49:52 2005 for SCT DAQ/DCS Software - Java by doxygen 1.3.5