CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/OnlineDB/EcalCondDB/src/ODTTCFConfig.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <cstdlib>
00004 
00005 #include "OnlineDB/Oracle/interface/Oracle.h"
00006 
00007 #include "OnlineDB/EcalCondDB/interface/ODTTCFConfig.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 ODTTCFConfig::ODTTCFConfig()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018   m_config_tag="";
00019   m_ID=0;
00020   clear();
00021   m_size=0;
00022 }
00023 
00024 
00025 void ODTTCFConfig::clear(){
00026 
00027 }
00028 
00029 
00030 ODTTCFConfig::~ODTTCFConfig()
00031 {
00032 }
00033 
00034 int ODTTCFConfig::fetchNextId()  throw(std::runtime_error) {
00035 
00036   int result=0;
00037   try {
00038     this->checkConnection();
00039     std::cout<< "going to fetch new id for TTCF 1"<<endl;
00040     m_readStmt = m_conn->createStatement(); 
00041     m_readStmt->setSQL("select ecal_ttcf_config_sq.NextVal from dual");
00042     ResultSet* rset = m_readStmt->executeQuery();
00043     while (rset->next ()){
00044       result= rset->getInt(1);
00045     }
00046     std::cout<< "id is : "<< result<<endl;
00047 
00048     m_conn->terminateStatement(m_readStmt);
00049     return result; 
00050 
00051   } catch (SQLException &e) {
00052     throw(std::runtime_error("ODTTCFConfig::fetchNextId():  "+e.getMessage()));
00053   }
00054 
00055 }
00056 
00057 
00058 
00059 
00060 void ODTTCFConfig::prepareWrite()
00061   throw(std::runtime_error)
00062 {
00063   this->checkConnection();
00064   int next_id=fetchNextId();
00065 
00066   try {
00067     m_writeStmt = m_conn->createStatement();
00068     m_writeStmt->setSQL("INSERT INTO ECAL_TTCF_CONFIGURATION (ttcf_configuration_id, ttcf_tag, " 
00069                         " rxbc0_delay, reg_30 , ttcf_configuration_file , ttcf_configuration ) "
00070                         "VALUES (:1, :2, :3 , :4, :5, :6)");
00071     m_writeStmt->setInt(1, next_id);
00072     m_writeStmt->setString(2, getConfigTag());
00073 
00074     m_writeStmt->setInt( 3, getRxBC0Delay() );
00075     m_writeStmt->setInt( 4, getReg30() );
00076     
00077     m_writeStmt->setString(5, getTTCFConfigurationFile());
00078     
00079     oracle::occi::Clob clob(m_conn);
00080     clob.setEmpty();
00081     m_writeStmt->setClob(6,clob);
00082     m_writeStmt->executeUpdate ();
00083     m_ID=next_id; 
00084 
00085     m_conn->terminateStatement(m_writeStmt);
00086     std::cout<<"inserted into CONFIGURATION with id="<<next_id<<std::endl;
00087 
00088     // now we read and update it 
00089     m_writeStmt = m_conn->createStatement(); 
00090     m_writeStmt->setSQL ("SELECT ttcf_configuration FROM ECAL_TTCF_CONFIGURATION WHERE"
00091                          " ttcf_configuration_id=:1 FOR UPDATE");
00092 
00093   std::cout<<"updating the clob 0"<<std::endl;
00094 
00095 
00096   } catch (SQLException &e) {
00097     throw(std::runtime_error("ODTTCFConfig::prepareWrite():  "+e.getMessage()));
00098   }
00099 
00100   std::cout<<"updating the clob 1 "<<std::endl;
00101 
00102 }
00103 
00104 void ODTTCFConfig::writeDB()
00105   throw(std::runtime_error)
00106 {
00107 
00108   std::cout<<"updating the clob 2"<<std::endl;
00109 
00110   try {
00111     m_writeStmt->setInt(1, m_ID);
00112     ResultSet* rset = m_writeStmt->executeQuery();
00113 
00114     rset->next ();
00115 
00116     oracle::occi::Clob clob = rset->getClob (1);
00117     cout << "Opening the clob in read write mode" << endl;
00118     populateClob (clob, getTTCFConfigurationFile(), m_size);
00119     int clobLength=clob.length ();
00120     cout << "Length of the clob is: " << clobLength << endl;
00121 
00122     m_writeStmt->executeUpdate();
00123     m_writeStmt->closeResultSet (rset);
00124 
00125   } catch (SQLException &e) {
00126     throw(std::runtime_error("ODTTCFConfig::writeDB():  "+e.getMessage()));
00127   }
00128   // Now get the ID
00129   if (!this->fetchID()) {
00130     throw(std::runtime_error("ODTTCFConfig::writeDB:  Failed to write"));
00131   }
00132 
00133 
00134 }
00135 
00136 
00137 
00138 
00139 void ODTTCFConfig::fetchData(ODTTCFConfig * result)
00140   throw(std::runtime_error)
00141 {
00142   this->checkConnection();
00143   result->clear();
00144 
00145   if(result->getId()==0 && (result->getConfigTag()=="") ){
00146     throw(std::runtime_error("ODTTCFConfig::fetchData(): no Id defined for this ODTTCFConfig "));
00147   }
00148 
00149   try {
00150 
00151     m_readStmt->setSQL("SELECT *   "
00152                        "FROM ECAL_TTCF_CONFIGURATION  "
00153                        " where (ttcf_configuration_id = :1 or ttcf_tag= :2) " );
00154     m_readStmt->setInt(1, result->getId());
00155     m_readStmt->setString(2, result->getConfigTag());
00156     ResultSet* rset = m_readStmt->executeQuery();
00157 
00158     rset->next();
00159 
00160     result->setId(rset->getInt(1));
00161     result->setConfigTag(rset->getString(2));
00162     result->setTTCFConfigurationFile(rset->getString(3));
00163     Clob clob = rset->getClob (4);
00164     cout << "Opening the clob in Read only mode" << endl;
00165     clob.open (OCCI_LOB_READONLY);
00166     int clobLength=clob.length ();
00167     cout << "Length of the clob is: " << clobLength << endl;
00168     m_size=clobLength;
00169     unsigned char* buffer = readClob (clob, m_size);
00170     clob.close ();
00171     result->setTTCFClob((unsigned char*) buffer );
00172 
00173   } catch (SQLException &e) {
00174     throw(std::runtime_error("ODTTCFConfig::fetchData():  "+e.getMessage()));
00175   }
00176 }
00177 
00178 
00179 
00180 int ODTTCFConfig::fetchID()    throw(std::runtime_error)
00181 {
00182   if (m_ID!=0) {
00183     return m_ID;
00184   }
00185 
00186   this->checkConnection();
00187 
00188   try {
00189     Statement* stmt = m_conn->createStatement();
00190     stmt->setSQL("SELECT ttcf_configuration_id FROM ecal_ttcf_configuration "
00191                  "WHERE  ttcf_tag=:ttcf_tag "
00192                  );
00193 
00194     stmt->setString(1, getConfigTag() );
00195 
00196     ResultSet* rset = stmt->executeQuery();
00197 
00198     if (rset->next()) {
00199       m_ID = rset->getInt(1);
00200     } else {
00201       m_ID = 0;
00202     }
00203     m_conn->terminateStatement(stmt);
00204   } catch (SQLException &e) {
00205     throw(std::runtime_error("ODTTCFConfig::fetchID:  "+e.getMessage()));
00206   }
00207 
00208     return m_ID;
00209 }
00210 
00211 void ODTTCFConfig::setParameters(std::map<string,string> my_keys_map){
00212 
00213   // parses the result of the XML parser that is a map of
00214   // string string with variable name variable value
00215 
00216   for( std::map<std::string, std::string >::iterator ci=
00217          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00218 
00219     if(ci->first==  "TTCF_CONFIGURATION_ID") setConfigTag(ci->second);
00220     if(ci->first==  "Configuration") {
00221       std::string fname=ci->second ;
00222       string  str3;
00223       size_t pos, pose;
00224 
00225       pos = fname.find("=");    // position of "live" in str
00226       pose = fname.size();    // position of "]" in str
00227       str3 = fname.substr (pos+1, pose-pos-2);
00228 
00229       cout << "fname="<<fname<< " and reduced is: "<<str3 << endl;
00230       setTTCFConfigurationFile(str3 );
00231 
00232 
00233       // here we must open the file and read the LTC Clob
00234       std::cout << "Going to read file: " << str3 << endl;
00235 
00236       ifstream inpFile;
00237       inpFile.open(str3.c_str());
00238 
00239       // tell me size of file
00240       int bufsize = 0;
00241       inpFile.seekg( 0,ios::end );
00242       bufsize = inpFile.tellg();
00243       std::cout <<" bufsize ="<<bufsize<< std::endl;
00244       // set file pointer to start again
00245       inpFile.seekg( 0,ios::beg );
00246 
00247       inpFile.close();
00248       m_size=bufsize;
00249 
00250     } else if ( ci->first == "RXBC0_DELAY" ) {
00251       setRxBC0Delay( atoi( ci->second.c_str() ) );
00252     } else if ( ci->first == "REG_30" ) {
00253       setReg30( atoi( ci->second.c_str() ) );
00254     } 
00255   }
00256 
00257 }
00258