CMS 3D CMS Logo

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

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