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 SortedMap[] serialNumberMap = new TreeMap[sctViews];
00024 String uploadDir,institute,password,username,undefinedstring;
00025 gui guiControl;
00026
00027
00028
00029 public SCTDBAssemblyChecker(gui parent, SortedMap[] 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].keySet(),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].entrySet().iterator(); j.hasNext(); ) {
00083 Map.Entry e = (Map.Entry) j.next();
00084 String thisSerialNo = (String)e.getKey();
00085 ModuleCell thisCell = (ModuleCell)e.getValue();
00086 if(assembledMap.containsKey(thisSerialNo)) {
00087 SCTDBAssemblyInfo aInfo = (SCTDBAssemblyInfo)assembledMap.get(thisSerialNo);
00088 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());
00089 }
00090 else {
00091 newHash.put(thisSerialNo,new SCTDBAssemblyInfo(thisCell.getRow(),thisCell.getPosition()));
00092 newModuleCount++;
00093 }
00094 }
00095 if(newHash.size()>0) newModulesMap.put(SCTSerialNumbers[i],newHash);
00096 }
00097 }catch(Exception e) {errors.add("Exception reading from database - "+e.toString());}
00098
00099 if(errors.size()>0) {
00100 writeSCTDBErrors(errors);
00101 return;
00102 }
00103
00104 int nUploaded=0;
00105 for(int i=0;i<4;i++) {
00106 if(!newModulesMap.containsKey(SCTSerialNumbers[i])) continue;
00107 Map mMap = (Map) newModulesMap.get(SCTSerialNumbers[i]);
00108 int nmodules = mMap.size();
00109 if(nmodules==0) continue;
00110 Object[] options = { "Register now", "Do it later" };
00111 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]);
00112 if(response!=JOptionPane.YES_OPTION) return;
00113
00114 if(nUploaded>0) {
00115 System.out.println("SctGUI - more than one barrel/endcap has new modules? Not uploading modules for "+SCTViewNames[i]);
00116 return;
00117 }
00118
00119
00120 String uploadFile = writeUploadAssemblyFile(SCTSerialNumbers[i],mMap);
00121 if(uploadFile==null) {
00122 javax.swing.JOptionPane.showMessageDialog(null,"Failed to upload assembly information","SCT Database",JOptionPane.ERROR_MESSAGE);
00123 return;
00124 }
00125 String classpath = System.getProperty("guiclasspath", null);
00126 if(classpath!=null) {
00127 String cmd = "java -cp "+classpath+" UploadAssmData "+uploadFile+" "+username+" "+password;
00128
00129
00130
00131 int nCounts= nmodules+1;
00132 guiControl.uploader.start(nCounts, cmd, false);
00133 }
00134 nUploaded++;
00135 }
00136
00137
00138 }
00139 public void writeSCTDBErrors(List errors) {
00140
00141 File errorFile = new File(uploadDir,"SCTDBerrors.log");
00142
00143 try {
00144 BufferedWriter out = new BufferedWriter(new FileWriter(errorFile));
00145 for (ListIterator i = errors.listIterator(); i.hasNext(); ) {
00146 out.write((String)i.next());
00147 out.newLine();
00148 }
00149 out.close();
00150 }catch(Exception e){System.err.println("SctGUI - Failed to create error log file - "+e.toString());}
00151
00152 String error_msg = (errors.size()==1) ? "There is one database error\n" : "There are "+Integer.toString(errors.size())+" database errors.\n";
00153 StringBuffer emsg = new StringBuffer(error_msg);
00154 emsg.append("Please view log file at "+errorFile.getAbsolutePath()+"\n");
00155 emsg.append("and correct the error before uploading any further modules.");
00156 javax.swing.JOptionPane.showMessageDialog(null,emsg.toString(),"SCT Database",JOptionPane.ERROR_MESSAGE);
00157 }
00158
00159
00160 public Hashtable getAssemblyHash(String serialno) throws Exception {
00161 Hashtable tempHash = new Hashtable();
00162
00163 Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00164 ResultSet resultSet = statement.executeQuery("SELECT ser_no,posn from ASSM_ITEMS where assm_ser_no="+serialno);
00165 for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00166 String sn = resultSet.getString(1);
00167 tempHash.put(sn,new SCTDBAssemblyInfo(resultSet.getString(2)));
00168 }
00169 statement.close();
00170
00171 return tempHash;
00172 }
00173
00174 public Set getModuleSet(Set moduleList, String location) throws Exception {
00175 Set unknownModules = new HashSet(moduleList);
00176
00177 Statement statement = SCTDBInterface.getInstance().connection.createStatement();
00178
00179 ResultSet resultSet = statement.executeQuery("SELECT ser_no from ITEMS where locn_name='"+location+"' AND ctype LIKE '%Module%'");
00180 for(boolean n = resultSet.next() ; n==true ; n=resultSet.next() ){
00181 String sn =resultSet.getString(1);
00182 unknownModules.remove(sn);
00183 }
00184 statement.close();
00185
00186 return unknownModules;
00187 }
00188
00189
00190 public String writeUploadAssemblyFile(String serialno, Map moduleMap) {
00191 File fname = new File(uploadDir,serialno+".jassm");
00192 try {
00193 BufferedWriter out = new BufferedWriter(new FileWriter(fname));
00194 out.write("%Assembly");
00195 out.newLine();
00196 out.write("ASSEMBLY ITEM : "+serialno);
00197 out.newLine();
00198
00199 String todaysDate = guiUtilities.DaveUtils.getTodaysDate();
00200 int nuploads = moduleMap.size();
00201 int count=0;
00202 for (Iterator i=moduleMap.entrySet().iterator(); i.hasNext(); ) {
00203 Map.Entry e = (Map.Entry) i.next();
00204 String moduleSerialNo = (String)e.getKey();
00205 SCTDBAssemblyInfo a = (SCTDBAssemblyInfo)e.getValue();
00206 out.write("# index "+a.getIndex()+" row "+a.getRow()+" pos "+a.getPos());
00207 out.newLine();
00208 out.write(moduleSerialNo+" "+Integer.toString(a.getIndex())+" "+todaysDate);
00209 count++;
00210 if(count<nuploads) out.newLine();
00211 }
00212 out.close();
00213 }catch(Exception e){
00214 System.err.println("SctGUI - Failed to create assembly upload file - "+e.toString());
00215 return null;
00216 }
00217 return fname.getAbsolutePath();
00218 }
00219
00220
00221
00222 boolean checkVariables() {
00223
00224
00225 if(guiControl.getUploaderStatus()) {
00226 javax.swing.JOptionPane.showMessageDialog(null,"An upload is currently in progress... cannot upload assembly information.","SCT Database Assembly Uploader",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00227 return false;
00228 }
00229 if(institute.equals(undefinedstring)) {
00230 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);
00231 return false;
00232 }
00233 if(password.equals(undefinedstring)) {
00234 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);
00235 return false;
00236 }
00237 try {
00238 username = ProdDatabase.GeneralUtilities.getUserName(institute);
00239 if(username==null) {
00240 javax.swing.JOptionPane.showMessageDialog(null,"No username is available for location "+institute,"SCT Database Assembly Uploader",javax.swing.JOptionPane.ERROR_MESSAGE);
00241 return false;
00242 }
00243 }catch(Exception db){
00244 javax.swing.JOptionPane.showMessageDialog(null,"Exception "+db.toString(),"SCT Database Error",javax.swing.JOptionPane.ERROR_MESSAGE);
00245 return false;
00246 }
00247
00248 return true;
00249 }
00250
00251
00252
00253
00254
00255 }