CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_8_patch3/src/OnlineDB/EcalCondDB/src/ODLTSConfig.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <cstdlib>
00003 #include <string>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005 
00006 #include "OnlineDB/EcalCondDB/interface/ODLTSConfig.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 ODLTSConfig::ODLTSConfig()
00012 {
00013   m_env = NULL;
00014   m_conn = NULL;
00015   m_writeStmt = NULL;
00016   m_readStmt = NULL;
00017   m_config_tag="";
00018   m_ID=0;
00019   clear();
00020 
00021 }
00022 
00023 void ODLTSConfig::clear(){
00024   m_trg_type="";
00025   m_num=0;
00026   m_rate=0;
00027   m_delay=0;
00028 }
00029 
00030 
00031 
00032 ODLTSConfig::~ODLTSConfig()
00033 {
00034 }
00035 
00036 void ODLTSConfig::setParameters(std::map<string,string> my_keys_map){
00037   
00038   // parses the result of the XML parser that is a map of 
00039   // string string with variable name variable value 
00040   
00041   for( std::map<std::string, std::string >::iterator ci=
00042          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00043 
00044     if(ci->first==  "LTS_CONFIGURATION_ID") setConfigTag(ci->second);
00045     if(ci->first==  "NUM_OF_EVENTS") setNumberOfEvents(atoi(ci->second.c_str()) );
00046     if(ci->first==  "RATE") setRate(atoi(ci->second.c_str() ));
00047     if(ci->first==  "TRIGGER_TYPE") setTriggerType(ci->second );
00048     if(ci->first==  "TRIG_LOC_L1_DELAY") setTrigLocL1Delay(atoi(ci->second.c_str() ));
00049   }
00050   
00051 }
00052 
00053 int ODLTSConfig::fetchNextId()  throw(std::runtime_error) {
00054 
00055   int result=0;
00056   try {
00057     this->checkConnection();
00058 
00059     m_readStmt = m_conn->createStatement(); 
00060     m_readStmt->setSQL("select ecal_lts_config_sq.NextVal from dual");
00061     ResultSet* rset = m_readStmt->executeQuery();
00062     while (rset->next ()){
00063       result= rset->getInt(1);
00064     }
00065     m_conn->terminateStatement(m_readStmt);
00066     return result; 
00067 
00068   } catch (SQLException &e) {
00069     throw(std::runtime_error("ODLTSConfig::fetchNextId():  "+e.getMessage()));
00070   }
00071 
00072 }
00073 
00074 
00075 void ODLTSConfig::prepareWrite()
00076   throw(std::runtime_error)
00077 {
00078   this->checkConnection();
00079   int next_id=fetchNextId();
00080 
00081   try {
00082     m_writeStmt = m_conn->createStatement();
00083     m_writeStmt->setSQL("INSERT INTO ECAL_LTS_CONFIGURATION ( lts_configuration_id, lts_tag, "
00084                         "trigger_type, num_of_events, rate, trig_loc_l1_delay ) "
00085                         "VALUES (  "
00086                         ":1, :2, :3, :4 , :5, :6 )");
00087     m_writeStmt->setInt(1, next_id);
00088     m_ID=next_id;
00089 
00090   } catch (SQLException &e) {
00091     throw(std::runtime_error("ODLTSConfig::prepareWrite():  "+e.getMessage()));
00092   }
00093 }
00094 
00095 
00096 
00097 void ODLTSConfig::writeDB()
00098   throw(std::runtime_error)
00099 {
00100   this->checkConnection();
00101   this->checkPrepare();
00102 
00103   try {
00104 
00105     m_writeStmt->setString(2, this->getConfigTag());
00106     m_writeStmt->setString(3, this->getTriggerType());
00107     m_writeStmt->setInt(4, this->getNumberOfEvents());
00108     m_writeStmt->setInt(5, this->getRate());
00109     m_writeStmt->setInt(6, this->getTrigLocL1Delay());
00110 
00111     m_writeStmt->executeUpdate();
00112 
00113 
00114   } catch (SQLException &e) {
00115     throw(std::runtime_error("ODLTSConfig::writeDB():  "+e.getMessage()));
00116   }
00117   // Now get the ID
00118   if (!this->fetchID()) {
00119     throw(std::runtime_error("ODLTSConfig::writeDB:  Failed to write"));
00120   }
00121 
00122 
00123 }
00124 
00125 
00126 
00127 void ODLTSConfig::fetchData(ODLTSConfig * result)
00128   throw(std::runtime_error)
00129 {
00130   this->checkConnection();
00131   result->clear();
00132   if(result->getId()==0 && (result->getConfigTag()=="") ){
00133     throw(std::runtime_error("ODLTSConfig::fetchData(): no Id defined for this ODLTSConfig "));
00134   }
00135 
00136   try {
00137 
00138     m_readStmt->setSQL("SELECT * "
00139                        "FROM ECAL_LTS_CONFIGURATION  "
00140                        " where ( lts_configuration_id = :1 or lts_tag=:2 ) " );
00141     m_readStmt->setInt(1, result->getId());
00142     m_readStmt->setString(2, result->getConfigTag());
00143     ResultSet* rset = m_readStmt->executeQuery();
00144 
00145     rset->next();
00146     // 1 is the id and 2 is the config tag
00147     result->setId(rset->getInt(1));
00148     result->setConfigTag(rset->getString(2));
00149 
00150     result->setTriggerType(        rset->getString(3) );
00151     result->setNumberOfEvents(     rset->getInt(4) );
00152     result->setRate(               rset->getInt(5) );
00153     result->setTrigLocL1Delay(     rset->getInt(6) );
00154   
00155 
00156   } catch (SQLException &e) {
00157     throw(std::runtime_error("ODLTSConfig::fetchData():  "+e.getMessage()));
00158   }
00159 }
00160 
00161 int ODLTSConfig::fetchID()    throw(std::runtime_error)
00162 {
00163   // Return from memory if available
00164   if (m_ID!=0) {
00165     return m_ID;
00166   }
00167 
00168   this->checkConnection();
00169 
00170   try {
00171     Statement* stmt = m_conn->createStatement();
00172     stmt->setSQL("SELECT lts_configuration_id FROM ecal_lts_configuration "
00173                  "WHERE  lts_tag=:lts_tag  " );
00174 
00175     stmt->setString(1, getConfigTag());
00176 
00177     ResultSet* rset = stmt->executeQuery();
00178 
00179     if (rset->next()) {
00180       m_ID = rset->getInt(1);
00181     } else {
00182       m_ID = 0;
00183     }
00184     m_conn->terminateStatement(stmt);
00185   } catch (SQLException &e) {
00186     throw(std::runtime_error("ODLTSConfig::fetchID:  "+e.getMessage()));
00187   }
00188 
00189   return m_ID;
00190 }