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.getUploaderStatus()) {
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 (java.util.Iterator m = ConfigurationInterface.getInstance().getSerialNumberList(guiControl.selectorPane.getViewCategoryIndex(),guiControl.selectorPane.getViewIndex()).iterator(); m.hasNext(); ) {
00151 String sn=(String)m.next();
00152 String objectName = header+sn;
00153
00154 try {
00155 if(!Sct.IS.SctNames.getISRepository().contains(objectName)) continue;
00156 if(!ProdDatabase.GeneralUtilities.itemExists(sn)) {
00157 System.out.println(sn+" does not exist in the database!");
00158 continue;
00159 }
00160 TestSummaryIS result = new TestSummaryIS();
00161 Sct.IS.SctNames.getISRepository().getValue(objectName, result);
00162 String signature = getSignature(sn,result.dataString);
00163 if(signature!=null) uploads.put(signature,objectName);
00164 }catch(Exception e2){System.err.println("SctGUI::UploadManager - failed to retrieve signatures: "+e2.toString());}
00165 }
00166
00167 }
00168
00169 private String getSignature(String sn, String resultObject) {
00170
00171 Matcher matcher;
00172
00173 Hashtable thisHash = new Hashtable();
00174
00175 boolean nextLineIsTime=false;
00176 boolean hasDCS=false;
00177
00178 String[] lines = resultObject.split("\\n");
00179
00180 for(int i=0;i<lines.length;i++) {
00181 String line = lines[i];
00182 if(line.equals("")) continue;
00183
00184 if(line.startsWith("%DCS_INFO")) hasDCS=true;
00185
00186 matcher = testPattern.matcher(line);
00187 if(matcher.matches()) {
00188 String theScanType = line.substring(matcher.start()+1,matcher.end());
00189 thisHash.put("TEST",(String)testMap.get(theScanType));
00190 break;
00191 }
00192 matcher = serialNoPattern.matcher(line);
00193 if(matcher.matches()) {
00194 String thisSN = line.substring(matcher.start(1),matcher.end(1));
00195 if(!thisSN.startsWith("CRATE") && !thisSN.equals(sn)) continue;
00196 thisHash.put("SERIALNO",sn);
00197 snMap.put(thisSN,sn);
00198 continue;
00199 }
00200 matcher = runPattern.matcher(line);
00201 if(matcher.matches()) {
00202 thisHash.put("RUNNO",line.substring(matcher.start(1),matcher.end(1)));
00203 continue;
00204 }
00205 matcher = testDatePattern.matcher(line);
00206 if(matcher.matches()) {
00207 thisHash.put("DATE",getDate(line));
00208 continue;
00209 }
00210 if(nextLineIsTime) {
00211 matcher=timePattern.matcher(line);
00212 if(matcher.matches()) {
00213 thisHash.put("TIME",line.substring(matcher.start()+1,matcher.end()-1));
00214 continue;
00215 }
00216 }
00217
00218 nextLineIsTime = timeFlag.matcher(line).matches();
00219 }
00220
00221 if(thisHash.size()!=5) {
00222 System.err.println("SctGUI::UploadManager Missing info in signature");
00223 System.out.println(thisHash);
00224 return null;
00225 }
00226
00227 StringBuffer thisSignature = new StringBuffer();
00228 if(thisHash.containsKey("SERIALNO")) thisSignature.append((String)thisHash.get("SERIALNO"));
00229 if(thisHash.containsKey("TEST")) thisSignature.append("_"+(String)thisHash.get("TEST"));
00230 if(thisHash.containsKey("RUNNO")) thisSignature.append("_"+(String)thisHash.get("RUNNO"));
00231 if(thisHash.containsKey("DATE")) thisSignature.append("_"+(String)thisHash.get("DATE"));
00232 if(thisHash.containsKey("TIME")) thisSignature.append("_"+(String)thisHash.get("TIME"));
00233
00234 if(!hasDCS) dcsIsMissing=true;
00235
00236 return thisSignature.toString();
00237 }
00238
00239
00240 public void createUploadFiles() {
00241
00242 Calendar rightnow = Calendar.getInstance();
00243 long nowtime = rightnow.getTimeInMillis();
00244 File uploadDir = new File(prefs.getPreference(PreferencesInterface.UPLOAD_DIR));
00245 File[] uploadFiles = uploadDir.listFiles(new UploadFileFilter());
00246 for(int i=0;i<uploadFiles.length;i++) {
00247 try {
00248 System.out.println("Deleting upload file "+uploadFiles[i].getAbsolutePath());
00249 uploadFiles[i].delete();
00250 }catch(Exception io1){System.err.println("SctGUI::UploadManager - failed to delete old upload files");}
00251 }
00252
00253 int fileCount=0;
00254
00255 try {
00256 for (Iterator i=uploads.entrySet().iterator(); i.hasNext(); ) {
00257 Map.Entry e = (Map.Entry) i.next();
00258 String objectName = (String) e.getValue();
00259
00260 TestSummaryIS result = new TestSummaryIS();
00261 Sct.IS.SctNames.getISRepository().getValue(objectName, result);
00262 String resultString = result.dataString;
00263
00264 resultString = processUploadFile(resultString);
00265
00266
00267 Long tname = new Long(nowtime);
00268 String fname = "a"+tname.toString()+".upl";
00269 File tmpFile = new File(uploadDir,fname);
00270 if(tmpFile.exists()) {
00271 nowtime+=1000;
00272 tname = new Long(nowtime);
00273 fname = "a"+tname.toString()+".upl";
00274 }
00275 tmpFile = new File(uploadDir,fname);
00276 if(tmpFile.exists()) {
00277 System.out.println("ERROR: Upload file "+fname+" not created.");
00278 continue;
00279 }
00280
00281 System.out.println("Creating "+tmpFile.getAbsolutePath());
00282 BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile));
00283 out.write(resultString);
00284 out.close();
00285 nowtime++;
00286 fileCount++;
00287 }
00288 System.out.println("Created "+fileCount+" database upload files.");
00289 }catch(Exception e) {
00290 System.out.println("Error writing upload file(s) to "+uploadDir);
00291 }
00292 }
00293
00294 private String processUploadFile(String resultString) {
00295 String[] lines = resultString.split("\\n");
00296 String sn=null;
00297 StringBuffer newString = new StringBuffer();
00298 int lineCount=0;
00299 boolean nextLineIsVersion=false;
00300 boolean dcsInfoIsPresent=false;
00301 for(int i=0;i<lines.length;i++) {
00302 String line = lines[i];
00303 if(line.equals("")) continue;
00304 if(lineCount>0) newString.append("\n");
00305 lineCount++;
00306
00307 if(line.startsWith("%DCS_INFO")) dcsInfoIsPresent=true;
00308
00309 Matcher matcher = serialNoPattern.matcher(line);
00310 if(matcher.matches()) {
00311 sn = (String)snMap.get(line.substring(matcher.start(1),matcher.end(1)));
00312 line = "SERIAL NUMBER : "+sn;
00313 newString.append(line);
00314 continue;
00315 }
00316
00317 matcher = testDatePattern.matcher(line);
00318 if(matcher.matches()) {
00319 line = "TEST DATE : "+getDate(line);
00320 newString.append(line);
00321 continue;
00322 }
00323 matcher = locationPattern.matcher(line);
00324 if(matcher.matches()) {
00325 line = "LOCATION NAME : "+prefs.getPreference(PreferencesInterface.INSTITUTE);
00326 newString.append(line);
00327 continue;
00328 }
00329 matcher = runPattern.matcher(line);
00330 if(matcher.matches()) {
00331 line = "Run number : "+line.substring(matcher.start(1),matcher.end(1));
00332 newString.append(line);
00333 continue;
00334 }
00335 matcher = userPattern.matcher(line);
00336 if(matcher.matches()) {
00337 line = "TEST MADE BY : "+user;
00338 newString.append(line);
00339 continue;
00340 }
00341 matcher = testPattern.matcher(line);
00342 if(matcher.matches() && !dcsInfoIsPresent && dcsMap!=null && sn!=null) {
00343
00344 SummaryReader.DCSInfo dcsInfo = (SummaryReader.DCSInfo)dcsMap.get(sn);
00345 if(dcsInfo!=null) appendDCSInfo(newString,dcsInfo);
00346 }
00347
00348 if(nextLineIsVersion) {
00349 if(!line.startsWith("\"SctRodDaq")) {
00350 line = "\"SctRodDaq_DCS_"+line.substring(1);
00351 newString.append(line);
00352 nextLineIsVersion=false;
00353 continue;
00354 }
00355 }
00356 nextLineIsVersion = versionFlag.matcher(line).matches();
00357
00358 newString.append(line);
00359 }
00360 return newString.toString();
00361 }
00362
00363 private boolean dcsOK() {
00364 if(!dcsIsMissing) return true;
00365 Map dcsMap = guiControl.isInterface.getDCSMap(guiControl.tablesDisplayPane.getTestControlObjectName());
00366 if(dcsMap==null) {
00367 dcsWarning=true;
00368 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);
00369 if(response==javax.swing.JOptionPane.YES_OPTION) return true;
00370 else return false;
00371 }
00372 for (Iterator i=uploads.entrySet().iterator(); i.hasNext(); ) {
00373 Map.Entry e = (Map.Entry) i.next();
00374 String signature = (String) e.getKey();
00375 String sn = signature.substring(0,14);
00376 if(!dcsMap.containsKey(sn)) {
00377 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);
00378 if(response!=javax.swing.JOptionPane.YES_OPTION) return false;
00379 }
00380 }
00381 return true;
00382
00383 }
00384
00385 private void appendDCSInfo(StringBuffer newString, SummaryReader.DCSInfo dcsInfo) {
00386 newString.append("%DCS_INFO");
00387 newString.append("\n#T0 T1");
00388 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.T0)+" "+dcsInfo.get(SummaryReader.DCSInfo.T0));
00389 newString.append("\n#VDET IDET");
00390 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VDET)+" "+dcsInfo.get(SummaryReader.DCSInfo.IDET));
00391 newString.append("\n#VCC ICC");
00392 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VCC)+" "+dcsInfo.get(SummaryReader.DCSInfo.ICC));
00393 newString.append("\n#VDD IDD");
00394 newString.append("\n"+dcsInfo.get(SummaryReader.DCSInfo.VDD)+" "+dcsInfo.get(SummaryReader.DCSInfo.IDD));
00395 newString.append("\n#TIME_POWERED\n.\n");
00396 }
00397
00398 private void createSignatureMap() {
00399 testMap = new HashMap();
00400 for(int i=0;i<sctdaqDBTestNames.length;i++) {
00401 testMap.put(resultFileKeyNames[i],sctdaqDBTestNames[i]);
00402 }
00403 }
00404 private String getDate(String line) {
00405 Matcher matcher = testDatePattern.matcher(line);
00406 if(matcher.matches()) {
00407 String day = line.substring(matcher.start(1),matcher.end(1));
00408 String month = line.substring(matcher.start(2),matcher.end(2));
00409 String year = line.substring(matcher.start(3),matcher.end(3));
00410 if(month.length()==1) month="0"+month;
00411 if(day.length()==1) day="0"+day;
00412 return day+"/"+month+"/"+year;
00413 }
00414 return null;
00415 }
00416
00417
00418 }