CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/OnlineDB/EcalCondDB/src/ODTCCConfig.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/ODTCCConfig.h"
00007 
00008 using namespace std;
00009 using namespace oracle::occi;
00010 
00011 ODTCCConfig::ODTCCConfig()
00012 {
00013   m_env = NULL;
00014   m_conn = NULL;
00015   m_writeStmt = NULL;
00016   m_readStmt = NULL;
00017   m_config_tag="";
00018 
00019    m_ID=0;
00020    clear();
00021    m_size=0; 
00022 }
00023 
00024 void ODTCCConfig::clear(){
00025   
00026   m_tcc_file="";
00027   m_lut_file="";
00028   m_slb_file="";
00029   m_test_url="";
00030   m_ntest=0;
00031 }
00032 
00033 
00034 ODTCCConfig::~ODTCCConfig()
00035 {
00036 }
00037 
00038 int ODTCCConfig::fetchNextId()  throw(std::runtime_error) {
00039 
00040   int result=0;
00041   try {
00042     this->checkConnection();
00043 
00044     m_readStmt = m_conn->createStatement(); 
00045     m_readStmt->setSQL("select ecal_tcc_config_sq.NextVal from dual");
00046     ResultSet* rset = m_readStmt->executeQuery();
00047     while (rset->next ()){
00048       result= rset->getInt(1);
00049     }
00050     m_conn->terminateStatement(m_readStmt);
00051     return result; 
00052 
00053   } catch (SQLException &e) {
00054     throw(std::runtime_error("ODTCCConfig::fetchNextId():  "+e.getMessage()));
00055   }
00056 
00057 }
00058 
00059 
00060 void ODTCCConfig::setParameters(std::map<string,string> my_keys_map){
00061 
00062   // parses the result of the XML parser that is a map of
00063   // string string with variable name variable value
00064 
00065   for( std::map<std::string, std::string >::iterator ci=
00066          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00067 
00068     if(ci->first==  "TCC_CONFIGURATION_ID") setConfigTag(ci->second);
00069     if(ci->first==  "N_TESTPATTERNS_TO_LOAD") setNTestPatternsToLoad(atoi(ci->second.c_str()));
00070     if(ci->first==  "LUT_CONFIGURATION_FILE") setLUTConfigurationFile(ci->second );
00071     if(ci->first==  "CONFIGURATION_FILE") setTCCConfigurationFile(ci->second );
00072     if(ci->first==  "SLB_CONFIGURATION_FILE") setSLBConfigurationFile(ci->second );
00073     if(ci->first==  "TESTPATTERNFILE_URL") setTestPatternFileUrl(ci->second );
00074 
00075   }
00076 
00077 }
00078 
00079 
00080 
00081 void ODTCCConfig::prepareWrite()
00082   throw(std::runtime_error)
00083 {
00084   this->checkConnection();
00085 
00086   int next_id=fetchNextId();
00087 
00088   try {
00089     m_writeStmt = m_conn->createStatement();
00090     m_writeStmt->setSQL("INSERT INTO ECAL_TCC_CONFIGURATION (tcc_configuration_id, tcc_tag, "
00091                         "Configuration_file, LUT_CONFIGURATION_FILE, SLB_CONFIGURATION_FILE, "
00092                         "TESTPATTERNFILE_URL , N_TESTPATTERNS_TO_LOAD, "
00093                         "tcc_configuration, lut_configuration, slb_configuration ) "
00094                         "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 , :9, :10)");
00095     m_writeStmt->setInt(1, next_id);
00096     m_writeStmt->setString(2, getConfigTag());
00097     m_writeStmt->setString(3, getTCCConfigurationFile());
00098     m_writeStmt->setString(4, getLUTConfigurationFile());
00099     m_writeStmt->setString(5, getSLBConfigurationFile());
00100     m_writeStmt->setString(6, getTestPatternFileUrl());
00101     m_writeStmt->setInt(7, getNTestPatternsToLoad());
00102     // and now the clobs
00103     oracle::occi::Clob clob1(m_conn);
00104     clob1.setEmpty();
00105     m_writeStmt->setClob(8,clob1);
00106 
00107     oracle::occi::Clob clob2(m_conn);
00108     clob2.setEmpty();
00109     m_writeStmt->setClob(9,clob2);
00110 
00111     oracle::occi::Clob clob3(m_conn);
00112     clob3.setEmpty();
00113     m_writeStmt->setClob(10,clob3);
00114 
00115     m_writeStmt->executeUpdate ();
00116     m_ID=next_id; 
00117 
00118     m_conn->terminateStatement(m_writeStmt);
00119     std::cout<<"TCC 3 empty Clobs inserted into CONFIGURATION with id="<<next_id<<std::endl;
00120 
00121     // now we read and update it 
00122     m_writeStmt = m_conn->createStatement(); 
00123     m_writeStmt->setSQL ("SELECT tcc_configuration, lut_configuration, slb_configuration FROM ECAL_TCC_CONFIGURATION WHERE"
00124                          " tcc_configuration_id=:1 FOR UPDATE");
00125 
00126     std::cout<<"updating the clobs 0"<<std::endl;
00127 
00128     
00129   } catch (SQLException &e) {
00130     throw(std::runtime_error("ODTCCConfig::prepareWrite():  "+e.getMessage()));
00131   }
00132 
00133   std::cout<<"updating the clob 1 "<<std::endl;
00134   
00135 }
00136 
00137 void ODTCCConfig::writeDB()
00138   throw(std::runtime_error)
00139 {
00140 
00141   std::cout<<"updating the clob 2"<<std::endl;
00142 
00143   try {
00144 
00145     m_writeStmt->setInt(1, m_ID);
00146     ResultSet* rset = m_writeStmt->executeQuery();
00147 
00148     while (rset->next ())
00149       {
00150         oracle::occi::Clob clob1 = rset->getClob (1);
00151         oracle::occi::Clob clob2 = rset->getClob (2);
00152         oracle::occi::Clob clob3 = rset->getClob (3);
00153         cout << "Opening the clob in read write mode" << endl;
00154         cout << "Populating the clobs" << endl;
00155         populateClob (clob1, getTCCConfigurationFile(), m_size);
00156         populateClob (clob2, getLUTConfigurationFile(), m_size);
00157         populateClob (clob3, getSLBConfigurationFile(), m_size);
00158        
00159       }
00160 
00161     m_writeStmt->executeUpdate();
00162     m_writeStmt->closeResultSet (rset);
00163 
00164   } catch (SQLException &e) {
00165     throw(std::runtime_error("ODTCCConfig::writeDB():  "+e.getMessage()));
00166   }
00167   // Now get the ID
00168   if (!this->fetchID()) {
00169     throw(std::runtime_error("ODTCCConfig::writeDB:  Failed to write"));
00170   }
00171 
00172 
00173 }
00174 
00175 
00176 
00177 
00178 
00179 
00180 void ODTCCConfig::fetchData(ODTCCConfig * result)
00181   throw(std::runtime_error)
00182 {
00183   this->checkConnection();
00184   result->clear();
00185   if(result->getId()==0 && (result->getConfigTag()=="") ){
00186     throw(std::runtime_error("ODTCCConfig::fetchData(): no Id defined for this ODTCCConfig "));
00187   }
00188 
00189   try {
00190 
00191     m_readStmt->setSQL("SELECT * "
00192                        "FROM ECAL_TCC_CONFIGURATION d "
00193                        " where (tcc_configuration_id = :1 or tcc_tag=:2 )" );
00194     m_readStmt->setInt(1, result->getId());
00195     m_readStmt->setString(2, result->getConfigTag());
00196     ResultSet* rset = m_readStmt->executeQuery();
00197 
00198     rset->next();
00199     // the first is the id 
00200     result->setId(rset->getInt(1));
00201     result->setConfigTag(rset->getString(2));
00202 
00203     result->setTCCConfigurationFile(rset->getString(3));
00204     result->setLUTConfigurationFile(rset->getString(4));
00205     result->setSLBConfigurationFile(rset->getString(5));
00206     result->setTestPatternFileUrl(rset->getString(6));
00207     result->setNTestPatternsToLoad(rset->getInt(7));
00208     //
00209 
00210     Clob clob1 = rset->getClob (8);
00211     cout << "Opening the clob in Read only mode" << endl;
00212     clob1.open (OCCI_LOB_READONLY);
00213     int clobLength=clob1.length ();
00214     cout << "Length of the clob1 is: " << clobLength << endl;
00215     unsigned char* buffer = readClob (clob1, clobLength);
00216     clob1.close ();
00217     cout<< "the clob buffer is:"<<endl;  
00218     for (int i = 0; i < clobLength; ++i)
00219       cout << (char) buffer[i];
00220     cout << endl;
00221     result->setTCCClob(buffer );
00222 
00223     Clob clob2 = rset->getClob (9);
00224     cout << "Opening the clob in Read only mode" << endl;
00225     clob2.open (OCCI_LOB_READONLY);
00226     clobLength=clob2.length ();
00227     cout << "Length of the clob2 is: " << clobLength << endl;
00228     unsigned char* buffer2 = readClob (clob2, clobLength);
00229     clob2.close ();
00230     cout<< "the clob buffer is:"<<endl;  
00231     for (int i = 0; i < clobLength; ++i)
00232       cout << (char) buffer2[i];
00233     cout << endl;
00234     result->setLUTClob(buffer2 );
00235 
00236     Clob clob3 = rset->getClob (10);
00237     cout << "Opening the clob in Read only mode" << endl;
00238     clob3.open (OCCI_LOB_READONLY);
00239     clobLength=clob3.length ();
00240     cout << "Length of the clob3 is: " << clobLength << endl;
00241     unsigned char* buffer3 = readClob (clob3, clobLength);
00242     clob3.close ();
00243     cout<< "the clob buffer is:"<<endl;  
00244     for (int i = 0; i < clobLength; ++i)
00245       cout << (char) buffer3[i];
00246     cout << endl;
00247     result->setSLBClob(buffer3 );
00248 
00249   } catch (SQLException &e) {
00250     throw(std::runtime_error("ODTCCConfig::fetchData():  "+e.getMessage()));
00251   }
00252 }
00253 
00254 
00255 
00256 int ODTCCConfig::fetchID()    throw(std::runtime_error)
00257 {
00258   if (m_ID!=0) {
00259     return m_ID;
00260   }
00261 
00262   this->checkConnection();
00263 
00264   try {
00265     Statement* stmt = m_conn->createStatement();
00266     stmt->setSQL("SELECT tcc_configuration_id FROM ecal_tcc_configuration "
00267                  "WHERE  tcc_tag=:tcc_tag "
00268                  );
00269 
00270     stmt->setString(1, getConfigTag() );
00271 
00272     ResultSet* rset = stmt->executeQuery();
00273 
00274     if (rset->next()) {
00275       m_ID = rset->getInt(1);
00276     } else {
00277       m_ID = 0;
00278     }
00279     m_conn->terminateStatement(stmt);
00280   } catch (SQLException &e) {
00281     throw(std::runtime_error("ODTCCConfig::fetchID:  "+e.getMessage()));
00282   }
00283 
00284     return m_ID;
00285 }