Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

DBPlotManager.java

Go to the documentation of this file.
00001 package cedar.jetweb.db;
00002 
00003 import cedar.jetweb.JetWebConfig;
00004 import cedar.jetweb.JetWebException;
00005 import cedar.jetweb.model.RunSeries;
00006 import cedar.jetweb.model.Model;
00007 import cedar.jetweb.generator.Generator;
00008 import cedar.jetweb.model.paper.*;
00009 import cedar.jetweb.model.plots.*;
00010 import cedar.jetweb.job.CutCollection;
00011 
00012 import java.io.File;
00013 
00014 import java.sql.*;
00015 
00016 import java.util.ArrayList;
00017 import java.util.Collections;
00018 import java.util.Date;
00019 import java.util.Enumeration;
00020 import java.util.HashSet;
00021 import java.util.Iterator;
00022 import java.util.List;
00023 import java.util.Vector;
00024 import java.util.HashMap;
00025 import java.util.TreeMap;
00026 import java.util.Collection;
00027 
00028 
00037 public abstract class DBPlotManager {
00038 
00042     public static final String REAL = "data_point";
00043 
00047     public static final String PREDICTED = "predicted_point";
00048 
00052     public static final String FITTED = "fitted_point";
00053 
00061     private static PreparedStatement insertRealData_;
00062     private static PreparedStatement insertPredictedData_; 
00063     private static PreparedStatement insertFittedData_; 
00064     private static PreparedStatement retrieveRealPaper_;
00065     private static PreparedStatement selectPlotsForPaper_;
00066     private static boolean initialisedData_ = false;
00067     private static boolean initialisedRetrievePaper_ = false;
00068 
00069 
00070     private static void initData(){
00071 
00072     if(initialisedData_)return;
00073     initialisedData_ = true;
00074 
00075     try{
00076     insertRealData_ = 
00077         DBConfig.getConnection().prepareStatement
00078         ("INSERT INTO data_point"+
00079          "(csn_id,pnt_no,x,y,dy_up,dy_down,bin_width) VALUES" + 
00080          "(?,?,?,?,?,?,?)");
00081 
00082     insertPredictedData_ = 
00083         DBConfig.getConnection().prepareStatement
00084         ("INSERT INTO predicted_point" +
00085          "(runseries_id,csn_id,pnt_no,x,y,dy_up,dy_down,bin_width) "+
00086          "VALUES" + 
00087          "(?,?,?,?,?,?,?,?)");
00088      
00089     insertFittedData_ = 
00090         DBConfig.getConnection().prepareStatement
00091         ("INSERT INTO fitted_point" + 
00092          "(fit_id,chi2,csn_id,pnt_no,x,y,dy_up,dy_down,bin_width) VALUES" +
00093          "(?,?,?,?,?,?,?,?,?)");
00094     }catch(SQLException err){
00095         System.out.println
00096         ("Error: cannot initialse prepared statements "+
00097          "for inserting data in DB");
00098         System.out.println(err.getMessage());
00099         err.printStackTrace();
00100     }
00101      return;
00102     }
00103     private static void initRetrievePaper(){
00104     if(initialisedRetrievePaper_) return;
00105     initialisedRetrievePaper_ = true;
00106     try{
00107         retrieveRealPaper_ = 
00108         DBConfig.getConnection().prepareStatement
00109         ("SELECT * FROM paper WHERE paper_id=?");
00110 
00111         selectPlotsForPaper_ = 
00112         DBConfig.getConnection().prepareStatement
00113         ("SELECT * FROM cross_section where paper_id=?");
00114 
00115     }catch(SQLException err){
00116         System.out.println
00117         ("Error: cannot initialise prepared statements " +
00118          "for retrieving real papers from DB");
00119         System.out.println(err.getMessage());
00120         err.printStackTrace();
00121     }
00122     }
00123 
00124     private static void checkInserts(){
00125     
00126     boolean start = initialisedData_;
00127     initData();
00128     if(start!=initialisedData_) return;
00129 
00130     //Just a test because sometimes we drop the preparedstatement
00131     //if we lose the connection to the DB
00132     try{
00133         try{
00134         insertRealData_.setInt(1, 1);
00135         }catch(NullPointerException err){
00136         //presume DB connection is lost if we get here :(
00137         System.out.println
00138             ("Lost DB connection!  Getting new PreparedStatements");
00139         insertRealData_.close();
00140         insertPredictedData_.close();
00141         insertFittedData_.close();
00142         initialisedData_ = false;
00143         initData();
00144         }
00145     }catch(SQLException sqlErr){
00146         System.out.println
00147         ("DBPlotManager: unable to check status of DB connection");
00148         System.out.println(sqlErr.getMessage());
00149         sqlErr.printStackTrace();
00150     }
00151     return;
00152     }
00153 
00154     public static synchronized Collection<RealPaper> getRealPapers()
00155     throws JetWebException{
00156     Vector<RealPaper> papers = new Vector<RealPaper>();
00157     String selectString = "SELECT * FROM paper";
00158     try{
00159 
00160         Statement stmt = DBConfig.getConnection().createStatement();
00161             ResultSet dbrs = stmt.executeQuery(selectString);
00162 
00163             while (dbrs.next()) {
00164                 RealPaper paper = new RealPaper();
00165                 fill(paper, dbrs);
00166                 papers.add(paper);
00167             }
00168 
00169             dbrs.close();
00170             stmt.close();
00171 
00172     } catch (SQLException E) {
00173             System.out.println("SQLException: " + E.getMessage());
00174             System.out.println("SQLState:     " + E.getSQLState());
00175             System.out.println("VendorError:  " + E.getErrorCode());
00176             throw new JetWebDBException(E, selectString, "");
00177         }
00178 
00179     return papers;
00180     }
00181 
00182     public static synchronized Collection<Paper> getPapers()
00183         throws JetWebException {
00184         Vector<Paper> papers = new Vector<Paper>();
00185         String selectString = "SELECT * from paper";
00186 
00187         try {
00188             Statement stmt = DBConfig.getConnection().createStatement();
00189             ResultSet dbrs = stmt.executeQuery(selectString);
00190 
00191             while (dbrs.next()) {
00192                 Paper paper = new Paper(true);
00193                 fill(paper, dbrs);
00194                 papers.add(paper);
00195             }
00196 
00197             dbrs.close();
00198             stmt.close();
00199         } catch (SQLException E) {
00200             System.out.println("SQLException: " + E.getMessage());
00201             System.out.println("SQLState:     " + E.getSQLState());
00202             System.out.println("VendorError:  " + E.getErrorCode());
00203             throw new JetWebDBException(E, selectString, "");
00204         }
00205 
00206         return papers;
00207     }
00208 
00213     public static synchronized void fillRealPaper(RealPaper paper)
00214     throws JetWebException{
00215     
00216     initRetrievePaper();
00217     boolean filled = false;
00218 
00219     try{
00220         retrieveRealPaper_.setInt(1, paper.getId());
00221         ResultSet dbrs = retrieveRealPaper_.executeQuery();
00222         while(dbrs.next()){
00223         fill(paper, dbrs);
00224         filled = true;
00225         }
00226         dbrs.close();
00227         retrieveRealPaper_.clearParameters();
00228     }catch(SQLException err){
00229         System.out.println("SQLException: " + err.getMessage());
00230             System.out.println("SQLState:     " + err.getSQLState());
00231             System.out.println("VendorError:  " + err.getErrorCode());
00232             throw new JetWebDBException(err, ((Integer)paper.getId()).toString(), "");
00233     }
00234 
00235     if(!filled){
00236         throw new JetWebException("Paper not filled."," ");
00237     }
00238     return;
00239     }
00240 
00246     public static synchronized void fillPaper(Paper paper)
00247         throws JetWebException {
00248         String selectString = "SELECT * from paper where paper_id=" + paper.getId();
00249         boolean filled = false;
00250 
00251         try {
00252             Statement stmt = DBConfig.getConnection().createStatement();
00253             ResultSet dbrs = stmt.executeQuery(selectString);
00254 
00255             while (dbrs.next()) {
00256                 fill(paper, dbrs);
00257                 filled = true;
00258             }
00259 
00260             dbrs.close();
00261             stmt.close();
00262         } catch (SQLException E) {
00263             System.out.println("SQLException: " + E.getMessage());
00264             System.out.println("SQLState:     " + E.getSQLState());
00265             System.out.println("VendorError:  " + E.getErrorCode());
00266             throw new JetWebDBException(E, selectString, "");
00267         }
00268 
00269         if (!filled) {
00270             throw new JetWebException("Paper not filled.", " ");
00271         }
00272     }
00273 
00281     public static synchronized int getPaperId(String dirName)
00282         throws JetWebException {
00283         int paperId = -1;
00284         String query = "SELECT paper_id FROM paper WHERE directory='" + dirName + "'";
00285 
00286         try {
00287             Statement stmt = DBConfig.getConnection().createStatement();
00288             ResultSet rs = stmt.executeQuery(query);
00289 
00290             if (rs.next()) {
00291                 paperId = rs.getInt("paper_id");
00292             }
00293 
00294             rs.close();
00295             stmt.close();
00296         } catch (SQLException e) {
00297             System.out.println("Failed to select paperId for dirname:" + dirName + "error:" + e);
00298             throw new JetWebDBException(e, query, "Directory:" + dirName);
00299         }
00300 
00301         return paperId;
00302     }
00303 
00304     private static void fill(RealPaper paper, ResultSet dbrs)
00305     throws SQLException, JetWebException{
00306     paper.setAccelerator(dbrs.getString("accelerator"));
00307     paper.setDirName(dbrs.getString("directory"));
00308     paper.setReference(dbrs.getString("reference"));
00309     paper.setTitle(dbrs.getString("title"));
00310     paper.setId(dbrs.getInt("paper_id"));
00311     paper.setCollaboration(dbrs.getString("collaboration"));
00312     paper.setCodeAuthor(dbrs.getString("code_author"));
00313     paper.setCodeContact(dbrs.getString("code_contact"));
00314     paper.setIRN(dbrs.getInt("spires_id"));
00315     fillPlots(paper);
00316     return;
00317     }
00318 
00319     private static void fill(Paper paper, ResultSet dbrs)
00320         throws SQLException, JetWebException {
00321         if (paper.isReal()) {
00322             paper.setAccelerator(dbrs.getString("accelerator"));
00323             paper.setDirName(dbrs.getString("directory"));
00324             paper.setReference(dbrs.getString("reference"));
00325             paper.setTitle(dbrs.getString("title"));
00326             paper.setID(dbrs.getInt("paper_id"));
00327             paper.setCollaboration(dbrs.getString("collaboration"));
00328             paper.setCodeAuthor(dbrs.getString("code_author"));
00329             paper.setContact(dbrs.getString("code_contact"));
00330             paper.setIRN(dbrs.getInt("spires_id"));
00331 
00332             // Now the plots.
00333             fillPlots(paper);
00334         } else {
00335             throw new JetWebDBException("Attempt to fill MC Paper from DB is not valid",
00336                 "PaperId:" + paper.getId());
00337         }
00338     }
00339 
00344     public static synchronized void fillPlots(RealPaper paper)
00345     throws JetWebException{
00346     initRetrievePaper();
00347     try{
00348         selectPlotsForPaper_.setInt(1, paper.getId());
00349         ResultSet csrs = selectPlotsForPaper_.executeQuery();
00350         while(csrs.next()){
00351         RealPlot plot = new RealPlot();
00352         fill(plot, csrs);
00353         paper.addPlot(plot);
00354         }
00355         selectPlotsForPaper_.clearParameters();
00356     }catch(SQLException E){
00357         System.out.println("SQLException: " + E.getMessage());
00358             System.out.println("SQLState:     " + E.getSQLState());
00359             System.out.println("VendorError:  " + E.getErrorCode());
00360             throw new JetWebDBException(E, "SELECT", 
00361                     "PaperId:" + paper.getId());
00362     }
00363     }
00364 
00371     public static synchronized void fillPlots(Paper paper)
00372         throws JetWebException {
00373         // Run a query looking for all plots in the cross_section table which
00374         // match this paper_id.
00375         String query = "SELECT * from cross_section where paper_id=" +
00376             Integer.toString(paper.getId());
00377 
00378         try {
00379             Statement stmt = DBConfig.getConnection().createStatement();
00380             ResultSet csrs = stmt.executeQuery(query);
00381 
00382             while (csrs.next()) {
00383                 RealPlot plot = new RealPlot();
00384 
00385                 //System.out.println("plot got");
00386                 fill(plot, csrs);
00387                 paper.addPlot(plot);
00388             }
00389 
00390             csrs.close();
00391             stmt.close();
00392         } catch (SQLException E) {
00393             System.out.println("SQLException: " + E.getMessage());
00394             System.out.println("SQLState:     " + E.getSQLState());
00395             System.out.println("VendorError:  " + E.getErrorCode());
00396             throw new JetWebDBException(E, query, "PaperId:" + paper.getId());
00397         }
00398     }
00399 
00404     public static synchronized void fromDB(String ConName, RealPlot plot)
00405         throws JetWebException {
00406         // Run a query looking for all plots in the cross_section table which
00407         // match this paper_id.
00408         String query = "SELECT * from cross_section where csn_id=" +
00409             Integer.toString(plot.getId());
00410 
00411         try {
00412             Statement stmt = DBConfig.getConnection().createStatement();
00413             ResultSet csrs = stmt.executeQuery(query);
00414 
00415             while (csrs.next()) {
00416                 //System.out.println("plot got");
00417                 fill(plot, csrs);
00418             }
00419 
00420             csrs.close();
00421             stmt.close();
00422         } catch (SQLException E) {
00423             System.out.println("SQLException: " + E.getMessage());
00424             System.out.println("SQLState:     " + E.getSQLState());
00425             System.out.println("VendorError:  " + E.getErrorCode());
00426             throw new JetWebDBException(E, query, "PlotId:" + plot.getId());
00427         }
00428     }
00429 
00430     public static synchronized void fromDB(RealPlot plot)
00431         throws JetWebException {
00432         // Run a query looking for all plots in the cross_section table which
00433         // match this paper_id.
00434         String query = "SELECT * from cross_section where csn_id=" +
00435             Integer.toString(plot.getId());
00436 
00437         try {
00438         Statement stmt = DBConfig.getConnection().createStatement();
00439         //Statement stmt = DBConfig.getDBConnectionManager().
00440         //getConnection("hepdata").createStatement();
00441             ResultSet csrs = stmt.executeQuery(query);
00442 
00443             while (csrs.next()) {
00444                 //System.out.println("plot got");
00445                 fill(plot, csrs);
00446             }
00447 
00448             csrs.close();
00449             stmt.close();
00450         } catch (SQLException E) {
00451             System.out.println("SQLException: " + E.getMessage());
00452             System.out.println("SQLState:     " + E.getSQLState());
00453             System.out.println("VendorError:  " + E.getErrorCode());
00454             throw new JetWebDBException(E, query, "PlotId:" + plot.getId());
00455         }
00456     }
00457 
00458     public static synchronized boolean addRealPaper(RealPaper paper)
00459     throws JetWebException{
00460     boolean paperExisted = false;
00461 
00462     String query = "";
00463 
00464     if(paper.getId()>0){
00465         paperExisted = true;
00466         query = "SELECT * FROM paper WHERE paper_id="+paper.getId();
00467     
00468         try{
00469         Statement stmt = DBConfig.getConnection().createStatement();
00470         ResultSet pprrs = stmt.executeQuery(query);
00471         while(pprrs.next()){
00472             Statement stmt2 = 
00473             DBConfig.getConnection().createStatement();
00474             String mod = "UPDATE paper set title='" + 
00475             paper.getTitle() + "', " + 
00476             "reference='" + paper.getReference()+"', " + 
00477             "directory='" + paper.getDirName()+"', " + 
00478             "collaboration='" + paper.getCollaboration() + "', " +
00479             "code_author='" + paper.getCodeAuthor() + "', " +
00480             "code_contact='" + paper.getCodeContact() + "', " + 
00481             "accelerator='" + paper.getAccelerator() + "'," + 
00482             "spires_id='" + paper.getIRN() + "', " + 
00483             "WHERE paper_id=" + paper.getId();
00484             Integer rowsChanged = stmt2.executeUpdate(mod);
00485             stmt2.close();
00486             
00487         }
00488         pprrs.close();
00489         stmt.close();
00490 
00491         }catch(SQLException err){
00492         System.out.println("SQLException: " + err.getMessage());
00493                 System.out.println("SQLState:     " + err.getSQLState());
00494                 System.out.println("VendorError:  " + err.getErrorCode());
00495                 throw new JetWebDBException(err, query, 
00496                         "PaperId:" + paper.getId());
00497         }
00498     }else{
00499         String ins =
00500                 "Insert into paper " + 
00501         "(title, reference, directory, collaboration, " + 
00502         "accelerator, code_contact, code_author, spires_id)" +
00503                 "values('" + paper.getTitle() + "','" + 
00504         paper.getReference() + "','" +
00505                 paper.getDirName() + "','" + 
00506         paper.getCollaboration() + "','" +
00507                 paper.getAccelerator() + "','" + 
00508         paper.getCodeContact() + "','" +
00509                 paper.getCodeAuthor() + "," + 
00510         paper.getIRN()+"')";
00511 
00512             try {
00513                 Statement stmt = DBConfig.getConnection().createStatement();
00514                 int rowsChanged = stmt.executeUpdate(ins);
00515                 stmt.close();
00516             } catch (SQLException E) {
00517                 System.out.println("SQLException: " + E.getMessage());
00518                 System.out.println("SQLState:     " + E.getSQLState());
00519                 System.out.println("VendorError:  " + E.getErrorCode());
00520                 throw new JetWebDBException(E, ins, "PaperId:" + paper.getId());
00521             }
00522         }
00523     
00524     return paperExisted;
00525     }
00526 
00527 
00537     public static synchronized boolean addPaper(Paper paper)
00538         throws JetWebException {
00539         boolean paperExisted = false;
00540 
00541         if (paper.getId() > 0) {
00542             paperExisted = true;
00543 
00544             String query = "SELECT * from paper where paper_id=" + paper.getId();
00545 
00546             try {
00547                 Statement stmt = DBConfig.getConnection().createStatement();
00548                 ResultSet pprrs = stmt.executeQuery(query);
00549 
00550                 while (pprrs.next()) {
00551                     Statement stmt2 = DBConfig.getConnection().createStatement();
00552                     String mod = "UPDATE paper SET title='" + paper.getTitle() + "',reference='" +
00553                         paper.getReference() + "', directory='" + paper.getDirName() +
00554                         "', collaboration='" + paper.getCollaboration() + "', code_author='" +
00555                         paper.getCodeAuthor() + "', code_contact='" + paper.getContact() +
00556                         "', accelerator='" + paper.getAccelerator() + 
00557                         "', spires_id='" + paper.getIRN() + "' where paper_id=" +
00558                         paper.getId();
00559                     int rowsChanged = stmt2.executeUpdate(mod);
00560                     stmt2.close();
00561                 }
00562 
00563                 pprrs.close();
00564                 stmt.close();
00565             } catch (SQLException E) {
00566                 System.out.println("SQLException: " + E.getMessage());
00567                 System.out.println("SQLState:     " + E.getSQLState());
00568                 System.out.println("VendorError:  " + E.getErrorCode());
00569                 throw new JetWebDBException(E, query, "PaperId:" + paper.getId());
00570             }
00571         } else {
00572             String ins =
00573                 "Insert into paper (title, reference, directory, collaboration, accelerator, code_contact, code_author, spires_id)" +
00574                 "values('" + paper.getTitle() + "','" + paper.getReference() + "','" +
00575                 paper.getDirName() + "','" + paper.getCollaboration() + "','" +
00576                 paper.getAccelerator() + "','" + paper.getContact() + "','" +
00577                 paper.getCodeAuthor() + ","+paper.getIRN()+"')";
00578 
00579             try {
00580                 Statement stmt = DBConfig.getConnection().createStatement();
00581                 int rowsChanged = stmt.executeUpdate(ins);
00582                 stmt.close();
00583             } catch (SQLException E) {
00584                 System.out.println("SQLException: " + E.getMessage());
00585                 System.out.println("SQLState:     " + E.getSQLState());
00586                 System.out.println("VendorError:  " + E.getErrorCode());
00587                 throw new JetWebDBException(E, ins, "PaperId:" + paper.getId());
00588             }
00589         }
00590 
00591         return paperExisted;
00592     }
00593 
00594     private static void fill(RealPlot plot, ResultSet csrs)
00595         throws SQLException, JetWebException {
00596         plot.setPaperId(csrs.getInt("paper_id"));
00597         plot.setNumber(csrs.getInt("plot_number"));
00598         plot.setTitle(csrs.getString("title"));
00599         plot.setXLabel(csrs.getString("x_axis_title"));
00600         plot.setYLabel(csrs.getString("y_axis_title"));
00601         plot.setId(csrs.getInt("csn_id"));
00602         plot.setShape(csrs.getInt("is_shape") == 1);
00603     
00604     Integer cutId = csrs.getInt("cut_collection_id");
00605     if(cutId!=null){
00606         CutCollection cuts = new CutCollection(cutId);
00607         plot.setCutCollection(cuts);
00608     }
00609 
00610         if (csrs.getInt("in_default") >= 1) {
00611             plot.setDefaultSum(true);
00612         }
00613 
00614         if (csrs.getInt("in_default") == 2) {
00615             plot.setDefaultFit(true);
00616         }
00617 
00618         if (csrs.getString("y_axis_type").equals("log")) {
00619             plot.setLogarithmic(true);
00620         } else {
00621             plot.setLogarithmic(false);
00622         }
00623 
00624         plot.setSysScale(csrs.getFloat("scale_factor"));
00625 
00626         plot.setCollisionId(csrs.getInt("collision_id"));
00627 
00628         if (!plot.populateDBData()) {
00629             System.out.println("WARNING: datapoints not filled for DataPlot " + plot.getNumber());
00630         }
00631     }
00632 
00641     public static synchronized Vector<DataPoint> getDataPoints(DataPlot plot)
00642         throws JetWebException {
00643         //select records from appropriate table for this cross_section
00644         String queryString = getQueryString(plot);
00645     System.out.println(queryString);
00646         Vector<DataPoint> dataPoints = new Vector<DataPoint>();
00647     HashMap<Integer, DataPoint> pointMap = 
00648         new HashMap<Integer, DataPoint>();
00649 
00650     HashMap<Integer, Boolean> isInCuts = null;
00651 
00652     if(plot instanceof PredictedPlot){
00653         isInCuts = new HashMap<Integer, Boolean>();
00654     }
00655      
00656 
00657         try {
00658             Statement stmt = DBConfig.getConnection().createStatement();
00659             ResultSet dsrs = stmt.executeQuery(queryString);
00660 
00661             while (dsrs.next()) {
00662 
00663         //check that the runseries for this data_point 
00664         //generates data in the correct region.
00665 
00666         boolean inCuts = true;
00667 
00668         if(plot instanceof PredictedPlot){
00669 
00670             Integer rsid = dsrs.getInt("runseries_id");
00671 
00672             Boolean foundInCuts = isInCuts.get(rsid);
00673 
00674             if(foundInCuts==null){
00675 
00676             String selectCuts = 
00677                 "SELECT cut_collection_id FROM runseries WHERE " + 
00678                 "runseries_id = " + dsrs.getInt("runseries_id");
00679 
00680             System.out.println(selectCuts);
00681             
00682             Statement cutstmt = 
00683                 DBConfig.getConnection().createStatement();
00684             ResultSet rs = cutstmt.executeQuery(selectCuts);
00685             
00686             while(rs.next()){
00687                 CutCollection runCuts = 
00688                 new CutCollection(rs.getInt(1));
00689                 inCuts = plot.getCutCollection().isWithin(runCuts);
00690             }
00691 
00692             isInCuts.put(rsid, inCuts);
00693 
00694             }else{
00695             inCuts = foundInCuts;
00696             }
00697         }
00698 
00699         if(inCuts){
00700             //add a new DataPoint 
00701             DataPoint point = new DataPoint();
00702             point.setX(dsrs.getFloat("x"));
00703             point.setY(dsrs.getFloat("y"));
00704             point.setYUp(dsrs.getFloat("dy_up"));
00705             point.setYDown(dsrs.getFloat("dy_down"));
00706             point.setBinWidth(dsrs.getFloat("bin_width"));
00707             point.setNumber(dsrs.getInt("pnt_no"));
00708             //0,x 1,y, 2,ye 3,ye 4, bin width 5, chi2
00709             if (plot instanceof FittedPlot){
00710             point.setChi2(dsrs.getFloat("chi2"));
00711             }
00712         
00713             //System.out.println("Read datapoint for plot " + 
00714             //         plot.getId() + 
00715             //         point.toString());
00716 
00717 
00718             //Integer pointNumber = dsrs.getInt("pnt_no");
00719             Integer pointNumber = point.getNumber();
00720 
00721             DataPoint existingPoint = 
00722             pointMap.get(pointNumber);
00723     
00724             if(existingPoint != null ){
00725 
00726             System.out.println("Found existing point: " +
00727                        existingPoint.toString());
00728 
00729             point.add(existingPoint);
00730             }
00731 
00732             pointMap.put(pointNumber, point);
00733 
00734             //dataPoints.add(point);
00735         }
00736 
00737             }
00738 
00739         dataPoints = new Vector<DataPoint>(pointMap.values());
00740 
00741         
00742 
00743             dsrs.close();
00744             stmt.close();
00745         } catch (SQLException e) {
00746             System.out.println("Error reading data points " + e);
00747             throw new JetWebDBException(e, queryString, "DataPlot.id:" + plot.getId());
00748         }
00749 
00750     Collections.sort(dataPoints);
00751 
00752         return dataPoints;
00753     }
00754 
00755     public static synchronized HashMap<Integer, Paper> 
00756     getPredictedPapers(Model model) throws JetWebException{
00757     
00758     HashMap<Integer, Paper> foundPapers = 
00759         new HashMap<Integer, Paper>();
00760 
00761     try{
00762         PreparedStatement pstmt = 
00763         DBConfig.getConnection().prepareStatement
00764         ("select cross_section.paper_id, " + 
00765          "cross_section.cut_collection_id, " + 
00766          "predicted_point.runseries_id, " + 
00767          "runseries.cut_collection_id, " + 
00768          "cross_section.csn_id from " + 
00769          "cross_section, predicted_point, model, " + 
00770          "runseries_collection, runseries where " + 
00771          "runseries.runseries_id = " + 
00772          "runseries_collection.runseries_id and " + 
00773          "runseries_collection.runseries_collection_id =" + 
00774          "model.runseries_collection_id and " + 
00775          "runseries_collection.runseries_id =" + 
00776          "predicted_point.runseries_id and " + 
00777          "predicted_point.csn_id =cross_section.csn_id and " + 
00778          "model.mdl_id =?");
00779 
00780         pstmt.setInt(1, model.getId());
00781         ResultSet rs = pstmt.executeQuery();
00782             
00783         HashMap<Integer, PredictedPlot> foundPlots = 
00784         new HashMap<Integer, PredictedPlot>();
00785         
00786         HashMap<Integer, CutCollection> paperCuts = 
00787         new HashMap<Integer, CutCollection>();
00788         
00789         HashMap<Integer, CutCollection> rsCuts = 
00790         new HashMap<Integer, CutCollection>();
00791         
00792         HashMap<Integer, RunSeries> foundRunSeries = 
00793         new HashMap<Integer, RunSeries>();
00794 
00795         PreparedStatement paperStmt = 
00796         DBConfig.getConnection().prepareStatement
00797         ("SELECT * FROM paper where paper_id=?");
00798         
00799         PreparedStatement plotStmt = 
00800         DBConfig.getConnection().prepareStatement
00801         ("SELECT * FROM cross_section WHERE csn_id=?");
00802 
00803         while(rs.next()){
00804         Integer paperId = rs.getInt(1);
00805         Paper mcpaper;
00806         Integer paperCutsId = rs.getInt(2);
00807         Integer rsId = rs.getInt(3);
00808         Integer rsCutsId = rs.getInt(4);
00809         Integer plotId = rs.getInt(5);
00810         
00811         CutCollection paperCut = paperCuts.get(paperCutsId);
00812         CutCollection rsCut = rsCuts.get(rsCutsId);
00813         
00814         if(paperCut==null){
00815             paperCut = new CutCollection(paperCutsId);
00816             paperCuts.put(paperCutsId, paperCut);
00817         }
00818         
00819         if(rsCut==null){
00820             rsCut = new CutCollection(rsCutsId);
00821             rsCuts.put(rsCutsId, rsCut);
00822         }
00823         
00824         boolean inCuts = paperCut.isWithin(rsCut);
00825         if(inCuts){
00826             if(!foundPapers.containsKey(paperId)){
00827             
00828             mcpaper = new Paper(false);
00829             
00830             paperStmt.setInt(1, paperId);
00831             ResultSet paperRs = paperStmt.executeQuery();
00832             
00833             if(paperRs.next()){
00834                 mcpaper.setAccelerator(paperRs.getString("accelerator"));
00835                 mcpaper.setDirName(paperRs.getString("directory"));
00836                 mcpaper.setReference(paperRs.getString("reference"));
00837                 mcpaper.setTitle(paperRs.getString("title"));
00838                 mcpaper.setID(paperId);
00839                 mcpaper.setCollaboration
00840                 (paperRs.getString("collaboration"));
00841                 mcpaper.setCodeAuthor(paperRs.getString("code_author"));
00842                 mcpaper.setContact(paperRs.getString("code_contact"));
00843                 mcpaper.setIRN(paperRs.getInt("spires_id"));
00844                 foundPapers.put(paperId, mcpaper);
00845             }
00846             
00847             }else{
00848             mcpaper = foundPapers.get(paperId);
00849             }
00850             
00851             PredictedPlot plot = foundPlots.get(plotId);
00852 
00853             if(plot==null){
00854             RealPlot compPlot = new RealPlot();
00855             
00856             plotStmt.setInt(1, plotId);
00857             
00858             ResultSet plotRs = plotStmt.executeQuery();
00859             if(plotRs.next()){
00860                 fill(compPlot, plotRs);
00861 
00862                 RunSeries runseries = foundRunSeries.get(rsId);
00863                 if(runseries==null){
00864                 runseries = RunSeries.Maker(rsId);
00865                 }
00866 
00867                 plot = new PredictedPlot(compPlot, runseries);
00868                 foundPlots.put(plotId, plot);
00869             }
00870             mcpaper.addPlot(plot);
00871             }
00872         }
00873         }
00874     }catch(SQLException err){
00875         throw new JetWebException(err);
00876     }
00877     return foundPapers; 
00878     }
00879 
00880 
00881     private static String getQueryString(DataPlot plot) {
00882         String rtn = "";
00883     
00884         if (plot instanceof PredictedPlot) {
00885         /*
00886             rtn = "SELECT * FROM " + PREDICTED + " where csn_id=" + plot.getId() +
00887                 " and runseries_id=" + ((PredictedPlot) plot).getRunSeriesId();
00888     */
00889         rtn = "SELECT " + PREDICTED + ".* FROM " + 
00890         PREDICTED + ", runseries_collection " +
00891         "WHERE " + PREDICTED + ".csn_id = " + plot.getId() + 
00892         " AND " + PREDICTED + 
00893         ".runseries_id=runseries_collection.runseries_id AND " + 
00894         "runseries_collection.runseries_collection_id =  " + 
00895         "(SELECT runseries_collection_id FROM runseries_collection " + 
00896         "WHERE runseries_id = " + 
00897         ((PredictedPlot) plot).getRunSeriesId() + ")";
00898 
00899 
00900         } else if (plot instanceof RealPlot) {
00901             rtn = "SELECT * FROM " + REAL + " where csn_id=" + plot.getId();
00902         } else if (plot instanceof FittedPlot) {
00903             rtn = "SELECT * FROM " + FITTED + " where csn_id=" + plot.getId() + " and fit_id=" +
00904                 ((FittedPlot) plot).getFit().getId();
00905         }
00906     //System.out.println(rtn);
00907         return rtn;
00908     }
00909 
00917     public static synchronized void updateCrossSectionPoints(DataPlot plot)
00918         throws JetWebException {
00919         String sqlString = getDeleteString(plot);
00920 
00921         try {
00922             Statement stmt = DBConfig.getConnection().createStatement();
00923             int rtn = stmt.executeUpdate(sqlString);
00924             stmt.close();
00925         } catch (SQLException e) {
00926             System.out.println("DB access error in updateCross_Section (delete data points):" + e);
00927             System.out.println("SQL was:" + sqlString);
00928             throw new JetWebDBException(e, sqlString, "DataPlot.id:" + plot.getId());
00929         }
00930 
00931         try {
00932             insertData(plot);
00933         } catch (JetWebException j) {
00934             // insertDataPlot throws an exception if it failed. If we
00935             // did not manage to insert the data, we should tidy up by
00936             // deleting any other data for this plot.
00937             try {
00938                 sqlString = getDeleteString(plot);
00939 
00940                 Statement stmt = DBConfig.getConnection().createStatement();
00941                 int rtn = stmt.executeUpdate(sqlString);
00942                 stmt.close();
00943             } catch (SQLException e) {
00944                 System.out.println("DB access error in updateCrossSection (delete data points):" +
00945                     e);
00946                 System.out.println("SQL was:" + sqlString);
00947                 throw new JetWebDBException(e, sqlString, "DataPlot.id:" + plot.getId());
00948             }
00949         }
00950     }
00951 
00952     private static String getDeleteString(DataPlot plot) {
00953         String rtn = "";
00954 
00955         if (plot instanceof PredictedPlot) {
00956             rtn = "DELETE FROM " + PREDICTED + " WHERE csn_id=" + plot.getId() +
00957                 " and runseries_id=" + ((PredictedPlot) plot).getRunSeriesId();
00958         } else if (plot instanceof FittedPlot) {
00959             rtn = "DELETE FROM " + FITTED + " WHERE csn_id=" + plot.getId() + " and fitted_id=" +
00960                 ((FittedPlot) plot).getFit().getId();
00961         } else if (plot instanceof RealPlot) {
00962             rtn = "DELETE FROM " + REAL + " WHERE csn_id=" + plot.getId();
00963         }
00964 
00965         return rtn;
00966     }
00967 
00974     public static synchronized void insertData(DataPlot plot)
00975         throws JetWebException {
00976 
00977     //System.out.println("Inserting data for plot " + plot.getId());
00978 
00979     //initData();
00980 
00981     checkInserts();
00982 
00983         Enumeration<DataPoint> unsortedPoints = plot.getDataPoints().elements();
00984         int pointNo = 0;
00985 
00986     TreeMap<Double, DataPoint> sortedPoints = 
00987         new TreeMap<Double, DataPoint>();
00988 
00989     while(unsortedPoints.hasMoreElements()){
00990         DataPoint pt = unsortedPoints.nextElement();
00991         sortedPoints.put(pt.getX(), pt);
00992     }
00993 
00994     PreparedStatement pstmt = insertRealData_;
00995 
00996     boolean isFitted = false;
00997 
00998     Integer startIndex = 1;
00999     try{
01000         if(plot instanceof PredictedPlot){
01001         pstmt = insertPredictedData_;
01002         pstmt.setInt(1, ((PredictedPlot) plot).getRunSeriesId());
01003         startIndex = 2;
01004         }else if(plot instanceof FittedPlot){
01005         pstmt = insertFittedData_;
01006         pstmt.setInt(1, ((FittedPlot) plot).getFit().getId());
01007         startIndex = 3;
01008         }
01009         pstmt.setInt(startIndex, plot.getId());
01010     }catch(SQLException err){
01011         System.out.println("Error: DBPlotManager.insertData(DataPlot) "+
01012                    "unable to set plot data in PreparedStatement");
01013         err.printStackTrace();
01014         throw new JetWebException(err);
01015     }
01016     ++startIndex;
01017     
01018         //while (points.hasMoreElements()) {
01019 
01020     //System.out.println("Number of data points to add = " + 
01021     //   sortedPoints.size());
01022 
01023     for(DataPoint point: sortedPoints.values()){
01024             pointNo++;
01025 
01026             //DataPoint point = points.nextElement();
01027         
01028         Integer pIndex = startIndex;
01029         try{
01030 
01031         if(plot instanceof FittedPlot){
01032             pstmt.setDouble(2, point.getChi2());
01033         }
01034 
01035         pstmt.setInt(pIndex, pointNo);
01036         ++pIndex;
01037         pstmt.setDouble(pIndex, point.getX());
01038         ++pIndex;
01039         pstmt.setDouble(pIndex, point.getY());
01040         ++pIndex;
01041         pstmt.setDouble(pIndex, point.getYUp());
01042         ++pIndex;
01043         pstmt.setDouble(pIndex, point.getYDown());
01044         ++pIndex;
01045         pstmt.setDouble(pIndex, point.getBinWidth());
01046         pstmt.executeUpdate();
01047                 
01048         }catch(SQLException err){
01049         System.out.println("insertDataPlot failed: " + err);
01050         System.out.println("Data: pIndex = "+pIndex);
01051         throw new JetWebException(err);
01052         }
01053         }
01054     try{
01055         pstmt.clearParameters();
01056         //pstmt.close();
01057     }catch(SQLException err){
01058         System.out.println
01059         ("DBPlotManager.insertData: " + 
01060          "unable to clear prepared statement");
01061         System.out.println("Data: "+pstmt);
01062       
01063         throw new JetWebException(err);
01064     }
01065     return;
01066     }
01067 
01077     public static synchronized void insertCrossSection(String ConName, 
01078                                RealPlot plot, int paperId)
01079         throws JetWebException {
01080         int inDefault = 0;
01081 
01082         if (plot.isDefaultSum()) {
01083             inDefault += 1;
01084         }
01085 
01086         if (plot.isDefaultFit()) {
01087             inDefault += 1;
01088         }
01089 
01090         String yAxisType = "lin";
01091 
01092         if (plot.isLogarithmic()) {
01093             yAxisType = "log";
01094         }
01095 
01096         int shape = 0;
01097 
01098         if (plot.isShape()) {
01099             shape = 1;
01100         }
01101 
01102     Integer cutId=-1;
01103 
01104     if(plot.getCutCollection()!=null){
01105         plot.getCutCollection().store();
01106         cutId = plot.getCutCollection().getId();
01107     }
01108 
01109         String sqlString = 
01110         "INSERT INTO cross_section " +
01111             "(paper_id," +
01112         "title,x_axis_title," + 
01113         "y_axis_title," + 
01114         "in_default," + 
01115         "plot_number," + 
01116         "scale_factor," + 
01117         "y_axis_type," + 
01118         "is_shape," + 
01119         "collision_id," +
01120         "cut_collection_id)" +
01121             " VALUES (" + paperId + ",'" + plot.getTitle() + "','" + plot.getXLabel() + "','" +
01122             plot.getYLabel() + "'," + inDefault + "," + plot.getNumber() + "," +
01123             plot.getSysScale() + ",'" + yAxisType + "'," + shape + "," + 
01124         plot.getCollisionId() +
01125         "," + cutId +
01126             ")";
01127 
01128     System.out.println(sqlString);
01129 
01130         try {
01131             Statement stmt = DBConfig.getConnection().createStatement();
01132             int rtn = stmt.executeUpdate(sqlString);
01133         stmt.close();
01134         } catch (SQLException e) {
01135             System.out.println("DB access error in insertCrossSection" + e);
01136             System.out.println("SQL String was: " + sqlString);
01137             throw new JetWebDBException(e, sqlString,
01138                 "plotId:" + plot.getId() + " paperId:" + paperId);
01139         }
01140 
01141         // Now get the new ID.
01142         plot.setId(getPlotId(paperId, plot.getNumber()));
01143 
01144         // Now add the plot switches.
01145         plot.storeSwitches();
01146     }
01147 
01148     public static synchronized void insertCrossSection(RealPlot plot, int paperId)
01149     throws JetWebException {
01150     insertCrossSection(DBConfig.JETWEB, plot, paperId);
01151     }
01152 
01159     public static synchronized void updateCrossSectionLabels(RealPlot plot)
01160         throws JetWebException {
01161         int inDefault = 0;
01162 
01163         if (plot.isDefaultSum()) {
01164             inDefault += 1;
01165         }
01166 
01167         if (plot.isDefaultFit()) {
01168             inDefault += 1;
01169         }
01170 
01171         String yAxisType = "lin";
01172 
01173         if (plot.isLogarithmic()) {
01174             yAxisType = "log";
01175         }
01176 
01177         int shape = 0;
01178 
01179         if (plot.isShape()) {
01180             shape = 1;
01181         }
01182 
01183     int cutId = -1;
01184 
01185     if(plot.getCutCollection()!=null){
01186         plot.getCutCollection().store();
01187         cutId = plot.getCutCollection().getId();
01188     }
01189 
01190         String sqlString = 
01191         "UPDATE cross_section SET paper_id='" + plot.getPaperId() + 
01192         "',title='" + plot.getTitle() + 
01193         "',x_axis_title='" + plot.getXLabel() + 
01194         "',y_axis_title='" + plot.getYLabel() + 
01195         "',in_default='" + inDefault + 
01196         "',plot_number='" + plot.getNumber() +
01197             "',scale_factor='" + plot.getSysScale() + 
01198         "',y_axis_type='" + yAxisType +
01199             "',is_shape='" + shape + 
01200         "',collision_id='" + plot.getCollisionId() +
01201         "',cut_collection_id=" + cutId +
01202             " where csn_id=" + plot.getId();
01203 
01204         try {
01205             Statement stmt = DBConfig.getConnection().createStatement();
01206             int rtn = stmt.executeUpdate(sqlString);
01207         stmt.close();
01208         } catch (SQLException e) {
01209             System.out.println("DB access error in updateCrossSectionLabels" + e);
01210             System.out.println("SQL String was: " + sqlString);
01211             throw new JetWebDBException(e, sqlString,
01212                 "plotId:" + plot.getId() + " paperId:" + plot.getPaperId());
01213         }
01214 
01215         // Now add/update the plot switches.
01216         plot.storeSwitches();
01217     }
01218 
01225     public static synchronized void insertFittedPrediction(FittedPlot plot)
01226         throws JetWebException {
01227         String ins1 = "INSERT INTO fitted_prediction (fit_id,csn_id,chi2,dof) VALUES(" +
01228             plot.getFit().getId() + "," + plot.getId() + "," + plot.getChi2()[0] + "," +
01229             plot.getChi2()[1] + ")";
01230 
01231         try {
01232             Statement stmt = DBConfig.getConnection().createStatement();
01233             stmt.executeUpdate(ins1);
01234         stmt.close();
01235         } catch (SQLException e) {
01236             System.out.println("insertFittedPrediction failed: " + e);
01237             throw new JetWebDBException(e, ins1, "FittedPlot.id:" + plot.getId());
01238         }
01239     }
01240 
01252     public static synchronized int getPlotId(String ConName, int paperId, int plotNumber)
01253         throws JetWebException {
01254         int csnId = -1;
01255         String sqlString = "SELECT csn_id FROM cross_section WHERE paper_id=" + paperId +
01256             " AND plot_number=" + plotNumber;
01257 
01258         try {
01259             Statement stmt = DBConfig.getConnection().createStatement();
01260             ResultSet rs = stmt.executeQuery(sqlString);
01261 
01262             if (rs.next()) {
01263                 csnId = rs.getInt("csn_id");
01264             }
01265 
01266             rs.close();
01267             stmt.close();
01268         } catch (SQLException e) {
01269             System.out.println("DB access error in getPlotId:" + e);
01270             System.out.println("SQL was: " + sqlString);
01271 
01272             throw new JetWebDBException(e, sqlString,
01273                 "PaperId:" + paperId + "plotNumber:" + plotNumber);
01274         }
01275 
01276         return csnId;
01277     }
01278 
01279     public static synchronized int getPlotId(int paperId, int plotNumber)
01280         throws JetWebException {
01281         int csnId = -1;
01282         String sqlString = "SELECT csn_id FROM cross_section WHERE paper_id=" + paperId +
01283             " AND plot_number=" + plotNumber;
01284 
01285     System.out.println(sqlString);
01286 
01287         try {
01288             Statement stmt = DBConfig.getConnection().createStatement();
01289             ResultSet rs = stmt.executeQuery(sqlString);
01290 
01291             if (rs.next()) {
01292                 csnId = rs.getInt("csn_id");
01293             }
01294 
01295             rs.close();
01296             stmt.close();
01297         } catch (SQLException e) {
01298             System.out.println("DB access error in getPlotId:" + e);
01299             System.out.println("SQL was: " + sqlString);
01300 
01301             throw new JetWebDBException(e, sqlString,
01302                 "PaperId:" + paperId + "plotNumber:" + plotNumber);
01303         }
01304 
01305         return csnId;
01306     }
01307 
01317     public static synchronized List<Integer> getCSSIds(int csnId, boolean sorted)
01318         throws JetWebException {
01319         List<Integer> sets = new ArrayList<Integer>();
01320 
01321         //get all cross section sets for a cross section
01322         String queryString = "SELECT csn_set_id FROM cross_section_set WHERE csn_id=" + csnId;
01323 
01324         try {
01325             Statement stmt = DBConfig.getConnection().createStatement();
01326             ResultSet rs = stmt.executeQuery(queryString);
01327 
01328             while (rs.next()) {
01329                 sets.add(new Integer(rs.getInt("csn_set_id")));
01330             }
01331 
01332             rs.close();
01333             stmt.close();
01334         } catch (SQLException e) {
01335             System.out.println("Error reading cross_section_set table " + e);
01336             throw new JetWebDBException(e, queryString, "CrossSectionId:" + csnId);
01337         }
01338 
01339         if (sorted) {
01340             Collections.sort(sets);
01341         }
01342 
01343         return sets;
01344     }
01345 
01355     public static synchronized List<Integer> getCsnIds(int cssId, 
01356                                boolean sorted)
01357         throws JetWebException {
01358         List<Integer> csns = new ArrayList<Integer>();
01359 
01360         //find all csns belonging to this set
01361         String queryString = "SELECT csn_id FROM cross_section_set WHERE csn_set_id=" + cssId;
01362 
01363         //+" ORDER BY csn_id";
01364         try {
01365             Statement stmt = DBConfig.getConnection().createStatement();
01366             ResultSet rs = stmt.executeQuery(queryString);
01367 
01368             while (rs.next()) {
01369                 csns.add(new Integer(rs.getInt("csn_id")));
01370             }
01371 
01372             rs.close();
01373             stmt.close();
01374         } catch (SQLException e) {
01375             System.out.println("Error reading cross_section_set table " + e);
01376             throw new JetWebDBException(e, queryString, "CrossSectionSetId:" + cssId);
01377         }
01378 
01379         if (sorted) {
01380             Collections.sort(csns);
01381         }
01382 
01383         return csns;
01384     }
01385 
01395     public static synchronized int getMaxCssId() throws JetWebException {
01396         int cssId = 0;
01397         String query = "SELECT MAX(csn_set_id) FROM cross_section_set";
01398 
01399         try {
01400             Statement stmt = DBConfig.getConnection().createStatement();
01401             ResultSet rs = stmt.executeQuery(query);
01402 
01403             if (rs.next()) {
01404                 cssId = rs.getInt(1);
01405             }
01406 
01407             rs.close();
01408             stmt.close();
01409         } catch (SQLException e) {
01410             System.out.println("Error reading cross_section_set table " + e);
01411             throw new JetWebDBException(e, query, "");
01412         }
01413 
01414         return cssId;
01415     }
01416 
01424     public static synchronized void insertCrossSectionSet(Iterator<Integer> plotIds, int cssId)
01425         throws JetWebException {
01426         String ins = "";
01427 
01428         try {
01429             Statement stmt = DBConfig.getConnection().createStatement();
01430 
01431             while (plotIds.hasNext()) {
01432                 ins = "INSERT INTO cross_section_set (csn_id,csn_set_id) VALUES(" +
01433                     (plotIds.next()).toString() + "," + cssId + ")";
01434 
01435                 int i = stmt.executeUpdate(ins);
01436             }
01437 
01438             stmt.close();
01439         } catch (SQLException e) {
01440             System.out.println("Error inserting data into cross_section_set table ");
01441             System.out.println(e);
01442             System.out.println("SQL was: " + ins);
01443             throw new JetWebDBException(e, ins, "CrossSectionSetId:" + cssId);
01444         }
01445     }
01446 
01455     public static synchronized boolean plotExists(DataPlot plot, RunSeries runSeries)
01456         throws JetWebException {
01457         int plotId = plot.getId();
01458         int runSeriesId = runSeries.getId();
01459         boolean exists = false;
01460         String queryString = "";
01461 
01462         try {
01463             Statement stmt = DBConfig.getConnection().createStatement();
01464             queryString = "SELECT COUNT(runseries_id) count_plots " +
01465                 "FROM predicted_point WHERE runseries_id=" + runSeriesId + " AND csn_id=" + plotId;
01466 
01467             ResultSet rs = stmt.executeQuery(queryString);
01468 
01469             if (rs.next()) {
01470                 int n = rs.getInt("count_plots");
01471 
01472                 if (n > 0) {
01473                     exists = true;
01474                 }
01475             }
01476 
01477             rs.close();
01478             stmt.close();
01479         } catch (SQLException e) {
01480             System.out.println("Error reading predicted_points table ");
01481             System.out.println(e);
01482             System.out.println("SQL was: " + queryString);
01483             throw new JetWebDBException(e, queryString,
01484                 "PlotId:" + plotId + " runSeriesId:" + runSeriesId);
01485         }
01486 
01487         return exists;
01488     }
01489 
01493     public static synchronized void populateSelection(PlotSelection selection)
01494         throws JetWebException {
01495 
01496         String queryString = "";
01497         int cssId = -1;
01498     selection.clearFitPlots();
01499 
01500         try {
01501             cssId = selection.getId();
01502 
01503             Statement stmt = DBConfig.getConnection().createStatement();
01504             queryString = "SELECT csn_id " + "FROM cross_section_set WHERE csn_set_id=" + cssId;
01505 
01506             ResultSet rs = stmt.executeQuery(queryString);
01507 
01508             while (rs.next()) {
01509                 selection.addFitPlot(PaperBank.getPlot(rs.getInt("csn_id")));
01510             }
01511 
01512             rs.close();
01513             stmt.close();
01514         } catch (SQLException e) {
01515             System.out.println("Error reading predicted_points table ");
01516             System.out.println(e);
01517             System.out.println("SQL was: " + queryString);
01518             throw new JetWebDBException(e, queryString, "CssId=" + cssId);
01519         } catch (JetWebException e) {
01520             throw e;
01521         }
01522     }
01523 
01524     public static double[] getChi2(int plotId, int fitId) {
01525         String queryString = "";
01526         double[] chi2 = { 0.0, 0.0 };
01527 
01528         try {
01529             Statement stmt = DBConfig.getConnection().createStatement();
01530             queryString = "SELECT * FROM fitted_prediction WHERE fit_id=" + fitId + " AND csn_id=" +
01531                 plotId;
01532 
01533             ResultSet rs = stmt.executeQuery(queryString);
01534 
01535             while (rs.next()) {
01536                 chi2[0] = rs.getFloat("chi2");
01537                 chi2[1] = rs.getFloat("dof");
01538             }
01539 
01540             rs.close();
01541             stmt.close();
01542         } catch (SQLException e) {
01543             System.out.println("Error reading fitted_prediction table ");
01544             System.out.println(e);
01545             System.out.println("SQL was: " + queryString);
01546 
01547             //throw new JetWebDBException(e,queryString,"CssId="+cssId);
01548             //  } catch (JetWebException e) {
01549             //      throw e;
01550         }
01551 
01552         return chi2;
01553     }
01554 
01559     public static void selectPlotSwitch(PlotSwitch plotSwitch)
01560         throws JetWebException {
01561         // Get all PlotSwitches for a cross section
01562         String queryString = "SELECT * FROM cross_section_switches WHERE switch_id=" +
01563             plotSwitch.getId();
01564 
01565         try {
01566             Statement stmt = DBConfig.getConnection().createStatement();
01567             ResultSet rs = stmt.executeQuery(queryString);
01568 
01569             while (rs.next()) {
01570                 plotSwitch.setGeneratorName(rs.getString("generator_name"));
01571 
01572                 plotSwitch.setGeneratorVersion(rs.getFloat("generator_version"));
01573 
01574                 plotSwitch.setParameterName(rs.getString("parameter_name"));
01575 
01576                 //Double tmp = rs.getFloat("parameter_value_high");
01577         Double tmp = rs.getDouble("parameter_value_high");
01578         String stringTmp = rs.getString("parameter_value_high");
01579                   
01580                 if (rs.wasNull() || stringTmp.equals("null")) {
01581                     plotSwitch.setParameterValueHigh(Double.POSITIVE_INFINITY);
01582 
01583                 } else {
01584                     plotSwitch.setParameterValueHigh(tmp);
01585                 }
01586 
01587                 //tmp = rs.getFloat("parameter_value_low");
01588         tmp = rs.getDouble("parameter_value_low");
01589         stringTmp = rs.getString("parameter_value_low");
01590 
01591                 if (rs.wasNull()||stringTmp.equals("null")) {
01592                     plotSwitch.setParameterValueLow(Double.NEGATIVE_INFINITY);
01593 
01594                 } else {
01595                     plotSwitch.setParameterValueLow(tmp);
01596                 }
01597 
01598                 plotSwitch.setMCProcessTypeId(rs.getInt("process_id"));
01599 
01600                 plotSwitch.setPlotId(rs.getInt("csn_id"));
01601             }
01602 
01603             rs.close();
01604             stmt.close();
01605         } catch (SQLException e) {
01606             System.out.println("Error reading cross_section_switches table " + e);
01607             throw new JetWebDBException(e, queryString, "Switch Id :" + plotSwitch.getId());
01608         }
01609     }
01610 
01614     public static HashSet<PlotSwitch> getSwitches(int plotId) 
01615     throws JetWebException {
01616         HashSet<PlotSwitch> switches = new HashSet<PlotSwitch>();
01617 
01618         // Get all PlotSwitches for a cross section
01619         String queryString = "SELECT switch_id FROM cross_section_switches WHERE csn_id=" + plotId;
01620 
01621         try {
01622             Statement stmt = DBConfig.getConnection().createStatement();
01623             ResultSet rs = stmt.executeQuery(queryString);
01624 
01625             while (rs.next()) {
01626                 switches.add(new PlotSwitch(rs.getInt("switch_id")));
01627             }
01628 
01629             rs.close();
01630             stmt.close();
01631         } catch (SQLException e) {
01632             System.out.println("Error reading cross_section_switches table " + e);
01633             throw new JetWebDBException(e, queryString, "CrossSectionId:" + plotId);
01634         }
01635 
01636         return switches;
01637     }
01638 
01643     public static int getProcId(int runSeriesId) throws JetWebException {
01644         int procId;
01645 
01646         String selectString = "SELECT process_id from runseries WHERE runseries_id=" + runSeriesId;
01647 
01648         try {
01649             Statement stmt = DBConfig.getConnection().createStatement();
01650             ResultSet dbrs = stmt.executeQuery(selectString);
01651             dbrs.next();
01652             procId = dbrs.getInt("process_id");
01653             dbrs.close();
01654             stmt.close();
01655         } catch (SQLException E) {
01656             System.out.println("SQLException: " + E.getMessage());
01657             System.out.println("SQLState:     " + E.getSQLState());
01658             System.out.println("VendorError:  " + E.getErrorCode());
01659             throw new JetWebDBException(E, selectString, "");
01660         }
01661 
01662         return procId;
01663     }
01664 
01668     public static void update(PlotSwitch ps) throws JetWebException {
01669 
01670     // first delete it.
01671     String deleteIt = "DELETE FROM cross_section_switches WHERE switch_id='"
01672         +ps.getId()+"'";
01673 
01674         StringBuffer ud = new StringBuffer("INSERT INTO cross_section_switches SET process_id='" 
01675         + ps.getMCProcessTypeId());
01676 
01677         if (ps.getParameterName() != null) {
01678             ud.append("',parameter_name='" + ps.getParameterName());
01679 
01680         if (ps.getParameterValueHigh() != Double.POSITIVE_INFINITY) {
01681         ud.append("', parameter_value_high='" + ps.getParameterValueHigh());
01682         }
01683 
01684         if (ps.getParameterValueLow() != Double.NEGATIVE_INFINITY) {
01685         ud.append("', parameter_value_low='" + ps.getParameterValueLow());
01686         }
01687 
01688     }
01689     if (ps.getGeneratorName() != null) {
01690         ud.append("', generator_name='" + ps.getGeneratorName());
01691     }
01692 
01693     ud.append("', generator_version='" + ps.getGeneratorVersion() + "',switch_id='" + ps.getId());
01694     ud.append("', csn_id='"+ps.getPlotId()+"'");
01695 
01696 
01697     //System.out.println(ud.toString());
01698         try {
01699             Statement stmt = DBConfig.getConnection().createStatement();
01700             int rowsChanged = stmt.executeUpdate(deleteIt);
01701             rowsChanged = stmt.executeUpdate(ud.toString());
01702             stmt.close();
01703         } catch (SQLException E) {
01704             System.out.println("SQLException: " + E.getMessage());
01705             System.out.println("SQLState:     " + E.getSQLState());
01706             System.out.println("VendorError:  " + E.getErrorCode());
01707             throw new JetWebDBException(E, ud.toString(), "oh dear");
01708         }
01709     }
01710 
01711     public static void add(PlotSwitch ps) throws JetWebException {
01712         String ins1 = "Insert into cross_section_switches (csn_id, process_id, ";
01713         String ins2 = "values('" + ps.getPlotId() + "','" 
01714         + ps.getMCProcessTypeId() + "','";
01715 
01716         if (ps.getParameterName() != null) {
01717             ins1 = ins1 + " parameter_name,";
01718             ins2 = ins2 + ps.getParameterName() + "','";
01719         }
01720 
01721         if (ps.getParameterValueHigh() != Double.POSITIVE_INFINITY) {
01722             ins1 = ins1 + " parameter_value_high,";
01723             ins2 = ins2 + ps.getParameterValueHigh() + "','";
01724         }
01725 
01726         if (ps.getParameterValueLow() != Double.NEGATIVE_INFINITY) {
01727             ins1 = ins1 + " parameter_value_low,";
01728             ins2 = ins2 + ps.getParameterValueLow() + "','";
01729         }
01730 
01731         if (ps.getGeneratorName() != null) {
01732             ins1 = ins1 + " generator_name,";
01733             ins2 = ins2 + ps.getGeneratorName() + "','";
01734         }
01735 
01736         ins1 = ins1 + " generator_version)";
01737         ins2 = ins2 + ps.getGeneratorVersion() + "')";
01738 
01739         try {
01740             Statement stmt = DBConfig.getConnection().createStatement();
01741             int rowsChanged = stmt.executeUpdate(ins1 + ins2);
01742             stmt.close();
01743         } catch (SQLException E) {
01744             System.out.println("SQLException: " + E.getMessage());
01745             System.out.println("SQLState:     " + E.getSQLState());
01746             System.out.println("VendorError:  " + E.getErrorCode());
01747             throw new JetWebDBException(E, ins1 + ins2, "oh dear");
01748         }
01749     }
01750 
01757     public static synchronized void delete(PlotSwitch ps)
01758         throws JetWebException {
01759         String deleteString = "DELETE FROM cross_section_switches WHERE switch_id=" + ps.getId();
01760 
01761         try {
01762             Statement stmt = DBConfig.getConnection().createStatement();
01763             stmt.executeUpdate(deleteString);
01764             stmt.close();
01765         } catch (SQLException e) {
01766             System.out.println("Error deleting PlotSwitch");
01767             throw new JetWebDBException(e, deleteString, "PlotSwitchId:" + ps.getId());
01768         }
01769     }
01770 
01771 
01777     public static synchronized void delete(RealPlot plot)
01778         throws JetWebException {
01779 
01780     try {
01781         Connection con = DBConfig.getConnection();
01782         con.setAutoCommit(false);
01783             Statement stmt = con.createStatement();
01784             stmt.executeUpdate("DELETE FROM data_point WHERE csn_id="+plot.getId());
01785             stmt.executeUpdate("DELETE FROM predicted_point WHERE csn_id="+plot.getId());
01786             stmt.executeUpdate("DELETE FROM fitted_point WHERE csn_id="+plot.getId());
01787             stmt.executeUpdate("DELETE FROM cross_section_switches WHERE csn_id="+plot.getId());
01788             stmt.executeUpdate("DELETE FROM cross_section WHERE csn_id="+plot.getId());
01789             stmt.executeUpdate("DELETE FROM cross_section_set WHERE csn_id="+plot.getId());
01790             stmt.close();
01791         con.commit();
01792         } catch (SQLException e) {
01793             System.out.println("Error deleting plot");
01794             throw new JetWebDBException(e, "lots of SQL", "CsnId:" + plot.getId());
01795         }
01796     plot=null;
01797     }
01801     public static synchronized void delete(Paper paper)
01802         throws JetWebException {
01803 
01804     try {
01805         Connection con = DBConfig.getConnection();
01806             Statement stmt = con.createStatement();
01807             stmt.executeUpdate("DELETE FROM paper WHERE paper_id="+paper.getId());
01808             stmt.close();
01809         } catch (SQLException e) {
01810             System.out.println("Error deleting plot");
01811             throw new JetWebDBException(e, "lots of SQL", "PaperId:" + paper.getId());
01812         }
01813     paper=null;
01814     }
01815 
01824     public static Vector<PlotSwitch> getMatchedSwitches(int plotId, Generator gen)
01825         throws JetWebException {
01826         Vector<PlotSwitch> list = new Vector<PlotSwitch>();
01827 
01828         String selectString = 
01829         "SELECT switch_id from cross_section_switches where csn_id=" +
01830             plotId + 
01831         " and generator_name='" + gen.getName() + 
01832         "' and generator_version='" + gen.getVersion() + "'";
01833 
01834         try {
01835             Statement stmt = DBConfig.getConnection().createStatement();
01836             ResultSet dbrs = stmt.executeQuery(selectString);
01837 
01838             while (dbrs.next()) {
01839                 PlotSwitch ps = new PlotSwitch(dbrs.getInt("switch_id"));
01840                 list.add(ps);
01841             }
01842 
01843             dbrs.close();
01844             stmt.close();
01845 
01846             if (list.size() == 0) {
01847                 selectString = "SELECT switch_id from cross_section_switches where csn_id=" +
01848                     plotId + " and generator_name='" + gen.getName() + "'";
01849 
01850                 stmt = DBConfig.getConnection().createStatement();
01851                 dbrs = stmt.executeQuery(selectString);
01852 
01853                 while (dbrs.next()) {
01854                     PlotSwitch ps = new PlotSwitch(dbrs.getInt("switch_id"));
01855                     list.add(ps);
01856                 }
01857 
01858                 dbrs.close();
01859                 stmt.close();
01860             }
01861 
01862             if (list.size() == 0) {
01863                 selectString = "SELECT switch_id from cross_section_switches where csn_id=" +
01864                     plotId;
01865 
01866                 stmt = DBConfig.getConnection().createStatement();
01867                 dbrs = stmt.executeQuery(selectString);
01868 
01869                 while (dbrs.next()) {
01870                     PlotSwitch ps = new PlotSwitch(dbrs.getInt("switch_id"));
01871                     list.add(ps);
01872                 }
01873 
01874                 dbrs.close();
01875                 stmt.close();
01876             }
01877 
01878             return list;
01879         } catch (SQLException E) {
01880             System.out.println("SQLException: " + E.getMessage());
01881             System.out.println("SQLState:     " + E.getSQLState());
01882             System.out.println("VendorError:  " + E.getErrorCode());
01883             throw new JetWebDBException(E, selectString, "");
01884         }
01885     }
01886 }

Generated Wed Jan 17 09:14:27 GMT 2007