00001 package DisplayGUI;
00002 import java.util.*;
00003 import java.util.regex.*;
00004 import java.io.*;
00005 import ProdDatabase.*;
00006 import Preferences.*;
00007
00008 public class UploadManager implements TestDataInfo, SCTDBInfo {
00009 gui guiControl;
00010 Map uploads, dcsMap, testMap, snMap;
00011 String testregex,user,username;
00012 Pattern runPattern = Pattern.compile("Run [Nn]umber\\s*:\\s*(.*)");
00013 Pattern serialNoPattern = Pattern.compile("SERIAL NUMBER\\s*:\\s*(\\d{14}|CRATE\\d{2}CHAN\\d{2}).*");
00014 Pattern snPattern = Pattern.compile("(\\d{14})");
00015 Pattern testDatePattern = Pattern.compile("TEST[_\\s]+DATE\\s*:\\s*(\\d+)/(\\d+)/(\\d+)");
00016 Pattern locationPattern = Pattern.compile("LOCATION\\s*(NAME|MADE)\\s*:.*");
00017 Pattern userPattern = Pattern.compile("TEST\\s*MADE\\s*BY.*");
00018 Pattern timeFlag = Pattern.compile("^#TIME$");
00019 Pattern versionFlag = Pattern.compile("^#VERSION\\s*$");
00020 Pattern testPattern;
00021 Pattern timePattern = Pattern.compile("\"\\d{2}:\\d{2}:\\d{2}\"");
00022 TestSelection testSelection;
00023 IVTestSelection ivTestSelection;
00024 PreferencesInterface prefs;
00025 boolean dcsWarning=false;
00026 boolean dcsIsMissing=false;
00027
00028
00029 public UploadManager(gui parent) {
00030
00031 guiControl = parent;
00032
00033 snMap = new HashMap();
00034
00035 createSignatureMap();
00036
00037 prefs = PreferencesInterface.getInstance();
00038
00039
00040 uploads = new HashMap();
00041 testSelection = guiControl.tablesDisplayPane.getTestSelection();
00042 ivTestSelection = guiControl.tablesDisplayPane.getIVTestSelection();
00043
00044 if(checkVariables()) {
00045 getTestRegex();
00046 getSignatures();
00047 removePreviousUploads();
00048 if(uploads.size()>0 && dcsOK()) {
00049 createUploadFiles();
00050 String classpath = System.getProperty("guiclasspath", null);
00051 if(classpath!=null) {
00052 String cmd = "java -cp "+classpath+" UploadTestData "+prefs.getPreference(PreferencesInterface.UPLOAD_DIR)+"/*.upl "+username+" "+prefs.getPreference(PreferencesInterface.PASSWORD);
00053 guiControl.uploader.start(uploads.size(), cmd, false);
00054 }
00055 }
00056 }
00057 }
00058
00059 boolean checkVariables() {
00060 if(testSelection!=null) {
00061 int testIndex = testSelection.getTestIndex();
00062 if(testIndex==-1) return false;
00063 if(!testIsSCTDAQ[testIndex]) {
00064 javax.swing.JOptionPane.showMessageDialog(null,"Uploads are not possible for "+testNames[testIndex]+" Tests.","SCT Database Upload",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00065 return false;
00066 }
00067 int status = testSelection.getTestStatus();
00068 if(status!=1) {
00069 javax.swing.JOptionPane.showMessageDialog(null,"Test is not finished. Please wait until completion before attempting to upload.","SCT Database Upload",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00070 return false;
00071 }
00072 }
00073
00074 else if(ivTestSelection==null) return false;
00075
00076
00077 if(guiControl.uploader.isBusy()) {
00078 javax.swing.JOptionPane.showMessageDialog(null,"An upload is currently in progress... please wait until it is finished.","SCT Database Upload",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00079 return false;
00080 }
00081 if(prefs.getPreference(PreferencesInterface.INSTITUTE).equals(PreferencesInterface.unDefinedString)) {
00082 javax.swing.JOptionPane.showMessageDialog(null,"You must define your 'Upload Institute' before uploading anything... use the Preferences menu.","SCT Database Upload",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00083 return false;
00084 }
00085 if(prefs.getPreference(PreferencesInterface.PASSWORD).equals(PreferencesInterface.unDefinedString)) {
00086 javax.swing.JOptionPane.showMessageDialog(null,"You must define your SCTDB password before uploading anything... use the Preferences menu.","SCT Database Upload",javax.swing.JOptionPane.INFORMATION_MESSAGE);
00087 return false;
00088 }
00089 try {
00090 Vector users = ProdDatabase.GeneralUtilities.getNameList(prefs.getPreference(PreferencesInterface.INSTITUTE));
00091 if(users.size()==0) return false;
00092 user = (String)users.firstElement();
00093 username = ProdDatabase.GeneralUtilities.getUserName(prefs.getPreference(PreferencesInterface.INSTITUTE));
00094 if(username==null) {
00095 System.err.println("SctGUI::UploadManager - No username found for your location");
00096 return false;
00097 }
00098 }catch(Exception db){System.err.println("SctGUI::UploadManager - failed to get username - "+db.toString()); return false;}
00099
00100 return true;
00101 }
00102
00103
00104 private void removePreviousUploads() {
00105
00106 int runno=-1,scanno=-1;
00107
00108 if(testSelection!=null) {
00109 runno = testSelection.getRunNo();
00110 scanno = testSelection.getScanNo();
00111 }
00112 else if(ivTestSelection!=null) {
00113 runno = ivTestSelection.getRunNo();
00114 scanno = ivTestSelection.getScanNo();
00115 }
00116
00117 if(runno==-1 || scanno==-1) {
00118 uploads=new HashMap();
00119 return;
00120 }
00121 String runNo = Integer.toString(runno)+"-"+Integer.toString(scanno);
00122 int testIndex = (testSelection!=null) ? testSelection.getTestIndex() : TEST_IV;
00123 try {
00124 ProdDatabase.GeneralUtilities.removePreviousUploads(sctdaqDBTestNames[testIndex], runNo, prefs.getPreference(PreferencesInterface.INSTITUTE), uploads);
00125 }catch(Exception db){System.err.println("SctGUI::UploadManager - error checking previous uploads - "+db.toString()); uploads=new HashMap();}
00126 }
00127
00128 private void getTestRegex() {
00129 StringBuffer regex=new StringBuffer();
00130 int count=0;
00131 for(int i=0;i<resultFileKeyNames.length;i++) {
00132 if(resultFileKeyNames[i].equals("")) continue;
00133 if(count!=0) regex.append("|");
00134 count++;
00135 regex.append("%"+resultFileKeyNames[i]);
00136 }
00137 testregex=regex.toString();
00138 testPattern = Pattern.compile("^"+testregex);
00139 }
00140
00141 private void getSignatures() {
00142
00143 String header=null;
00144 if(testSelection!=null) header = testSelection.getResultObjectHeader();
00145 if(header==null) header = ivTestSelection.getResultObjectHeader();
00146 if(header==null) return;
00147
00148
00149
00150 for(Enumeration e = ConfigurationInterface.getInstance().getSerialNumberMap(guiControl.displayPane.getSCTView()).elements(); e.hasMoreElements();) {
00151 ModuleCell thisCell = (ModuleCell) e.nextElement();
00152 String sn = thisCell.getSerialNo();
00153 String objectName = header+sn;
00154
00155 try {
00156 if(!Sct.IS.SctNames.getISRepository().contains(objectName)) continue;
00157 if(!ProdDatabase.GeneralUtilities.itemExists(sn)) {
00158 System.out.println(sn+" does not exist in the database!");
00159 continue;
00160 }
00161 TestSummaryIS result = new TestSummaryIS();
00162 Sct.IS.SctNames.getISRepository().getValue(objectName, result);
00163 String signature = getSignature(sn,result.dataString);
00164 if(signature!=null) uploads.put(signature,objectName);
00165 }catch(Exception e2){System.err.println("SctGUI::UploadManager - failed to retrieve signatures: "+e2.toString());}
00166 }
00167
00168 }
00169
00170 private String getSignature(String sn, String resultObject) {
00171
00172 Matcher matcher;
00173
00174 Hashtable thisHash = new Hashtable();
00175
00176 boolean nextLineIsTime=false;
00177 boolean hasDCS=false;
00178
00179 String[] lines = resultObject.split("\\n");
00180
00181 for(int i=0;i<lines.length;i++) {
00182 String line = lines[i];
00183 if(line.equals("")) continue;
00184
00185 if(line.startsWith("%DCS_INFO")) hasDCS=true;
00186
00187 matcher = testPattern.matcher(line);
00188 if(matcher.matches()) {
00189 String theScanType = line.substring(matcher.start()+1,matcher.end());
00190 thisHash.put("TEST",(String)testMap.get(theScanType));
00191 break;
00192 }
00193 matcher = serialNoPattern.matcher(line);
00194 if(matcher.matches()) {
00195 String thisSN = line.substring(matcher.start(1),matcher.end(1));
00196 if(!thisSN.startsWith("CRATE") && !thisSN.equals(sn)) continue;
00197 thisHash.put("SERIALNO",sn);
00198 snMap.put(thisSN,sn);
00199 continue;
00200 }
00201 matcher = runPattern.matcher(line);
00202 if(matcher.matches()) {
00203 thisHash.put("RUNNO",line.substring(matcher.start(1),matcher.end(1)));
00204 continue;
00205 }
00206 matcher = testDatePattern.matcher(line);
00207 if(matcher.matches()) {
00208 thisHash.put("DATE",getDate(line));
00209 continue;
00210 }
00211 if(nextLineIsTime) {
00212 matcher=timePattern.matcher(line);
00213 if(matcher.matches()) {
00214 thisHash.put("TIME",line.substring(matcher.start()+1,matcher.end()-1));
00215 continue;
00216 }
00217 }
00218
00219 nextLineIsTime = timeFlag.matcher(line).matches();
00220 }
00221
00222 if(thisHash.size()!=5) {
00223 System.err.println("SctGUI::UploadManager Missing info in signature");
00224 System.out.println(thisHash);
00225 return null;
00226 }
00227
00228 StringBuffer thisSignature = new StringBuffer();
00229 if(thisHash.containsKey("SERIALNO")) thisSignature.append((String)thisHash.get("SERIALNO"));
00230 if(thisHash.containsKey("TEST")) thisSignature.append("_"+(String)thisHash.get("TEST"));
00231 if(thisHash.containsKey("RUNNO")) thisSignature.append("_"+(String)thisHash.get("RUNNO"));
00232 if(thisHash.containsKey("DATE")) thisSignature.append("_"+(String)thisHash.get("DATE"));
00233 if(thisHash.containsKey("TIME")) thisSignature.append("_"+(String)thisHash.get("TIME"));
00234
00235 if(!hasDCS) dcsIsMissing=true;
00236
00237 return thisSignature.toString();
00238 }
00239
00240
00241 public void createUploadFiles() {
00242
00243 Calendar rightnow = Calendar.getInstance();
00244 long nowtime = rightnow.getTimeInMillis();
00245 File uploadDir = new File(prefs.getPreference(PreferencesInterface.UPLOAD_DIR));
00246 File[] uploadFiles = uploadDir.listFiles(new UploadFileFilter());
00247 for(int i=0;i<uploadFiles.length;i++) {
00248 try {
00249 System.out.println("Deleting upload file "+uploadFiles[i].getAbsolutePath());
00250 uploadFiles[i].delete();
00251 }catch(Exception io1){System.err.println("SctGUI::UploadManager - failed to delete old upload files");}
00252 }
00253
00254 int fileCount=0;
00255
00256 try {
00257 for (Iterator i=uploads.entrySet().iterator(); i.hasNext(); ) {
00258 Map.Entry e = (Map.Entry) i.next();
00259 String objectName = (String) e.getValue();
00260
00261 TestSummaryIS result = new TestSummaryIS();
00262 Sct.IS.SctNames.getISRepository().getValue(objectName, result);
00263 String resultString = result.dataString;
00264
00265 resultString = processUploadFile(resultString);
00266
00267
00268 Long tname = new Long(nowtime);
00269 String fname = "a"+tname.toString()+".upl";
00270 File tmpFile = new File(uploadDir,fname);
00271 if(tmpFile.exists()) {
00272 nowtime+=1000;
00273 tname = new Long(nowtime);
00274 fname = "a"+tname.toString()+".upl";
00275 }
00276 tmpFile = new File(uploadDir,fname);
00277 if(tmpFile.exists()) {
00278 System.out.println("ERROR: Upload file "+fname+" not created.");
00279 continue;
00280 }
00281
00282 System.out.println("Creating "+tmpFile.getAbsolutePath());
00283 BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
00284 out.write(resultString);
00285 out.close();
00286 nowtime++;
00287 fileCount++;
00288 }
00289 System.out.println("Created "+fileCount+" database upload files.");
00290 }catch(Exception e) {
00291 System.out.println("Error writing upload file(s) to "+uploadDir);
00292 }
00293 }
00294
00295 private String processUploadFile(String resultString) {
00296 String[] lines = resultString.split("\\n");
00297 String sn=null;
00298 StringBuffer newString = new StringBuffer();
00299 int lineCount=0;
00300 boolean nextLineIsVersion=false;
00301 boolean dcsInfoIsPresent=false;
00302 for(int i=0;i<lines.length;i++) {
00303 String line = lines[i];
00304 if(line.equals("")) continue;
00305 if(lineCount>0) newString.append("\n");
00306 lineCount++;
00307
00308 if(line.startsWith("%DCS_INFO")) dcsInfoIsPresent=true;
00309
00310 Matcher matcher = serialNoPattern.matcher(line);
00311 if(matcher.matches()) {
00312 sn = (String)snMap.get(line.substring(matcher.start(1),matcher.end(1)));
00313 line = "SERIAL NUMBER : "+sn;
00314 newString.append(line);
00315 continue;
00316 }
00317
00318 matcher = testDatePattern.matcher(line);
00319 if(matcher.matches()) {
00320 line = "TEST DATE : "+getDate(line);
00321 newString.append(line);
00322 continue;
00323 }
00324 matcher = locationPattern.matcher(line);
00325 if(matcher.matches()) {
00326 line = "LOCATION NAME : "+prefs.getPreference(PreferencesInterface.INSTITUTE);
00327 newString.append(line);
00328 continue;
00329 }
00330 matcher = runPattern.matcher(line);
00331 if(matcher.matches()) {
00332 line = "Run number : "+line.substring(matcher.start(1),matcher.end(1));
00333 newString.append(line);
00334 continue;
00335 }
00336 matcher = userPattern.matcher(line);
00337 if(matcher.matches()) {
00338 line = "TEST MADE BY : "+user;
00339 newString.append(line);
00340 continue;
00341 }
00342 matcher = testPattern.matcher(line);
00343 if(matcher.matches() && !dcsInfoIsPresent && dcsMap!=null && sn!=null) {
00344
00345 SummaryReader.DCSInfo dcsInfo = (SummaryReader.DCSInfo)dcsMap.get(sn);
00346 if(dcsInfo!=null) appendDCSInfo(newString,dcsInfo);
00347 }
00348
00349 if(nextLineIsVersion) {
00350 if(!line.startsWith("\"SctRodDaq")) {
00351 line = "\"SctRodDaq_DCS_"+line.substring(1);
00352 newString.append(line);
00353 nextLineIsVersion=false;
00354 continue;
00355 }
00356 }
00357 nextLineIsVersion = versionFlag.matcher(line).matches();
00358
00359 newString.append(line);
00360 }
00361 return newString.toString();
00362 }
00363
00364 private boolean dcsOK() {
00365 if(!dcsIsMissing) return true;
00366 Map dcsMap = guiControl.isInterface.getDCSMap(guiControl.tablesDisplayPane.getTestControlObjectName());
00367 if(dcsMap==null) {
00368 dcsWarning=true;
00369 int response = javax.swing.JOptionPane.showConfirmDialog(null,"There is no DCS information available for this test.\nUploading without DCS info is not recommended!\nDo you want to continue?","No DCS Info",javax.swing.JOptionPane.YES_NO_OPTION,javax.swing.JOptionPane.QUESTION_MESSAGE);
00370 if(response==javax.swing.JOptionPane.YES_OPTION) return true;
00371 else return false;
00372 }
00373 for (Iterator i=uploads.entrySet().iterator(); i.hasNext(); ) {
00374 Map.Entry e = (Map.Entry) i.next();
00375 String signature = (String) e.getKey();
00376 String sn = signature.substring(0,14);
00377 if(!dcsMap.containsKey(sn)) {
00378 int response = javax.swing.JOptionPane.showConfirmDialog(null,"There is no DCS information available for some or all of these tests.\nUploading without DCS info is not recommended!\nDo you want to continue?","No DCS Info",javax.swing.JOptionPane.YES_NO_OPTION,javax.swing.JOptionPane.QUESTION_MESSAGE);
00379 if(response!=javax.swing.JOptionPane.YES_OPTION) return false;
00380 }
00381 }
00382 return true;
00383
00384 }
00385
00386 private void appendDCSInfo(StringBuffer newString, SummaryReader.DCSInfo dcsInfo) {
00387 newString.append("%DCS_INFO");
00388 newString.append("\n#T0 T1");
00389 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.T0)+" "+dcsInfo.get(SummaryReader.DCSInfo.T0));
00390 newString.append("\n#VDET IDET");
00391 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VDET)+" "+dcsInfo.get(SummaryReader.DCSInfo.IDET));
00392 newString.append("\n#VCC ICC");
00393 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VCC)+" "+dcsInfo.get(SummaryReader.DCSInfo.ICC));
00394 newString.append("\n#VDD IDD");
00395 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VDD)+" "+dcsInfo.get(SummaryReader.DCSInfo.IDD));
00396 newString.append("\n#TIME_POWERED\n.\n");
00397 }
00398
00399 private void createSignatureMap() {
00400 testMap = new HashMap();
00401 for(int i=0;i<sctdaqDBTestNames.length;i++) {
00402 testMap.put(resultFileKeyNames[i],sctdaqDBTestNames[i]);
00403 }
00404 }
00405 private String getDate(String line) {
00406 Matcher matcher = testDatePattern.matcher(line);
00407 if(matcher.matches()) {
00408 String day = line.substring(matcher.start(1),matcher.end(1));
00409 String month = line.substring(matcher.start(2),matcher.end(2));
00410 String year = line.substring(matcher.start(3),matcher.end(3));
00411 if(month.length()==1) month="0"+month;
00412 if(day.length()==1) day="0"+day;
00413 return day+"/"+month+"/"+year;
00414 }
00415 return null;
00416 }
00417
00418
00419 }