CMS 3D CMS Logo

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

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