00001 package cedar.jetweb.db; 00002 00003 import java.util.Vector; 00004 00005 import cedar.jetweb.JetWebConfig; 00006 import cedar.jetweb.JetWebException; 00007 import cedar.jetweb.model.Model; 00008 import cedar.jetweb.model.MCProcessType; 00009 import cedar.jetweb.job.CutCollection; 00010 import cedar.jetweb.model.fit.Fit; 00011 import cedar.jetweb.model.plots.PlotSelection; 00012 import cedar.jetweb.model.RunSeries; 00013 00014 import java.io.File; 00015 00016 import java.sql.*; 00017 00018 import java.util.Date; 00019 import java.util.HashMap; 00020 import java.util.Map; 00021 import java.util.Map.Entry; 00022 00030 public abstract class DBFitManager { 00034 public static final String SUCCESS = "Operation Completed Successfully"; 00035 00047 public static synchronized String selectFromDB(Fit fit) 00048 throws JetWebException { 00049 String query = ""; 00050 00051 try { 00052 if (fit.getId() >= 0) { 00053 query = "SELECT * FROM fit WHERE fit_id=" + fit.getId(); 00054 } else { 00055 query = "SELECT * FROM fit WHERE mdl_id=" + fit.getModel().getId() + 00056 " AND csn_set_id=" + fit.getPlotSelection().getId(); 00057 } 00058 } catch (Exception e) { 00059 throw new JetWebException(e,"Error getting model"); 00060 } 00061 00062 String gotit = ""; 00063 00064 try { 00065 Statement stmt = DBConfig.getConnection().createStatement(); 00066 ResultSet rs = stmt.executeQuery(query); 00067 00068 if (rs.next()) { 00069 gotit = fill(fit, rs); 00070 } 00071 00072 rs.close(); 00073 stmt.close(); 00074 } catch (SQLException e) { 00075 try { 00076 System.out.println("Error reading fit table " + e + "ModelId=" + 00077 fit.getModel().getId() + " CSSet=" + fit.getPlotSelection().getId()); 00078 throw new JetWebDBException(e, query, "FitId:" + fit.getId()); 00079 } catch (JetWebException je) { 00080 System.out.println("Error getting model " + je + "FitId:" + fit.getId()); 00081 throw je; 00082 } 00083 } 00084 00085 return gotit; 00086 } 00087 00091 private static String fill(Fit fit, ResultSet rs) throws JetWebException { 00092 String rtn = SUCCESS; 00093 00094 try { 00095 fit.setId(rs.getInt("fit_id")); 00096 Model model = new Model(rs.getInt("mdl_id")); 00097 //fit.setModel(model); 00098 fit.setModelId(model.getId()); 00099 00100 //need to get a list of runseries that generate data for this fit 00101 String select = 00102 "SELECT runseries_id FROM runseries_collection, model "+ 00103 "WHERE " + 00104 "runseries_collection.runseries_collection_id = " + 00105 "model.runseries_collection_id AND " + 00106 "model.mdl_id = " + model.getId(); 00107 00108 Statement stmt = DBConfig.getConnection().createStatement(); 00109 ResultSet runseriesRs = stmt.executeQuery(select); 00110 00111 HashMap<MCProcessType, CutCollection> processCuts = 00112 new HashMap<MCProcessType, CutCollection>(); 00113 00114 while(runseriesRs.next()){ 00115 Integer rsid = runseriesRs.getInt(1); 00116 00117 if(rsid!=null){ 00118 //RunSeries runseries = new RunSeries(rsid); 00119 RunSeries runseries = RunSeries.Maker(rsid); 00120 MCProcessType proc = runseries.getMCProcessType(); 00121 CutCollection cuts = runseries.getCutCollection(); 00122 00123 CutCollection existingCuts = 00124 processCuts.get(proc); 00125 00126 if(existingCuts==null || 00127 existingCuts.isWithin(cuts) 00128 ){ 00129 processCuts.put(proc, cuts); 00130 } 00131 00132 } 00133 00134 System.out.println("processCuts = " + processCuts); 00135 00136 } 00137 00138 PlotSelection selection = 00139 new PlotSelection(rs.getInt("csn_set_id")); 00140 00141 for(Map.Entry entry: processCuts.entrySet()){ 00142 selection.restrictCutCollection 00143 ((MCProcessType)entry.getKey(), 00144 (CutCollection)entry.getValue()); 00145 } 00146 00147 //fit.setPlotSelection(new PlotSelection(rs.getInt("csn_set_id"))); 00148 fit.setPlotSelection(selection); 00149 Date fdate = rs.getTimestamp("time"); 00150 fit.setDate(fdate); 00151 00152 if (!(model.getDate() == null)) { 00153 if (fdate.before(model.getDate())) { 00154 return "Fit is older than Model"; 00155 } 00156 } 00157 00158 fit.setScale(rs.getFloat("scalefactor")); 00159 00160 double[] tmpChi2 = { 0., 0. }; 00161 tmpChi2[0] = rs.getFloat("chi2"); 00162 tmpChi2[1] = rs.getFloat("dof"); 00163 fit.setChi2(tmpChi2); 00164 } catch (SQLException e) { 00165 System.out.println("Error reading fit table " + e + "ModelId=" + 00166 fit.getModel().getId() + " CSSet=" + fit.getPlotSelection().getId()); 00167 throw new JetWebDBException(e, null, 00168 "Error reading fit table " + "ModelId=" + fit.getModel().getId() + " CSSet=" + 00169 fit.getPlotSelection().getId()); 00170 } catch (JetWebException je) { 00171 System.out.println("Error getting model " + je); 00172 } 00173 00174 return rtn; 00175 } 00176 00183 public static synchronized void addFit(Fit fit) throws JetWebException { 00184 String insertFit = makeInsertFitString(fit); 00185 00186 try { 00187 Statement stmt = DBConfig.getConnection().createStatement(); 00188 stmt.executeUpdate(insertFit); 00189 stmt.close(); 00190 } catch (SQLException e) { 00191 try { 00192 System.out.println("Error inserting fit results " + e + "ModelId=" + 00193 fit.getModel().getId() + " CSSet=" + fit.getPlotSelection().getId()); 00194 throw new JetWebDBException(e, insertFit, 00195 "Error inserting fit results " + "ModelId=" + fit.getModel().getId() + 00196 " CSSet=" + fit.getPlotSelection().getId()); 00197 } catch (JetWebException je) { 00198 System.out.println("Error getting model " + je); 00199 throw je; 00200 } 00201 } 00202 00203 try { 00204 // Set the fitId in the Fit object 00205 fit.setId(selectFitId(fit.getModel().getId(), fit.getPlotSelection().getId())); 00206 } catch (JetWebException je) { 00207 System.out.println("Error getting model " + je); 00208 throw je; 00209 } 00210 } 00211 00216 private static int selectFitId(int mdlId, int cssId) 00217 throws JetWebException { 00218 int fitId = 0; 00219 String query = "SELECT fit_id FROM fit WHERE mdl_id=" + mdlId + " AND csn_set_id=" + cssId; 00220 00221 try { 00222 Statement stmt = DBConfig.getConnection().createStatement(); 00223 ResultSet rs = stmt.executeQuery(query); 00224 00225 if (rs.next()) { 00226 fitId = rs.getInt("fit_id"); 00227 } 00228 rs.close(); 00229 stmt.close(); 00230 } catch (SQLException e) { 00231 System.out.println("Error reading fit table " + e + "ModelId=" + mdlId + " CSSet=" + 00232 cssId); 00233 throw new JetWebDBException(e, query, 00234 "Error reading fit table " + "ModelId=" + mdlId + " CSSet=" + cssId); 00235 } 00236 00237 return fitId; 00238 } 00239 00246 public static synchronized void deleteAll(int fitId) 00247 throws JetWebException { 00248 String deleteString = ""; 00249 String deleteString1 = "DELETE FROM fitted_prediction WHERE fit_id=" + fitId; 00250 String deleteString2 = "DELETE FROM fitted_point WHERE fit_id=" + fitId; 00251 String deleteString3 = "DELETE FROM fit WHERE fit_id=" + fitId; 00252 00253 //System.out.println("Called Fit.fit()"); 00254 //JetWebException e2 = new JetWebException("help","help"); 00255 //e2.printStackTrace(); 00256 00257 00258 00259 try { 00260 Statement stmt = DBConfig.getConnection().createStatement(); 00261 deleteString = deleteString1; 00262 System.out.println(deleteString); 00263 System.out.println(stmt.executeUpdate(deleteString)); 00264 deleteString = deleteString2; 00265 System.out.println(deleteString); 00266 System.out.println(stmt.executeUpdate(deleteString)); 00267 deleteString = deleteString3; 00268 System.out.println(deleteString); 00269 System.out.println(stmt.executeUpdate(deleteString)); 00270 stmt.close(); 00271 } catch (SQLException e) { 00272 System.out.println("Error deleting fit results " + e + "FitId=" + fitId); 00273 throw new JetWebDBException(e, deleteString, "FitId:" + fitId); 00274 } 00275 } 00276 00277 private static String makeInsertFitString(Fit fit) throws JetWebException { 00278 String ins1 = "INSERT INTO fit ("; 00279 String ins2 = "VALUES("; 00280 00281 if (fit.getId() > 0) { 00282 ins1 = ins1 + "fit_id,"; 00283 ins2 = ins2 + fit.getId() + ","; 00284 } 00285 00286 ins1 = ins1 + "mdl_id,csn_set_id,scalefactor," + "chi2,dof)"; 00287 00288 fit.setDate(new Date()); 00289 00290 try { 00291 ins2 = ins2 + fit.getModel().getId() + "," + fit.getPlotSelection().getId() + "," + fit.getScale(); 00292 } catch (JetWebException je) { 00293 System.out.println("Error getting model " + je); 00294 } 00295 00296 ins2 = ins2 + "," + fit.getChi2()[0] + "," + fit.getChi2()[1] + ")"; 00297 00298 //System.out.println(ins1+ins2); 00299 return ins1 + ins2; 00300 } 00301 00305 public static void zeroFit(int fitId) { 00306 String zeroIt = "update fitted_point set y=0.0,dy_up=0.0,dy_down=0.0 where fit_id=" + 00307 fitId; 00308 00309 try { 00310 Statement stmt = DBConfig.getConnection().createStatement(); 00311 stmt.executeUpdate(zeroIt); 00312 stmt.close(); 00313 } catch (SQLException e) { 00314 System.out.println("DBFitManager: Error executing " + zeroIt); 00315 } 00316 00317 zeroIt = "update fitted_prediction set chi2=0.0,dof=0.0 where fit_id=" + fitId; 00318 00319 try { 00320 Statement stmt = DBConfig.getConnection().createStatement(); 00321 stmt.executeUpdate(zeroIt); 00322 stmt.close(); 00323 } catch (SQLException e) { 00324 System.out.println("DBFitManager: Error executing " + zeroIt); 00325 } 00326 } 00327 00331 public static synchronized Vector<Fit> select(Model model) 00332 throws JetWebException { 00333 00334 Vector<Fit> fits = new Vector<Fit>(); 00335 String query = ""; 00336 00337 try { 00338 query = "SELECT fit_id FROM fit WHERE mdl_id=" + model.getId(); 00339 } catch (Exception e) { 00340 throw new JetWebException(e,"Error getting model"); 00341 } 00342 00343 try { 00344 Statement stmt = DBConfig.getConnection().createStatement(); 00345 ResultSet rs = stmt.executeQuery(query); 00346 00347 while (rs.next()) { 00348 Fit fit = new Fit(rs.getInt("fit_id")); 00349 fits.add(fit); 00350 } 00351 rs.close(); 00352 stmt.close(); 00353 } catch (SQLException e) { 00354 throw new JetWebDBException(e, query, "ModelId:" + model.getId()); 00355 } 00356 00357 return fits; 00358 } 00359 00360 00361 }
Generated Wed Jan 17 09:14:27 GMT 2007