CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/OnlineDB/EcalCondDB/src/ODRunConfigInfo.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include "OnlineDB/Oracle/interface/Oracle.h"
00003 
00004 #include "OnlineDB/EcalCondDB/interface/ODRunConfigInfo.h"
00005 #include "OnlineDB/EcalCondDB/interface/Tm.h"
00006 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 
00012 ODRunConfigInfo::ODRunConfigInfo()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_ID = 0;
00017   //
00018   m_tag ="";
00019   m_version=0;
00020   m_num_seq=0;
00021   m_runTypeDef = RunTypeDef();
00022   m_runModeDef = RunModeDef();
00023   m_defaults=0;
00024   m_trigger_mode=""; 
00025   m_num_events=0;
00026 }
00027 
00028 
00029 
00030 ODRunConfigInfo::~ODRunConfigInfo(){}
00031 
00032 
00033 
00034 //
00035 RunTypeDef ODRunConfigInfo::getRunTypeDef() const {  return m_runTypeDef;}
00036 void ODRunConfigInfo::setRunTypeDef(const RunTypeDef runTypeDef)
00037 {
00038   if (runTypeDef != m_runTypeDef) {
00039     m_ID = 0;
00040     m_runTypeDef = runTypeDef;
00041   }
00042 }
00043 //
00044 RunModeDef ODRunConfigInfo::getRunModeDef() const {  return m_runModeDef;}
00045 void ODRunConfigInfo::setRunModeDef(const RunModeDef runModeDef)
00046 {
00047   if (runModeDef != m_runModeDef) {
00048     m_ID = 0;
00049     m_runModeDef = runModeDef;
00050   }
00051 }
00052 //
00053 
00054 int ODRunConfigInfo::fetchNextId()  throw(std::runtime_error) {
00055 
00056   int result=0;
00057   try {
00058     this->checkConnection();
00059 
00060     m_readStmt = m_conn->createStatement(); 
00061     m_readStmt->setSQL("select ecal_run_sq.NextVal from dual");
00062     ResultSet* rset = m_readStmt->executeQuery();
00063     while (rset->next ()){
00064       result= rset->getInt(1);
00065     }
00066     m_conn->terminateStatement(m_readStmt);
00067     return result; 
00068 
00069   } catch (SQLException &e) {
00070     throw(std::runtime_error("ODDCCConfig::fetchNextId():  "+e.getMessage()));
00071   }
00072 
00073 }
00074 
00075 int ODRunConfigInfo::fetchID()
00076   throw(std::runtime_error)
00077 {
00078   // Return from memory if available
00079   if (m_ID>0) {
00080     return m_ID;
00081   }
00082 
00083   this->checkConnection();
00084 
00085 
00086   DateHandler dh(m_env, m_conn);
00087 
00088   try {
00089     Statement* stmt = m_conn->createStatement();
00090     stmt->setSQL("SELECT config_id from ECAL_RUN_CONFIGURATION_DAT "
00091                  "WHERE tag = :tag " 
00092                  " and version = :version " );
00093     stmt->setString(1, m_tag);
00094     stmt->setInt(2, m_version);
00095 
00096     ResultSet* rset = stmt->executeQuery();
00097     if (rset->next()) {
00098       m_ID = rset->getInt(1);
00099     } else {
00100       m_ID = 0;
00101     }
00102     m_conn->terminateStatement(stmt);
00103   } catch (SQLException &e) {
00104     throw(std::runtime_error("ODRunConfigInfo::fetchID:  "+e.getMessage()));
00105   }
00106   setByID(m_ID);
00107   return m_ID;
00108 }
00109 
00110 
00111 
00112 int ODRunConfigInfo::fetchIDLast()
00113   throw(std::runtime_error)
00114 {
00115 
00116   this->checkConnection();
00117 
00118   DateHandler dh(m_env, m_conn);
00119 
00120   try {
00121     Statement* stmt = m_conn->createStatement();
00122     stmt->setSQL("SELECT max(config_id) FROM ecal_run_configuration_dat "       );
00123     ResultSet* rset = stmt->executeQuery();
00124 
00125     if (rset->next()) {
00126       m_ID = rset->getInt(1);
00127     } else {
00128       m_ID = 0;
00129     }
00130     m_conn->terminateStatement(stmt);
00131   } catch (SQLException &e) {
00132     throw(std::runtime_error("ODRunConfigInfo::fetchIDLast:  "+e.getMessage()));
00133   }
00134 
00135   setByID(m_ID);
00136   return m_ID;
00137 }
00138 
00139 //
00140 int ODRunConfigInfo::fetchIDFromTagAndVersion()
00141   throw(std::runtime_error)
00142 {
00143   fetchID();
00144   return m_ID;
00145 }
00146 
00147 
00148 
00149 void ODRunConfigInfo::setByID(int id) 
00150   throw(std::runtime_error)
00151 {
00152    this->checkConnection();
00153 
00154    DateHandler dh(m_env, m_conn);
00155 
00156    try {
00157      Statement* stmt = m_conn->createStatement();
00158 
00159      stmt->setSQL("SELECT tag, version, run_type_def_id, run_mode_def_id, num_of_sequences, description, defaults,"
00160                   " trg_mode,num_of_events, db_timestamp, usage_status"
00161                   " FROM ECAL_RUN_CONFIGURATION_DAT WHERE config_id = :1");
00162      stmt->setInt(1, id);
00163      
00164      ResultSet* rset = stmt->executeQuery();
00165      if (rset->next()) {
00166        m_tag= rset->getString(1);
00167        m_version= rset->getInt(2);
00168        int run_type_id=rset->getInt(3);
00169        int run_mode_id=rset->getInt(4);
00170        m_num_seq=rset->getInt(5);
00171        m_description= rset->getString(6);
00172        m_defaults= rset->getInt(7);
00173        m_trigger_mode= rset->getString(8);
00174        m_num_events= rset->getInt(9);
00175        Date dbdate = rset->getDate(10);
00176        m_db_time = dh.dateToTm( dbdate );
00177        m_ID = id;
00178        m_runModeDef.setConnection(m_env, m_conn);
00179        m_runModeDef.setByID(run_mode_id);
00180        m_runTypeDef.setConnection(m_env, m_conn);
00181        m_runTypeDef.setByID(run_type_id);
00182        m_usage_status=rset->getString(11);
00183      } else {
00184        throw(std::runtime_error("ODRunConfigInfo::setByID:  Given config_id is not in the database"));
00185      }
00186      m_conn->terminateStatement(stmt);
00187    } catch (SQLException &e) {
00188      throw(std::runtime_error("ODRunConfigInfo::setByID:  "+e.getMessage()));
00189    }
00190 }
00191 
00192 void ODRunConfigInfo::prepareWrite()
00193   throw(std::runtime_error)
00194 {
00195   this->checkConnection();
00196 
00197   int next_id=fetchNextId();
00198 
00199 
00200 
00201 
00202   try {
00203     m_writeStmt = m_conn->createStatement();
00204     m_writeStmt->setSQL("INSERT INTO ECAL_RUN_CONFIGURATION_DAT (CONFIG_ID, tag, version, run_type_def_id, "
00205                  " run_mode_def_id, num_of_sequences, defaults, trg_mode, num_of_events, description, usage_status ) "
00206                  " VALUES (:1, :2, :3 , :4, :5, :6 ,:7, :8, :9, :10 , :11)");
00207 
00208     m_writeStmt->setInt(1, next_id);
00209     m_ID=next_id;
00210 
00211   } catch (SQLException &e) {
00212     throw(std::runtime_error("ODRunConfigInfo::prepareWrite():  "+e.getMessage()));
00213   }
00214 }
00215 
00216 
00217 void ODRunConfigInfo::writeDB()
00218   throw(std::runtime_error)
00219 {
00220   this->checkConnection();
00221   this->checkPrepare();
00222 
00223   // Validate the data, use infinity-till convention
00224   DateHandler dh(m_env, m_conn);
00225 
00226   try {
00227     
00228     // get the run mode
00229     m_runModeDef.setConnection(m_env, m_conn);
00230     int run_mode_id = m_runModeDef.fetchID();
00231 
00232     // get the run type
00233     m_runTypeDef.setConnection(m_env, m_conn);
00234     int run_type_id = m_runTypeDef.fetchID();
00235 
00236     // now insert 
00237 
00238     m_writeStmt->setString(2, this->getTag());
00239     m_writeStmt->setInt(3, this->getVersion());
00240     m_writeStmt->setInt(4, run_type_id);
00241     m_writeStmt->setInt(5, run_mode_id);
00242     m_writeStmt->setInt(6, this->getNumberOfSequences());
00243     m_writeStmt->setInt(7, this->getDefaults());
00244     m_writeStmt->setString(8, this->getTriggerMode());
00245     m_writeStmt->setInt(9, this->getNumberOfEvents());
00246     m_writeStmt->setString(10, this->getDescription());
00247     m_writeStmt->setString(11, this->getUsageStatus());
00248 
00249     m_writeStmt->executeUpdate();
00250 
00251   } catch (SQLException &e) {
00252     throw(std::runtime_error("ODRunConfigInfo::writeDB:  "+e.getMessage()));
00253   }
00254   // Now get the ID
00255   if (!this->fetchID()) {
00256     throw(std::runtime_error("ODRunConfigInfo::writeDB  Failed to write"));
00257   }
00258 
00259   this->setByID(m_ID);
00260 
00261   cout<< "ODRunConfigInfo::writeDB>> done inserting ODRunConfigInfo with id="<<m_ID<<endl;
00262 }
00263 
00264 
00265 int ODRunConfigInfo::updateDefaultCycle()
00266   throw(std::runtime_error)
00267 {
00268   this->checkConnection();
00269 
00270   // Check if this has already been written
00271   if(!this->fetchID()){
00272     this->writeDB();
00273   }
00274 
00275 
00276   try {
00277     Statement* stmt = m_conn->createStatement();
00278     
00279     stmt->setSQL("UPDATE ecal_run_configuration_dat set defaults=:1 where config_id=:2 " );
00280    
00281     stmt->setInt(1, m_defaults);
00282     stmt->setInt(2, m_ID);
00283 
00284     stmt->executeUpdate();
00285 
00286     m_conn->terminateStatement(stmt);
00287   } catch (SQLException &e) {
00288     throw(std::runtime_error("ODRunConfigInfo::writeDB:  "+e.getMessage()));
00289   }
00290   
00291   return m_ID;
00292 }
00293 
00294 void ODRunConfigInfo::clear(){
00295   m_num_seq = 0;
00296   m_runTypeDef = RunTypeDef();
00297   m_runModeDef = RunModeDef();
00298   m_defaults = 0;
00299   m_trigger_mode = "";
00300   m_num_events = 0;
00301 }
00302 
00303 void ODRunConfigInfo::fetchData(ODRunConfigInfo * result)
00304   throw(std::runtime_error)
00305 {
00306   this->checkConnection();
00307   DateHandler dh(m_env, m_conn);
00308   //  result->clear();
00309 
00310   if(result->getId()==0){
00311     //throw(std::runtime_error("FEConfigMainInfo::fetchData(): no Id defined for this FEConfigMainInfo "));
00312     result->fetchID();
00313   }
00314   try {
00315     m_readStmt->setSQL("SELECT config_id, tag, version, run_type_def_id, run_mode_def_id, \
00316       num_of_sequences, description, defaults, trg_mode, num_of_events, db_timestamp, usage_status \
00317       FROM ECAL_RUN_CONFIGURATION_DAT WHERE config_id = :1 ");
00318     m_readStmt->setInt(1, result->getId());
00319 
00320     ResultSet* rset = m_readStmt->executeQuery();
00321     rset->next();
00322 
00323     result->setId(               rset->getInt(1) );
00324     result->setTag(              rset->getString(2) );
00325     result->setVersion(          rset->getInt(3) );
00326     //    RunTypeDef myRunType = rset->getInt(4);
00327     //    result->setRunTypeDef( myRunType );
00328     //    RunModeDef myRunMode = rset->getInt(5);
00329     //    result->setRunModeDef( myRunMode );
00330     result->setNumberOfSequences(rset->getInt(6) );
00331     result->setDescription(      rset->getString(7) );
00332     result->setDefaults(         rset->getInt(8) );
00333     result->setTriggerMode(      rset->getString(9) );
00334     result->setNumberOfEvents(   rset->getInt(10) );
00335     Date dbdate = rset->getDate(11);
00336     result->setDBTime(dh.dateToTm( dbdate ));
00337     result->setUsageStatus(      rset->getString(12) );
00338 
00339   } catch (SQLException &e) {
00340     cout << " ODRunConfigInfo::fetchData():  " << e.getMessage() << endl;
00341     throw(std::runtime_error("ODRunConfigInfo::fetchData():  "+e.getMessage()));
00342   }
00343 }