00001 package DisplayGUI;
00002
00003 import java.io.*;
00004 import java.util.*;
00005 import is.*;
00006 import Sct.Serializable;
00007 import Sct.IS.*;
00008
00009 import java.sql.*;
00010 import javax.swing.JOptionPane;
00011 import ProdDatabase.SCTDBInterface;
00012
00013
00014
00015
00016
00017
00022 public class SCTDBAssemblyChecker extends Thread implements SCTInfo {
00023 private Set[] serialNumberMap = new TreeSet[sctViews];
00024 String uploadDir,institute,password,username,undefinedstring;
00025 gui guiControl;
00026
00027
00028
00029 public SCTDBAssemblyChecker(gui parent, Set[] serialNumberMap, String uploadDir, String institute, String password, String undefinedstring) {
00030 super("SCTDB_DOWNLOAD");
00031 guiControl=parent;
00032 for(int i=0;i<sctViews;i++) this.serialNumberMap[i] = serialNumberMap[i];
00033 this.uploadDir = uploadDir;
00034 this.institute=institute;
00035 this.password=password;
00036 this.undefinedstring=undefinedstring;
00037 username=null;
00038
00039 setPriority(Thread.MIN_PRIORITY);
00040
00041 }
00042
00043
00044 public boolean isOkToStart() {
00045
00046 if(!checkVariables()) return false;
00047
00048 Thread[] allThreads = new Thread[Thread.activeCount()];
00049 Thread.enumerate(allThreads);
00050 for(int i=0;i<allThreads.length;i++) {
00051 if(allThreads[i]!=null && allThreads[i].getName().equals("SCTDB_DOWNLOAD") && allThreads[i].isAlive()) {
00052 javax.swing.JOptionPane.showMessageDialog(null,"A download from the SCT database is already in progress.\nPlease wait for this to complete before requesting another download.");
00053 return false;
00054 }
00055 }
00056 return true;
00057 }
00058
00059 public void run() {
00060
00061
00062
00063 List errors = new ArrayList();
00064 Map newModulesMap = new HashMap();
00065 int newModuleCount=0;
00066
00067
00068 try {
00069 for(int i=0;i<4;i++) {
00070 if(serialNumberMap[i].size()==0) continue;
00071 Map newHash = new HashMap();
00072 Hashtable assembledMap = getAssemblyHash(SCTSerialNumbers[i]);
00073
00074
00075
00076 Set unknownModuleSet = getModuleSet(serialNumberMap[i],institute);
00077 if(unknownModuleSet.size()>0) {
00078 for (Iterator j = unknownModuleSet.iterator(); j.hasNext(); ) errors.add((String)j.next()+" is not located at "+institute+" and cannot be assembled.");
00079 }
00080
00081
00082 for (Iterator j=serialNumberMap[i].iterator(); j.hasNext(); ) {
00083 String thisSerialNo = (String) j.next();
00084 ModuleCell thisCell = ConfigurationInterface.getInstance().getModuleCell(thisSerialNo);
00085 if(assembledMap.containsKey(thisSerialNo)) {
00086 SCTDBAssemblyInfo aInfo = (SCTDBAssemblyInfo)assembledMap.get(thisSerialNo);
00087 if(thisCell.getRow()!=aInfo.getRow() || thisCell.getPosition()!=aInfo.getPos()) errors.add(thisSerialNo+" SctRodDaq/SCTDB mismatch : Row "+thisCell.getRow()+"/"+aInfo.getRow()+" Pos "+thisCell.getPosition()+"/"+aInfo.getPos());
00088 }
00089 else {
00090 newHash.put(thisSerialNo,new SCTDBAssemblyInfo(thisCell.getRow(),thisCell.getPosition()));
00091 newModuleCount++;
00092 }
00093 }
00094 if(newHash.size()>0) newModulesMap.put(SCTSerialNumbers[i],newHash);
00095 }
00096 }catch(Exception e) {errors.add("Exception reading from database - "+e.toString());}
00097
00098 if(errors.size()>0) {
00099 writeSCTDBErrors(errors);
00100 return;
00101 }
00102
00103 int nUploaded=0;
00104 for(int i=0;i<4;i++) {
00105 if(!newModulesMap.containsKey(SCTSerialNumbers[i])) continue;
00106 Map mMap = (Map) newModulesMap.get(SCTSerialNumbers[i]);
00107 int nmodules = mMap.size();
00108 if(nmodules==0) continue;
00109 Object[] options = { "Register now", "Do it later" };
00110 int response = JOptionPane.showOptionDialog(null, "You have "+nmodules+" new modules on "+SCTViewNames[i]+". Do you want register their positions? ", "SCT Database",JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,null, options, options[0]);
00111 if(response!=JOptionPane.YES_OPTION) return;
00112
00113 if(nUploaded>0) {
00114 System.out.println("SctGUI - more than one barrel/endcap has new modules? Not uploading modules for "+SCTViewNames[i]);
00115 return;
00116 }
00117
00118
00119 String uploadFile = writeUploadAssemblyFile(SCTSerialNumbers[i],mMap);
00120 if(uploadFile==null) {
00121 javax.swing.JOptionPane.showMessageDialog(null,"Failed to upload assembly information","SCT Database",JOptionPane.ERROR_MESSAGE);
00122 return;
00123 }
00124 String classpath = System.getProperty("guiclasspath", null);
00125 if(classpath!=null) {
00126 String cmd = "java -cp "+classpath+" UploadAssmData "+uploadFile+" "+username+" "+password;
00127
00128
00129
00130 int nCounts= nmodules+1;
00131 guiControl.uploader.start(nCounts, cmd, false);
00132 }
00133 nUploaded++;
00134 }
00135
00136
00137 }
00138 public void writeSCTDBErrors(List errors) {
00139
00140 File errorFile = new File(uploadDir,"SCTDBerrors.log");
00141
00142 try {
00143 BufferedWriter out = new BufferedWriter(new FileWriter(errorFile));
00144 for (ListIterator i = errors.listIterator(); i.hasNext(); ) {
00145 out.write((String)i.next());
00146 out.newLine();
00147 }
00148 out.close();
00149 }catch(Exception e){System.err.println("SctGUI - Failed to create error log file - "+e.toString());}
00150
00151 String error_msg = (errors.size()==1) ? "There is one database error\n" : "There are "+Integer.toString(errors.size())+" database errors.\n";
00152 StringBuffer emsg = new StringBuffer(error_msg);
00153 emsg.append("Please view log file at "+errorFile.getAbsolutePath()+"\n");
00154 emsg.append("and correct the error before uploading any further modules.");
00155 javax.swing.JOptionPane.showMessageDialog(null,emsg.toString(),"SCT Database",JOptionPane.ERROR_MESSAGE);
00156 }
00157
00158
00159 public Hashtable getAssemblyHash(String serialno) throws Exception {
00160 Hashtable tempHash = new Hashtable();
00161
00162 Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00163 ResultSet resultSet = statement.executeQuery("SELECT ser_no,posn from ASSM_ITEMS where assm_ser_no="+serialno);
00164 for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00165 String sn = resultSet.getString(1);
00166 tempHash.put(sn,new SCTDBAssemblyInfo(resultSet.getString(2)));
00167 }
00168 statement.close();
00169
00170 return tempHash;
00171 }
00172
00173 public Set getModuleSet(Set moduleList, String location) throws Exception {
00174 Set unknownModules = new HashSet(moduleList);
00175
00176 Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00177
00178 ResultSet resultSet = statement.executeQuery("SELECT ser_no from ITEMS where locn_name='"+location+"' AND ctype LIKE '%Module%'");
00179 for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00180 String sn =resultSet.getString(1);
00181 unknownModules.remove(sn);
00182 }
00183 statement.close();
00184
00185 return unknownModules;
00186 }
00187
00188
00189 public String writeUploadAssemblyFile(String serialno, Map moduleMap) {
00190 File fname = new File(uploadDir,serialno+".jassm");
00191 try {
00192 BufferedWriter out = new BufferedWriter(new FileWriter(fname));
00193 out.write("%Assembly");
00194 out.newLine();
00195 out.write("ASSEMBLY ITEM : "+serialno);
00196 out.newLine();
00197
00198 String todaysDate = guiUtilities.DaveUtils.getTodaysDate();
00199 int nuploads = moduleMap.size();
00200 int count=0;
00201 for (Iterator i=moduleMap.entrySet().iterator(); i.hasNext(); ) {
00202 Map.Entry e = (Map.Entry) i.next();
00203 String moduleSerialNo = (String)e.getKey();
00204 SCTDBAssemblyInfo a = (SCTDBAssemblyInfo)e.getValue();
00205 out.write("# index "+a.getIndex()+" row "+a.getRow()+" pos "+a.getPos());
00206 out.newLine();
00207 out.write(moduleSerialNo+" "+Integer.toString(a.getIndex())+" "+todaysDate);
00208 count++;
00209 if(count<nuploads) out.newLine();
00210 }
00211 out.close();
00212 }catch(Exception e){
00213 System.err.println("SctGUI - Failed to create assembly upload file - "+e.toString());
00214 return null;
00215 }
00216 return fname.getAbsolutePath();
00217 }
00218
00219
00220
00221 boolean checkVariables() {
00222
00223
00224 if(guiControl.getUploaderStatus()) {
00225 javax.swing.JOptionPane.showMessageDialog(null,"An upload is currently in progress... cannot upload assembly information.","SCT Database Assembly Uploader",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00226 return false;
00227 }
00228 if(institute.equals(undefinedstring)) {
00229 javax.swing.JOptionPane.showMessageDialog(null,"You must define your 'Upload Institute' before uploading anything... use the Preferences menu.","SCT Database Assembly Uploader",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00230 return false;
00231 }
00232 if(password.equals(undefinedstring)) {
00233 javax.swing.JOptionPane.showMessageDialog(null,"You must define your SCTDB password before uploading anything... use the Preferences menu.","SCT Database Assembly Uploader",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00234 return false;
00235 }
00236 try {
00237 username = ProdDatabase.GeneralUtilities.getUserName(institute);
00238 if(username==null) {
00239 javax.swing.JOptionPane.showMessageDialog(null,"No username is available for location "+institute,"SCT Database Assembly Uploader",javax.swing.JOptionPane.ERROR_MESSAGE);
00240 return false;
00241 }
00242 }catch(Exception db){
00243 javax.swing.JOptionPane.showMessageDialog(null,"Exception "+db.toString(),"SCT Database Error",javax.swing.JOptionPane.ERROR_MESSAGE);
00244 return false;
00245 }
00246
00247 return true;
00248 }
00249
00250
00251
00252
00253
00254 }