CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/OnlineDB/EcalCondDB/src/ODTTCciConfig.cc

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