CMS 3D CMS Logo

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