00001 package cedar.jetweb.db; 00002 00003 import cedar.jetweb.*; 00004 import cedar.jetweb.generator.Generator; 00005 import cedar.jetweb.model.*; 00006 import cedar.jetweb.job.LogFile; 00007 import cedar.jetweb.job.Cut; 00008 import cedar.jetweb.job.CutCollection; 00009 00010 import java.io.File; 00011 00012 import java.sql.*; 00013 00014 import java.util.Enumeration; 00015 import java.util.Vector; 00016 import java.util.HashMap; 00017 00018 00027 public abstract class DBManager { 00040 private static DBObjectManager objManager = new DBObjectManager(); 00041 00042 public static synchronized Integer getId(Model model) 00043 throws JetWebException{ 00044 00045 //if model already has valid id then just return that 00046 if(model.getId()>0){ 00047 00048 return model.getId(); 00049 } 00050 00051 //If matching model exists in database then return that id 00052 Vector<Model> matches = getConsistentModels(model); 00053 00054 if(matches.size()==1){ 00055 return matches.firstElement().getId(); 00056 }else if(matches.size()>1){ 00057 throw new JetWebException 00058 ("More than one model matches this Model!", 00059 "Found Id"+matches.firstElement().getId()); 00060 } 00061 00062 //if we've got here then we need a new model id 00063 //use the next highest available id 00064 00065 Integer mId; 00066 00067 String query = "SELECT MAX(mdl_id) from model"; 00068 try{ 00069 Statement stmt = DBConfig.getConnection().createStatement(); 00070 ResultSet rs = stmt.executeQuery(query); 00071 if(rs.next()){ 00072 mId = rs.getInt(1); 00073 00074 if(mId==null){ 00075 mId = 1; 00076 }else{ 00077 ++mId; 00078 } 00079 rs.close(); 00080 stmt.close(); 00081 return mId; 00082 } 00083 }catch(Throwable err){ 00084 System.out.println(err.getMessage()); 00085 throw new JetWebException("Unable to query model table", 00086 "DBManager.getId(Model)"); 00087 } 00088 return -1; 00089 } 00090 00091 00092 public static synchronized boolean addToDB(Model model) 00093 throws JetWebException { 00094 // Firstly, if this model has an ID already, see if it exists in the 00095 // DB. If it does, return false. 00096 00097 if(exists("model", "mdl_id", model.getId()))return false; 00098 00099 // Ok, this really is a new Model. Store it. 00100 00101 // Now the model itself. 00102 String insertString = makeInsertModelString(model); 00103 int modelsFound = 0; 00104 00105 try { 00106 Statement stmt = DBConfig.getConnection().createStatement(); 00107 int result = stmt.executeUpdate(insertString); 00108 stmt.close(); 00109 // Now store the generator parameters. 00110 addGeneratorParameters(model); 00111 00112 } catch (SQLException E) { 00113 System.out.println("Error inserting model"); 00114 System.out.println("SQLException: " + E.getMessage()); 00115 System.out.println("SQLState: " + E.getSQLState()); 00116 System.out.println("VendorError: " + E.getErrorCode()); 00117 throw new JetWebDBException 00118 (E, insertString, "modelId:" + model.getId()); 00119 } 00120 00121 return (modelsFound > 0); 00122 } 00123 00124 private static synchronized String makeInsertModelString(Model model) 00125 throws JetWebException { 00126 String ins1 = "INSERT INTO model" + 00127 "(mdl_id, generator_id"; 00128 String ins2 = ") VALUES('" + 00129 model.getId() + "', '" + 00130 model.getGenerator().getGeneratorId()+ "'"; 00131 ins1 = ins1 + ",photon_pdf"; 00132 ins2 = ins2 + "," + model.getPhotonPDF(0); 00133 00134 ins1 = ins1 + ",proton_pdf"; 00135 ins2 = ins2 + "," + model.getProtonPDF(0); 00136 00137 // Now add the RunSeriesCollectionId 00138 ins1 = ins1 + ",runseries_collection_id"; 00139 ins2 = ins2 + ",'" + model.getRunSeriesCollection().getId()+ "'"; 00140 00141 ins1 = ins1 + ", description"; 00142 ins2 = ins2 + ", '" + model.getDescription() + "' )"; 00143 00144 return ins1 + ins2; 00145 } 00146 00153 public static synchronized void selectFromDB(Model model) 00154 throws JetWebException { 00155 String query = "SELECT * FROM model WHERE mdl_id=" + model.getId(); 00156 00157 try { 00158 Statement stmt = DBConfig.getConnection().createStatement(); 00159 ResultSet rs = stmt.executeQuery(query); 00160 00161 if (rs.next()) { 00162 fill(model, rs); 00163 } else { 00164 rs.close(); 00165 stmt.close(); 00166 throw new JetWebException("No such Model exists","ID="+model.getId()); 00167 } 00168 00169 rs.close(); 00170 stmt.close(); 00171 } catch (SQLException e) { 00172 System.out.println("Error reading model table " + e + "ModelId=" + model.getId()); 00173 throw new JetWebDBException(e, query, "modelId:" + model.getId()); 00174 } 00175 00176 // Now fill the generator specific parameters. 00177 DBGeneratorManager.selectGeneratorParameters(model); 00178 } 00179 00180 private static synchronized void fill(ResultSearchPattern pattern, ResultSet rs) 00181 throws JetWebException { 00182 fillCommon(pattern, rs); 00183 } 00184 00185 // Fill variables which are common to all manifestations of the class. 00186 // The generator is create but not filled. 00187 private static synchronized void fillCommon(ResultSearchPattern pattern, ResultSet rs) 00188 throws JetWebException { 00189 00190 try { 00191 00192 Object tmp = rs.getObject("photon_pdf"); 00193 00194 if (!rs.wasNull()&&tmp!=null) { 00195 pattern.getPhotonPDFList().clear(); 00196 pattern.getPhotonPDFList().add 00197 (new PDF((Integer)tmp)); 00198 } 00199 00200 tmp = rs.getObject("proton_pdf"); 00201 00202 if (!rs.wasNull()&&tmp!=null) { 00203 pattern.getProtonPDFList().clear(); 00204 pattern.getProtonPDFList().add 00205 (new PDF((Integer)tmp)); 00206 } 00207 00208 int generatorId = rs.getInt("generator_id"); 00209 Generator gen = Generator.Maker(generatorId); 00210 pattern.setGenerator(gen); 00211 } catch (SQLException E) { 00212 System.out.println("DBManager: error filling common variables"); 00213 System.out.println("SQLException: " + E.getMessage()); 00214 System.out.println("SQLState: " + E.getSQLState()); 00215 System.out.println("VendorError: " + E.getErrorCode()); 00216 throw new JetWebDBException(E, "", "Pattern:" + pattern.getParms()); 00217 } 00218 } 00219 00220 private static synchronized void fill(Model model, ResultSet rs) 00221 throws JetWebException { 00222 fillCommon(model, rs); 00223 //System.out.println(model.getGenerator()); 00224 00225 try { 00226 RunSeriesCollection rsc = 00227 new RunSeriesCollection(rs.getInt("runseries_collection_id")); 00228 00229 model.setRunSeriesCollection(rsc); 00230 model.setDescription(rs.getString("description")); 00231 00232 } catch (SQLException e) { 00233 System.out.println("Error reading model table " + e); 00234 throw new JetWebDBException(e, "", "modelId:" + model.getId()); 00235 } 00236 } 00237 00238 public static synchronized Integer 00239 getRunSeriesCollectionId(Vector<Integer> runseries) 00240 throws JetWebException{ 00241 00242 Vector<Integer> foundIds = new Vector<Integer>(); 00243 00244 try{ 00245 PreparedStatement pstmt = 00246 DBConfig.getConnection().prepareStatement("SELECT runseries_collection_id FROM runseries_collection WHERE runseries_id=?"); 00247 00248 boolean firstrow = true; 00249 00250 for(Integer rs: runseries){ 00251 pstmt.setInt(1, rs); 00252 ResultSet dbrs = pstmt.executeQuery(); 00253 00254 Vector<Integer> newIds = new Vector<Integer>(); 00255 Vector<Integer> oldIds = new Vector<Integer>(foundIds); 00256 00257 while(dbrs.next()){ 00258 newIds.add(dbrs.getInt(1)); 00259 } 00260 00261 if(firstrow){ 00262 foundIds = newIds; 00263 firstrow = false; 00264 }else{ 00265 foundIds.clear(); 00266 for(Integer id: newIds){ 00267 if(oldIds.contains(id)){ 00268 foundIds.add(id); 00269 } 00270 } 00271 } 00272 00273 } 00274 00275 00276 }catch(SQLException err){ 00277 throw new JetWebException(err); 00278 } 00279 00280 if(foundIds.size()==1){ 00281 return foundIds.get(0); 00282 }else if(foundIds.size()>1){ 00283 Integer idRet = -1; 00284 Integer min = Integer.MAX_VALUE; 00285 try{ 00286 PreparedStatement pstmt = 00287 DBConfig.getConnection().prepareStatement 00288 ("SELECT COUNT(runseries_id) FROM " + 00289 "runseries_collection WHERE " + 00290 "runseries_collection_id=?"); 00291 00292 for(Integer id: foundIds){ 00293 pstmt.setInt(1, id); 00294 ResultSet dbrs = pstmt.executeQuery(); 00295 if(dbrs.next()){ 00296 Integer count = dbrs.getInt(1); 00297 if(count<min){ 00298 min = count; 00299 idRet = id; 00300 } 00301 } 00302 } 00303 00304 }catch(SQLException err){ 00305 throw new JetWebException(err); 00306 } 00307 return idRet; 00308 } 00309 00310 return -1; 00311 } 00312 00313 00332 public static synchronized Vector<Model> getConsistentModels(RunSeries runseries) 00333 throws JetWebException { 00334 00335 Vector<Model> models = new Vector<Model>(); 00336 //System.out.print(runseries.getGenerator()+" "+runseries.getGenerator(0)); 00337 String query = makeModelQueryString(runseries,0); 00338 00339 try { 00340 Statement stmt = DBConfig.getConnection().createStatement(); 00341 ResultSet rs = stmt.executeQuery(query); 00342 00343 System.out.println("Looking for models matching runseries"); 00344 System.out.println(query); 00345 00346 while (rs.next()) { 00347 int modelId = rs.getInt("mdl_id"); 00348 System.out.println("trying model "+modelId); 00349 Model mdl = new Model(modelId); 00350 if(mdl.matches(runseries)){ 00351 //if (mdl.getGenerator().isConsistentWith(runseries.getGenerator())){ 00352 System.out.println("Adding model " + mdl.getId() + 00353 " to list of consistent models"); 00354 models.add(mdl); 00355 } 00356 } 00357 00358 00359 rs.close(); 00360 stmt.close(); 00361 } catch (SQLException E) { 00362 System.out.println("DBManager: error finding consistent models for runseries."); 00363 System.out.println("SQLException: " + E.getMessage()); 00364 System.out.println("SQLState: " + E.getSQLState()); 00365 System.out.println("VendorError: " + E.getErrorCode()); 00366 throw new JetWebDBException(E, query, "RunseriesId:" + runseries.getId()); 00367 } 00368 00369 return models; 00370 } 00371 00381 public static synchronized Vector<Model> getConsistentModels(ResultSearchPattern pattern) 00382 throws JetWebException { 00383 Vector<Model> models = new Vector<Model>(); 00384 00385 int genIndex=0; 00386 for (Generator gen : pattern.getGeneratorList()){ 00387 00388 String query = makeModelQueryString(pattern,genIndex); 00389 genIndex++; 00390 00391 try { 00392 Statement stmt = DBConfig.getConnection().createStatement(); 00393 ResultSet rs = stmt.executeQuery(query); 00394 00395 while (rs.next()) { 00396 int modelId = rs.getInt("mdl_id"); 00397 00398 Model mdl = new Model(modelId); 00399 00400 //if (mdl.getGenerator().isConsistentWith(gen)){ 00401 if(pattern.matches(mdl)){ 00402 models.add(mdl); 00403 } 00404 } 00405 00406 rs.close(); 00407 stmt.close(); 00408 } catch (SQLException E) { 00409 System.out.println("DBManager: error finding consistent models for pattern."); 00410 System.out.println("SQLException: " + E.getMessage()); 00411 System.out.println("SQLState: " + E.getSQLState()); 00412 System.out.println("VendorError: " + E.getErrorCode()); 00413 throw new JetWebDBException(E, query, pattern.getParms()); 00414 } 00415 } 00416 00417 return models; 00418 } 00419 00420 // Make the query string using everything which is defined. 00421 // Looks at the genId-th generator in the list. 00422 private static String makeModelQueryString(ResultSearchPattern pattern, int genId) 00423 throws JetWebException { 00424 String query = "SELECT mdl_id FROM model "; 00425 StringBuffer q1 = new StringBuffer(); 00426 boolean anything = false; 00427 00428 Generator gen = pattern.getGenerator(genId); 00429 00430 q1.append("(generator_id='" + gen.getGeneratorId() +"') AND "); 00431 00432 if (pattern.getPhotonPDFList().size() == 1) { 00433 if(pattern.getPhotonPDF()!=null){ 00434 q1.append("photon_pdf=" + pattern.getPhotonPDF() + " AND "); 00435 } 00436 } 00437 00438 if (pattern.getPhotonPDFList().size() == 2) { 00439 q1.append 00440 ("(photon_pdf=" + 00441 pattern.getPhotonPDF() + 00442 " OR photon_pdf=" + 00443 pattern.getPhotonPDF(1) + ") AND "); 00444 } 00445 00446 if (pattern.getProtonPDFList().size() == 1) { 00447 if(pattern.getProtonPDF()!=null){ 00448 q1.append("proton_pdf=" + pattern.getProtonPDF() + " AND "); 00449 } 00450 } 00451 00452 if (pattern.getProtonPDFList().size() == 2) { 00453 q1.append("(proton_pdf=" + pattern.getProtonPDF() + " OR proton_pdf=" + 00454 pattern.getProtonPDF(1) + ") AND "); 00455 } 00456 00457 String query1 = q1.toString(); 00458 if (query1.trim().length() != 0) { 00459 query1 = query1.substring(0, query1.lastIndexOf("AND")); 00460 query = query + " WHERE " + query1; 00461 } 00462 00463 return query; 00464 } 00465 00472 public static void selectFromDB(RunSeries runseries) 00473 throws JetWebException { 00474 String query = "SELECT * FROM runseries WHERE runseries_id=" + runseries.getId(); 00475 00476 try { 00477 Statement stmt = DBConfig.getConnection().createStatement(); 00478 ResultSet rs = stmt.executeQuery(query); 00479 00480 while (rs.next()) { 00481 fill(runseries, rs); 00482 } 00483 00484 rs.close(); 00485 stmt.close(); 00486 } catch (SQLException e) { 00487 System.out.println("Error reading runseries table " + e); 00488 throw new JetWebDBException(e, query, "runseriesId:" + runseries.getId()); 00489 } 00490 00491 // Now fill the generator specific parameters. 00492 try { 00493 DBGeneratorManager.selectGeneratorParameters(runseries); 00494 } catch (JetWebException jwe) { 00495 throw jwe; 00496 } 00497 } 00498 00499 // Fill runseries specific variables. 00500 private static void fill(RunSeries runseries, ResultSet rs) 00501 throws JetWebException { 00502 fillCommon(runseries, rs); 00503 00504 try { 00505 00506 00507 runseries.setMCProcessType(new MCProcessType(rs.getInt("process_id"))); 00508 runseries.setDate(rs.getTimestamp("time")); 00509 runseries.setLumi(rs.getDouble("lumi")); 00510 runseries.setCrossSection(rs.getDouble("cross_section")); 00511 int cutCollectionId = rs.getInt("cut_collection_id"); 00512 00513 if(cutCollectionId!=0){ 00514 00515 runseries.getGenerator().setCutCollection 00516 (new CutCollection(cutCollectionId)); 00517 } 00518 } catch (SQLException e) { 00519 System.out.println("Error reading runseries table " + e); 00520 } 00521 } 00522 00523 private static void fill(Collision collision, ResultSet rs) 00524 throws JetWebException { 00525 try { 00526 Particle p1 = new Particle(); 00527 p1.setName(rs.getString("particle1")); 00528 p1.setEnergy(rs.getFloat("energy1")); 00529 00530 Particle p2 = new Particle(); 00531 p2.setName(rs.getString("particle2")); 00532 p2.setEnergy(rs.getFloat("energy2")); 00533 00534 collision.setId(rs.getInt("collision_id")); 00535 collision.addParticle(p1); 00536 collision.addParticle(p2); 00537 } catch (SQLException e) { 00538 System.out.println("Error reading collision table " + e); 00539 } 00540 } 00541 00549 public static synchronized void resetLogTime(int runseriesId) 00550 throws JetWebException { 00551 String update = "UPDATE runseries SET time=NULL WHERE runseries_id=" + runseriesId; 00552 00553 try { 00554 Statement stmt = DBConfig.getConnection().createStatement(); 00555 stmt.executeUpdate(update); 00556 stmt.close(); 00557 } catch (SQLException e) { 00558 System.out.println("DB access error in Logfile" + e); 00559 throw new JetWebDBException(e, update, "runseriesId:" + runseriesId); 00560 } 00561 } 00562 00563 00564 00571 //public static synchronized void addLumi(double lumi, int runseriesId) 00572 public static synchronized void addLumi(LogFile log) 00573 throws JetWebException { 00574 String sqlString = ""; 00575 00576 String select = ""; 00577 00578 int logId = log.getId(); 00579 00580 if(logId>0){ 00581 select = "SELECT lumi, log_id FROM logfile WHERE log_id="+logId; 00582 }else{ 00583 log.getRunSeries().getCutCollection().store(); 00584 select = "SELECT lumi, log_id FROM logfile WHERE runseries_id=" + 00585 log.getRunSeries().getId() + 00586 " AND cut_collection_id=" + 00587 log.getRunSeries().getGenerator().getCutCollection().getId(); 00588 } 00589 00590 //String select = 00591 // "SELECT lumi FROM logfile WHERE runseries_id=" + runseriesId; 00592 00593 try { 00594 Statement stmt = DBConfig.getConnection().createStatement(); 00595 sqlString = select; 00596 00597 ResultSet rs = stmt.executeQuery(sqlString); 00598 boolean hasWeighted = false; 00599 //double total = lumi; 00600 double total = log.getLumi(); 00601 00602 if (log.getLumi() < 0) { 00603 hasWeighted = true; 00604 } 00605 00606 int id = -1; 00607 00608 while (rs.next()) { 00609 double inc = rs.getDouble("lumi"); 00610 id = rs.getInt("log_id"); 00611 if (inc < 0.0) { 00612 hasWeighted = true; 00613 } else { 00614 total = total + inc; 00615 } 00616 00617 } 00618 00619 // Negative lumi means there are weighted events. 00620 if (hasWeighted) { 00621 System.out.println 00622 ("DBManager: Found weighted events"); 00623 total = -1.0 * total; 00624 } 00625 00626 // Need to get the exis 00627 00628 00629 sqlString = "UPDATE runseries SET lumi=" + total + 00630 " WHERE runseries_id=" + log.getRunSeries().getId(); 00631 //runseriesId; 00632 00633 stmt.executeUpdate(sqlString); 00634 rs.close(); 00635 stmt.close(); 00636 } catch (SQLException e) { 00637 System.out.println("DB access error in Logfile" + e); 00638 //throw new JetWebDBException(e, sqlString, "runseriesId:" + runseriesId); 00639 throw new JetWebDBException(e, sqlString, "runseriesId:" + 00640 log.getRunSeries().getId()); 00641 } 00642 } 00643 00647 public static Enumeration<Integer> getAllRunSeriesIds() throws JetWebDBException { 00648 Vector<Integer> ids = new Vector<Integer>(); 00649 String sqlString = ""; 00650 String select = "SELECT runseries_id FROM runseries"; 00651 00652 try { 00653 Statement stmt = DBConfig.getConnection().createStatement(); 00654 sqlString = select; 00655 00656 ResultSet rs = stmt.executeQuery(sqlString); 00657 00658 while (rs.next()) { 00659 Integer id = new Integer(rs.getInt("runseries_id")); 00660 ids.add(id); 00661 } 00662 rs.close(); 00663 stmt.close(); 00664 } catch (SQLException e) { 00665 System.out.println("DB access error in RunSeries" + e); 00666 throw new JetWebDBException(e, sqlString, "getAllRunSeriesIds"); 00667 } 00668 00669 return ids.elements(); 00670 } 00671 00677 //Don't think we actually use this 00678 00679 /* 00680 public static void updateLumi(RunSeries runseries, double newLumi) 00681 throws JetWebDBException { 00682 if (runseries.getLumi() != newLumi) { 00683 runseries.setLumi(newLumi); 00684 00685 String update = "UPDATE runseries SET lumi=" + newLumi + " WHERE runseries_id=" + 00686 runseries.getId(); 00687 00688 try { 00689 Statement stmt = DBConfig.getConnection().createStatement(); 00690 int i = stmt.executeUpdate(update); 00691 stmt.close(); 00692 } catch (SQLException e) { 00693 throw new JetWebDBException(e, update, "runseriesId:" + runseries.getId()); 00694 } 00695 } 00696 } 00697 */ 00698 00699 00704 public static void zero(RunSeries lp) { 00705 if (lp != null) { 00706 String zeroIt = 00707 "update predicted_point set y=0.0,dy_up=0.0,dy_down=0.0 where runseries_id=" + 00708 lp.getId(); 00709 00710 try { 00711 Statement stmt = DBConfig.getConnection().createStatement(); 00712 stmt.executeUpdate(zeroIt); 00713 stmt.close(); 00714 } catch (SQLException e) { 00715 System.out.println("DBManager: Error executing " + zeroIt); 00716 } 00717 00718 zeroIt = "update runseries set lumi=0.0 where runseries_id=" + lp.getId(); 00719 00720 try { 00721 Statement stmt = DBConfig.getConnection().createStatement(); 00722 stmt.executeUpdate(zeroIt); 00723 stmt.close(); 00724 } catch (SQLException e) { 00725 System.out.println("DBManager: Error executing " + zeroIt); 00726 } 00727 } else { 00728 System.out.println("DBManager: trapped attempt to zero a null runseries"); 00729 } 00730 } 00731 00737 public static void selectFromDB(MCProcessType proc) 00738 throws JetWebException { 00739 String query = 00740 "SELECT * FROM process_type WHERE process_id=" + proc.getId(); 00741 00742 try { 00743 Statement stmt = DBConfig.getConnection().createStatement(); 00744 ResultSet rs = stmt.executeQuery(query); 00745 00746 if(objManager.getDBConnection()==null){ 00747 objManager.passDBConnection 00748 (DBConfig.getDBConnectionManager()); 00749 } 00750 00751 while (rs.next()) { 00752 objManager.fillObject(proc, rs); 00753 } 00754 00755 rs.close(); 00756 stmt.close(); 00757 } catch (SQLException e) { 00758 System.out.println("Error reading process_type table " + e); 00759 throw new JetWebDBException(e, query, "processId:" + proc.getId()); 00760 } 00761 } 00762 00768 public static boolean addToDB(MCProcessType proc) 00769 throws JetWebException { 00770 00771 boolean newRow = false; 00772 if (proc.getId() > 0) { 00773 if (exists("process_type", "process_id", proc.getId())) { 00774 deleteFromDB("process_type","process_id",proc.getId()); 00775 newRow = true; 00776 } 00777 } 00778 if(objManager.getDBConnection()==null){ 00779 objManager.passDBConnection 00780 (DBConfig.getDBConnectionManager()); 00781 } 00782 objManager.writeObject(proc,"process_type",DBConfig.JETWEB); 00783 00784 return newRow; 00785 } 00790 public static boolean deleteFromDB(MCProcessType proc) 00791 throws JetWebException { 00792 00793 boolean success = false; 00794 if (proc.getId() > 0) { 00795 if (exists("process_type", "process_id", proc.getId())) { 00796 deleteFromDB("process_type","process_id",proc.getId()); 00797 success = true; 00798 } 00799 } 00800 return success; 00801 } 00802 00808 public static void selectFromDB(RunSeriesCollection rsc) 00809 throws JetWebException { 00810 String query = "SELECT * FROM runseries_collection WHERE runseries_collection_id=" + 00811 rsc.getId(); 00812 00813 try { 00814 Statement stmt = DBConfig.getConnection().createStatement(); 00815 ResultSet rs = stmt.executeQuery(query); 00816 00817 while (rs.next()) { 00818 int rsId = rs.getInt("runseries_id"); 00819 //RunSeries runSeries = new RunSeries(rsId); 00820 RunSeries runSeries = RunSeries.Maker(rsId); 00821 //rsc.getRunSeriesList().add(runSeries); 00822 rsc.add(runSeries); 00823 } 00824 00825 rs.close(); 00826 stmt.close(); 00827 } catch (SQLException e) { 00828 System.out.println("Error reading runseries_collection table " + e); 00829 throw new JetWebDBException(e, query, "runseries_collection_id:" + rsc.getId()); 00830 } 00831 } 00832 00839 public static Vector<MCProcessType> getAllMCProcessTypes(boolean selectable) 00840 throws JetWebException { 00841 00842 return getAllMCProcessTypes(null,selectable); 00843 } 00844 00849 public static Vector<MCProcessType> getAllMCProcessTypes(Collision collision,boolean selectable) 00850 throws JetWebException { 00851 00852 Vector<MCProcessType> procList = new Vector<MCProcessType>(); 00853 00854 StringBuffer query = new StringBuffer("SELECT * FROM process_type "); 00855 if ((collision!=null)|| selectable) {query.append(" WHERE ");} 00856 if (collision!=null) { 00857 query.append("collision_id='" + collision.getId()+"'"); 00858 if (selectable) {query.append(" AND ");} 00859 } 00860 if (selectable) query.append(" is_valid='1'"); 00861 00862 try { 00863 Statement stmt = DBConfig.getConnection().createStatement(); 00864 ResultSet rs = stmt.executeQuery(query.toString()); 00865 00866 while (rs.next()) { 00867 MCProcessType proc = new MCProcessType(); 00868 objManager.fillObject(proc, rs); 00869 procList.add(proc); 00870 } 00871 00872 rs.close(); 00873 stmt.close(); 00874 } catch (SQLException e) { 00875 System.out.println("Error reading process_type table " + e); 00876 throw new JetWebDBException(e, query.toString(), "?"); 00877 } 00878 00879 return procList; 00880 } 00881 00886 public static boolean exists(String tableName, String fieldName, int id) 00887 throws JetWebException { 00888 boolean exists = false; 00889 String query = " "; 00890 00891 try { 00892 Statement stmt = DBConfig.getConnection().createStatement(); 00893 query = "SELECT * FROM " + tableName + " where " + fieldName + "=" + id; 00894 00895 ResultSet rs = stmt.executeQuery(query); 00896 00897 if (rs.next()) { 00898 exists = true; 00899 } 00900 00901 rs.close(); 00902 stmt.close(); 00903 00904 return exists; 00905 } catch (SQLException E) { 00906 throw new JetWebDBException(E, query, "modelId:" + id); 00907 } 00908 } 00909 00910 00917 public static void deleteFromDB(String tableName, String fieldName, int id) 00918 throws JetWebException { 00919 00920 String query = " "; 00921 00922 try { 00923 Statement stmt = DBConfig.getConnection().createStatement(); 00924 query = "DELETE FROM " + tableName + " where " + fieldName + "=" + id; 00925 00926 int res = stmt.executeUpdate(query); 00927 00928 stmt.close(); 00929 00930 } catch (SQLException E) { 00931 throw new JetWebDBException(E, query, "dbmanager"); 00932 } 00933 } 00934 00935 00941 public static boolean addToDB(Collision collision) 00942 throws JetWebException { 00943 if (collision.getId() > 0) { 00944 if (exists("collision", "collision_id", collision.getId())) { 00945 throw new JetWebException("Attempt to re-store an existing collision.", 00946 "id = " + collision.getId()); 00947 } 00948 } 00949 00950 String ins = "INSERT INTO collision" + "(particle1,particle2,energy1,energy2) VALUES('" + 00951 collision.getParticle1().getName() + "','" + 00952 collision.getParticle2().getName() + "','" + 00953 collision.getParticle1().getEnergy() + "','" + collision.getParticle2().getEnergy() + 00954 "')"; 00955 00956 try { 00957 Statement stmt = DBConfig.getConnection().createStatement(); 00958 int result = stmt.executeUpdate(ins); 00959 stmt.close(); 00960 } catch (SQLException E) { 00961 throw new JetWebDBException(E, ins, "Id:" + collision.getId()); 00962 } 00963 00964 return true; 00965 } 00966 00967 public static boolean selectFromDB(Collision collision) 00968 throws JetWebException { 00969 if (collision.getId() <= 0) { 00970 throw new JetWebException("Attempt to select collision with id not defined", 00971 "id was " + collision.getId()); 00972 } 00973 00974 String query = "SELECT * FROM collision WHERE collision_id=" + collision.getId(); 00975 00976 try { 00977 Statement stmt = DBConfig.getConnection().createStatement(); 00978 ResultSet rs = stmt.executeQuery(query); 00979 00980 while (rs.next()) { 00981 fill(collision, rs); 00982 } 00983 00984 rs.close(); 00985 stmt.close(); 00986 } catch (SQLException e) { 00987 throw new JetWebDBException(e, query, "collision_id:" + collision.getId()); 00988 } 00989 00990 return true; 00991 } 00992 00996 public static Vector<Collision> getAllCollisions() throws JetWebException { 00997 Vector<Collision> list = new Vector<Collision>(); 00998 String query = "SELECT * FROM collision"; 00999 01000 try { 01001 Statement stmt = DBConfig.getConnection().createStatement(); 01002 ResultSet rs = stmt.executeQuery(query); 01003 01004 while (rs.next()) { 01005 Collision collision = new Collision(); 01006 fill(collision, rs); 01007 list.add(collision); 01008 } 01009 01010 rs.close(); 01011 stmt.close(); 01012 } catch (SQLException e) { 01013 System.out.println("Error reading process_type table " + e); 01014 throw new JetWebDBException(e, query, "?"); 01015 } 01016 01017 return list; 01018 } 01019 01025 public static boolean checkCollision(int collId, int procId) 01026 throws JetWebException { 01027 String query = "SELECT * FROM process_type WHERE process_id='" + procId + 01028 "' AND collision_id='" + collId + "'"; 01029 01030 try { 01031 Statement stmt = DBConfig.getConnection().createStatement(); 01032 ResultSet rs = stmt.executeQuery(query); 01033 01034 if (rs.next()) { 01035 rs.close(); 01036 stmt.close(); 01037 01038 return true; 01039 } 01040 01041 rs.close(); 01042 stmt.close(); 01043 01044 return false; 01045 } catch (SQLException e) { 01046 System.out.println("Error reading process_type table " + e); 01047 throw new JetWebDBException(e, query, "?"); 01048 } 01049 } 01050 01057 public static synchronized void deleteParms(int runseriesId) 01058 throws JetWebException { 01059 System.out.println("DBManager: Deleting runseries ID=" + runseriesId); 01060 01061 String deleteString = ""; 01062 String deleteString1 = "DELETE FROM runseries WHERE runseries_id=" + runseriesId; 01063 String deleteString2 = "DELETE FROM generator_runseries WHERE runseries_id=" + runseriesId; 01064 01065 try { 01066 Statement stmt = DBConfig.getConnection().createStatement(); 01067 deleteString = deleteString1; 01068 stmt.executeUpdate(deleteString); 01069 deleteString = deleteString2; 01070 stmt.executeUpdate(deleteString); 01071 stmt.close(); 01072 } catch (SQLException e) { 01073 System.out.println("Error deleting incorrect runseries " + e + "RunSeriesId=" + 01074 runseriesId); 01075 throw new JetWebDBException(e, deleteString, "id=" + runseriesId); 01076 } 01077 } 01078 01086 public static synchronized void addRunSeries(RunSeries runSeries) 01087 throws JetWebException { 01088 01089 //get a new id for the runseries: 01090 01091 String selectMax = "SELECT MAX(runseries_id) FROM runseries"; 01092 01093 Integer newId=1; 01094 01095 try{ 01096 Statement stmt = DBConfig.getConnection().createStatement(); 01097 ResultSet rs = stmt.executeQuery(selectMax); 01098 01099 while(rs.next()){ 01100 Integer tmpId = rs.getInt(1); 01101 if(tmpId!=null && tmpId>=newId) newId=tmpId+1; 01102 } 01103 01104 }catch(SQLException err){ 01105 throw new JetWebException(err); 01106 } 01107 01108 01109 MCProcessType proc = runSeries.getMCProcessType(); 01110 01111 CutCollection cuts = runSeries.getCutCollection(); 01112 if(cuts!=null) cuts.store(); 01113 01114 // First add those things which are always defined. 01115 String ins1 = "INSERT INTO runseries " + 01116 "(runseries_id, " + 01117 "lumi, " + 01118 "cross_section, " + 01119 "generator_id, " + 01120 "process_id, " + 01121 "cut_collection_id"; 01122 01123 String ins2 = "values(" + newId + "," + 01124 runSeries.getLumi() + "," 01125 + runSeries.getCrossSection() + ",'" 01126 + runSeries.getGenerator().getGeneratorId()+ "','" 01127 + proc.getId()+ "','" + 01128 runSeries.getCutCollection().getId() + 01129 "'"; 01130 01131 // Now those rows which may or may not be filled. 01132 // General Photon things. 01133 if (proc.hasPhoton()) { 01134 ins1 = ins1 + ", photon_pdf"; 01135 ins2 = ins2 + "," + runSeries.getPhotonPDF(); 01136 } 01137 01138 // General Proton things. 01139 if (proc.hasProton()) { 01140 ins1 = ins1 + ", proton_pdf"; 01141 ins2 = ins2 + "," + runSeries.getProtonPDF() + ""; 01142 } 01143 01144 String ins = ins1 + ")" + ins2 + ")"; 01145 01146 try { 01147 Statement stmt = DBConfig.getConnection().createStatement(); 01148 int rowsChanged = stmt.executeUpdate(ins); 01149 stmt.close(); 01150 } catch (SQLException e) { 01151 throw new JetWebDBException(e, ins, "runSeriesId:" + runSeries.getId()); 01152 } 01153 01154 String query = "SELECT MAX(runseries_id) FROM runseries"; 01155 01156 try { 01157 Statement stmt = DBConfig.getConnection().createStatement(); 01158 ResultSet rs = stmt.executeQuery(query); 01159 rs.next(); 01160 01161 int id = rs.getInt(1); 01162 01163 runSeries.setId(id); 01164 01165 addGeneratorParameters(runSeries); 01166 01167 // Check there were no errors. Could get errors if we allow asynchronous 01168 // multithreaded DB access? 01169 //log.setRunSeriesId(matchRunSeriesId(log)); 01170 if ((runSeries == null) || (runSeries.getId() <= 0)) { 01171 // Something went wrong. The data added to the DB did not match the 01172 // runseries it was supposed to come from. 01173 // Delete the RunSeries we added, and throw an exception. 01174 rs.close(); 01175 stmt.close(); 01176 deleteParms(id); 01177 throw new JetWebDBException("Failed to add runseries", "deleted"); 01178 } 01179 01180 rs.close(); 01181 stmt.close(); 01182 } catch (SQLException E) { 01183 System.out.println("SQLException: " + E.getMessage()); 01184 System.out.println("SQLState: " + E.getSQLState()); 01185 System.out.println("VendorError: " + E.getErrorCode()); 01186 throw new JetWebDBException(E, query, ""); 01187 } 01188 } 01189 01190 01191 // Add the generator tables to the db for a runSeries. 01192 private static void addGeneratorParameters(RunSeries runSeries) 01193 throws JetWebException { 01194 Generator gen = runSeries.getGenerator(); 01195 String insert = 01196 "INSERT INTO generator_runseries (runseries_id,name,aindices,ivalue,rvalue) values ('" + 01197 runSeries.getId() + "',"; 01198 01199 try { 01200 DBGeneratorManager.addGeneratorParameters(insert, gen); 01201 } catch (SQLException E) { 01202 System.out.println("DBManager: error adding generator parameters: " + insert); 01203 System.out.println("SQLException: " + E.getMessage()); 01204 System.out.println("SQLState: " + E.getSQLState()); 01205 System.out.println("VendorError: " + E.getErrorCode()); 01206 throw new JetWebDBException(E, insert, "runSeriesId:" + runSeries.getId()); 01207 } 01208 } 01209 01210 // Add the generator tables to the db for a runSeries. 01211 private static void addGeneratorParameters(Model model) 01212 throws JetWebException { 01213 Generator gen = model.getGenerator(); 01214 String insert = "INSERT INTO generator_model (mdl_id,name,aindices,ivalue,rvalue) values ('" + 01215 model.getId() + "',"; 01216 01217 try { 01218 DBGeneratorManager.addGeneratorParameters(insert, gen); 01219 } catch (SQLException E) { 01220 System.out.println("DBManager: error adding generator model: " + insert); 01221 System.out.println("SQLException: " + E.getMessage()); 01222 System.out.println("SQLState: " + E.getSQLState()); 01223 System.out.println("VendorError: " + E.getErrorCode()); 01224 throw new JetWebDBException(E, insert, "ModelId:" + model.getId()); 01225 } 01226 } 01227 01236 public static synchronized Vector<RunSeries> getConsistentRunSeries(MCProcessType proc, Model model) 01237 throws JetWebException { 01238 HashMap<Integer, RunSeries> foundRunSeries = 01239 new HashMap<Integer, RunSeries>(); 01240 //Vector<RunSeries> matchedList = new Vector<RunSeries>(); 01241 01242 String query = makeRunSeriesQueryString(model, proc); 01243 01244 try { 01245 Statement stmt = DBConfig.getConnection().createStatement(); 01246 ResultSet rs = stmt.executeQuery(query); 01247 01248 while (rs.next()) { 01249 // We have a matching row. Get the id. 01250 int rsid = rs.getInt("runseries_id"); 01251 boolean procMatch = true; 01252 01253 if (rsid>=0 && !foundRunSeries.containsKey(rsid)) { 01254 // Check the processtype, in case the process_id 01255 // was not already specified. 01256 if (rsid >= 0 && proc !=null && proc.getId()<0) { 01257 // Check the MCProcessType. If the 01258 // MCProcessType has a valid Id, then the check 01259 // was done in the original query. If not, then 01260 // the MCProcessType must have been only partially 01261 // specified, so lets see if the MCProcessType 01262 // belonging to this RunSeries matches it. 01263 int procId = rs.getInt("process_id"); 01264 MCProcessType thisProc = new MCProcessType(procId); 01265 if (thisProc.matches(proc)) { 01266 procMatch = false; 01267 } 01268 } 01269 01270 if (procMatch) { 01271 // Now build the runSeries. 01272 //RunSeries runSeries = new RunSeries(rsid); 01273 RunSeries runSeries = RunSeries.Maker(rsid); 01274 01275 //if (runSeries.getGenerator().isConsistentWith(model.getGenerator())){ 01276 if(runSeries.matches(model)){ 01277 //matchedList.add(new RunSeries(rsid)); 01278 //matchedList.add(runSeries); 01279 foundRunSeries.put(rsid, runSeries); 01280 }else{ 01281 System.out.println 01282 ("Runseries is not consistent with model"); 01283 } 01284 } 01285 } 01286 } 01287 rs.close(); 01288 stmt.close(); 01289 } catch (SQLException E) { 01290 System.out.println("SQLException: " + E.getMessage()); 01291 System.out.println("SQLState: " + E.getSQLState()); 01292 System.out.println("VendorError: " + E.getErrorCode()); 01293 throw new JetWebDBException(E, query, "ModelId:" + model.getId()); 01294 } 01295 01296 return new Vector<RunSeries>(foundRunSeries.values()); 01297 } 01298 01307 public static synchronized int matchRunSeriesId(RunSeries runSeries) 01308 throws JetWebException { 01309 01310 int rtn = -1; 01311 String query = makeRunSeriesQueryString 01312 (runSeries, runSeries.getMCProcessType()); 01313 01314 01315 try { 01316 Statement stmt = DBConfig.getConnection().createStatement(); 01317 01318 // query the runseries table for rows matching the parameters of 01319 // the input runseries. 01320 ResultSet rs = stmt.executeQuery(query); 01321 01322 while (rs.next() && (rtn <= 0)) { 01323 // We have a matching row. Get the id. 01324 rtn = rs.getInt("runseries_id"); 01325 01326 if (rtn >= 0) { 01327 // Now check generator specific auxilliary tables. 01328 01329 //RunSeries matchRunSeries = new RunSeries(rtn); 01330 RunSeries matchRunSeries = RunSeries.Maker(rtn); 01331 if (!(matchRunSeries.getGenerator().isConsistentWith(runSeries.getGenerator()))){ 01332 rtn = -1; 01333 } 01334 } 01335 } 01336 rs.close(); 01337 stmt.close(); 01338 } catch (SQLException E) { 01339 System.out.println("DBManager.matchRunSeriesId "); 01340 System.out.println(query); 01341 System.out.println("SQLException: " + E.getMessage()); 01342 System.out.println("SQLState: " + E.getSQLState()); 01343 System.out.println("VendorError: " + E.getErrorCode()); 01344 throw new JetWebDBException(E, query, "runseries Id:" + runSeries.getId()); 01345 } 01346 01347 return rtn; 01348 } 01349 01353 private static String makeRunSeriesQueryString(ResultSearchPattern pattern, MCProcessType proc) 01354 throws JetWebException { 01355 StringBuffer query = new StringBuffer 01356 ("SELECT * FROM runseries WHERE generator_id='" + 01357 pattern.getGenerator().getGeneratorId() + 01358 "'"); 01359 01360 if (proc.getId() > 0) { 01361 query.append(" AND process_id='" + proc.getId() + "'"); 01362 } 01363 01364 if (proc.hasPhoton()) { 01365 // add test on photon variables 01366 query.append(" AND photon_pdf=" + pattern.getPhotonPDF()); 01367 } 01368 01369 if (proc.hasProton()) { 01370 // add test on proton variables 01371 query.append(" AND proton_pdf=" + pattern.getProtonPDF()); 01372 } 01373 01374 // query.append("'"); 01375 01376 return query.toString(); 01377 } 01378 01383 public static void selectId(Collision collision) throws JetWebException { 01384 01385 String checkquery = "SELECT * FROM collision WHERE particle1='" + 01386 collision.getParticle1().getName() + "' AND particle2='" + 01387 collision.getParticle2().getName() + "' AND energy1='" + 01388 collision.getParticle1().getEnergy() + "' AND energy2='" + 01389 collision.getParticle2().getEnergy() + "'"; 01390 01391 try { 01392 Statement stmt = DBConfig.getConnection().createStatement(); 01393 ResultSet rs = stmt.executeQuery(checkquery); 01394 01395 if (rs.next()) { 01396 collision.setId(rs.getInt("collision_id")); 01397 rs.close(); 01398 stmt.close(); 01399 } 01400 01401 rs.close(); 01402 stmt.close(); 01403 } catch (SQLException E) { 01404 throw new JetWebDBException(E, "failed to select ID", checkquery); 01405 } 01406 } 01407 01412 public static void selectId(MCProcessType proc) throws JetWebException { 01413 String checkquery = "SELECT * FROM process_type WHERE collision_id='" + 01414 proc.getCollision().getId() + "' AND process_name='" + proc.getMCProcessName() + "'"; 01415 01416 try { 01417 Statement stmt = DBConfig.getConnection().createStatement(); 01418 ResultSet rs = stmt.executeQuery(checkquery); 01419 01420 /* 01421 if (rs.next()) { 01422 proc.setId(rs.getInt("process_id")); 01423 rs.close(); 01424 stmt.close(); 01425 } 01426 */ 01427 01428 if(objManager.getDBConnection()==null){ 01429 objManager.passDBConnection 01430 (DBConfig.getDBConnectionManager()); 01431 } 01432 01433 if(rs.next()){ 01434 objManager.fillObject(proc, rs); 01435 rs.close(); 01436 stmt.close(); 01437 } 01438 01439 rs.close(); 01440 stmt.close(); 01441 } catch (SQLException E) { 01442 throw new JetWebDBException(E, "failed to select ID", checkquery); 01443 } 01444 } 01445 01446 01453 public static boolean selectId(RunSeriesCollection rsc) 01454 throws JetWebException{ 01455 01456 if(rsc.getId() >=0) return true; 01457 01458 Statement stmt; 01459 ResultSet rs; 01460 String query = 01461 "SELECT MAX(runseries_collection_id) FROM runseries_collection"; 01462 01463 try { 01464 stmt = DBConfig.getConnection().createStatement(); 01465 rs = stmt.executeQuery(query); 01466 if (rs.next()){ 01467 rsc.setId(rs.getInt(1)+1); 01468 } else { 01469 rsc.setId(1); 01470 } 01471 } catch (SQLException E) { 01472 throw new JetWebDBException(E, query, "Id:" + rsc.getId()); 01473 } 01474 01475 return false; 01476 } 01477 01481 public static synchronized boolean addToDB(RunSeriesCollection rsc) 01482 throws JetWebException { 01483 01484 if (selectId(rsc)) { 01485 if (exists 01486 ("runseries_collection", "runseries_collection_id", 01487 rsc.getId())) { 01488 throw new JetWebException 01489 ("Attempt to re-store an existing run series collection.", 01490 "id = " + rsc.getId()); 01491 } 01492 } 01493 01494 Statement stmt; 01495 //ResultSet rs; 01496 01497 //make sure we don't store the same runseries in the collection twice 01498 Vector<Integer> storedRunSeries = new Vector<Integer>(); 01499 01500 for (RunSeries runSeries : rsc.getRunSeriesList()){ 01501 if(!storedRunSeries.contains(runSeries.getId())){ 01502 String ins = 01503 "INSERT INTO runseries_collection (runseries_collection_id,runseries_id) VALUES('" 01504 + rsc.getId() + "','" 01505 + runSeries.getId() + "')"; 01506 01507 try { 01508 stmt = DBConfig.getConnection().createStatement(); 01509 int result = stmt.executeUpdate(ins); 01510 storedRunSeries.add(runSeries.getId()); 01511 //rs.close(); 01512 stmt.close(); 01513 } catch (SQLException E) { 01514 throw new JetWebDBException(E, ins, "Id:" + rsc.getId()); 01515 } 01516 } 01517 } 01518 01519 return true; 01520 } 01521 01522 public static synchronized boolean updateDB 01523 (RunSeriesCollection rsc, RunSeries runseries) 01524 throws JetWebException{ 01525 01526 01527 try{ 01528 01529 String sql = "SELECT * FROM runseries_collection WHERE " + 01530 "runseries_collection_id = " + rsc.getId() + 01531 " AND runseries_id="+runseries.getId(); 01532 01533 Statement stmt = DBConfig.getConnection().createStatement(); 01534 ResultSet rs = stmt.executeQuery(sql); 01535 if(rs.next()){ 01536 rs.close(); 01537 stmt.close(); 01538 return false; 01539 } 01540 rs.close(); 01541 sql= "INSERT INTO runseries_collection(" + 01542 "runseries_collection_id, runseries_id) VALUES ("+ 01543 rsc.getId()+"," + runseries.getId() +")"; 01544 01545 stmt.executeUpdate(sql); 01546 stmt.close(); 01547 }catch(SQLException err){ 01548 throw new JetWebException(err); 01549 } 01550 01551 return true; 01552 } 01553 01554 01558 public static synchronized boolean updateDB(RunSeriesCollection rsc) 01559 throws JetWebException { 01560 01561 if (rsc.getId() < 0 || 01562 !exists("runseries_collection", "runseries_collection_id", 01563 rsc.getId())) { 01564 addToDB(rsc); 01565 //throw new JetWebException("Attempt to update a non-existant run series collection.", 01566 // "id = " + rsc.getId()); 01567 } 01568 01569 String sql = ""; 01570 01571 try { 01572 01573 Statement stmt = DBConfig.getConnection().createStatement(); 01574 ResultSet rs; 01575 01576 for (RunSeries runSeries : rsc.getRunSeriesList()){ 01577 01578 sql = "SELECT * FROM runseries_collection WHERE runseries_collection_id=" 01579 +rsc.getId()+" AND runseries_id="+runSeries.getId(); 01580 01581 rs = stmt.executeQuery(sql); 01582 if (!rs.next()) { 01583 01584 sql = 01585 "INSERT INTO runseries_collection (runseries_collection_id,runseries_id) VALUES('" 01586 + rsc.getId() + "','" 01587 + runSeries.getId() + "')"; 01588 int result = stmt.executeUpdate(sql); 01589 } 01590 rs.close(); 01591 } 01592 01593 stmt.close(); 01594 } catch (SQLException E) { 01595 throw new JetWebDBException(E, sql, "Id:" + rsc.getId()); 01596 } 01597 01598 return true; 01599 } 01600 01601 }
Generated Wed Jan 17 09:14:27 GMT 2007