Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:08

0001 #include <cstdlib>
0002 #include <stdexcept>
0003 #include <string>
0004 #include "OnlineDB/Oracle/interface/Oracle.h"
0005 #include <algorithm>
0006 #include <cctype>
0007 
0008 #include "OnlineDB/EcalCondDB/interface/ODSRPConfig.h"
0009 
0010 using namespace std;
0011 using namespace oracle::occi;
0012 
0013 ODSRPConfig::ODSRPConfig() {
0014   m_env = nullptr;
0015   m_conn = nullptr;
0016   m_writeStmt = nullptr;
0017   m_readStmt = nullptr;
0018   m_config_tag = "";
0019 
0020   m_ID = 0;
0021   clear();
0022   m_size = 0;
0023 }
0024 
0025 void ODSRPConfig::clear() {
0026   //  strcpy((char *)m_srp_clob, "");
0027   m_debug = 0;
0028   m_dummy = 0;
0029   m_file = "";
0030   m_patdir = "";
0031   m_auto = 0;
0032   m_bnch = 0;
0033 }
0034 
0035 ODSRPConfig::~ODSRPConfig() {}
0036 
0037 int ODSRPConfig::fetchNextId() noexcept(false) {
0038   int result = 0;
0039   try {
0040     this->checkConnection();
0041 
0042     m_readStmt = m_conn->createStatement();
0043     m_readStmt->setSQL("select ecal_srp_config_sq.NextVal from dual");
0044     ResultSet *rset = m_readStmt->executeQuery();
0045     while (rset->next()) {
0046       result = rset->getInt(1);
0047     }
0048     m_conn->terminateStatement(m_readStmt);
0049     return result;
0050 
0051   } catch (SQLException &e) {
0052     throw(std::runtime_error(std::string("ODSRPConfig::fetchNextId():  ") + e.getMessage()));
0053   }
0054 }
0055 
0056 void ODSRPConfig::setParameters(const std::map<string, string> &my_keys_map) {
0057   // parses the result of the XML parser that is a map of
0058   // string string with variable name variable value
0059 
0060   for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
0061     std::string name = ci->first;
0062     std::transform(name.begin(), name.end(), name.begin(), (int (*)(int))std::toupper);
0063 
0064     if (name == "SRP_CONFIGURATION_ID")
0065       setConfigTag(ci->second);
0066     if (name == "DEBUGMODE")
0067       setDebugMode(atoi(ci->second.c_str()));
0068     if (name == "DUMMYMODE")
0069       setDummyMode(atoi(ci->second.c_str()));
0070     if (name == "PATTERNDIRECTORY")
0071       setPatternDirectory(ci->second);
0072     if (name == "PATTERN_DIRECTORY")
0073       setPatternDirectory(ci->second);
0074     if (name == "AUTOMATICMASKS")
0075       setAutomaticMasks(atoi(ci->second.c_str()));
0076     if (name == "AUTOMATIC_MASKS")
0077       setAutomaticMasks(atoi(ci->second.c_str()));
0078     if (name == "AUTOMATICSRPSELECT")
0079       setAutomaticSrpSelect(atoi(ci->second.c_str()));
0080     if (name == "SRP0BUNCHADJUSTPOSITION")
0081       setSRP0BunchAdjustPosition(atoi(ci->second.c_str()));
0082     if (name == "SRP_CONFIG_FILE") {
0083       std::string fname = ci->second;
0084 
0085       cout << "fname=" << fname << endl;
0086       setConfigFile(fname);
0087 
0088       // here we must open the file and read the LTC Clob
0089       std::cout << "Going to read SRP file: " << fname << endl;
0090 
0091       ifstream inpFile;
0092       inpFile.open(fname.c_str());
0093 
0094       // tell me size of file
0095       int bufsize = 0;
0096       inpFile.seekg(0, ios::end);
0097       bufsize = inpFile.tellg();
0098       std::cout << " bufsize =" << bufsize << std::endl;
0099       // set file pointer to start again
0100       inpFile.seekg(0, ios::beg);
0101 
0102       m_size = bufsize;
0103 
0104       inpFile.close();
0105     }
0106   }
0107 }
0108 
0109 void ODSRPConfig::prepareWrite() noexcept(false) {
0110   this->checkConnection();
0111 
0112   int next_id = fetchNextId();
0113 
0114   try {
0115     m_writeStmt = m_conn->createStatement();
0116     m_writeStmt->setSQL(
0117         "INSERT INTO ECAL_SRP_CONFIGURATION (srp_configuration_id, srp_tag, "
0118         "   DEBUGMODE, DUMMYMODE, PATTERN_DIRECTORY, AUTOMATIC_MASKS,"
0119         " SRP0BUNCHADJUSTPOSITION, SRP_CONFIG_FILE, SRP_CONFIGURATION,  AUTOMATICSRPSELECT  ) "
0120         "VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10 )");
0121     m_writeStmt->setInt(1, next_id);
0122     m_writeStmt->setString(2, getConfigTag());
0123     m_writeStmt->setInt(3, getDebugMode());
0124     m_writeStmt->setInt(4, getDummyMode());
0125     m_writeStmt->setString(5, getPatternDirectory());
0126     m_writeStmt->setInt(6, getAutomaticMasks());
0127     m_writeStmt->setInt(10, getAutomaticSrpSelect());
0128     m_writeStmt->setInt(7, getSRP0BunchAdjustPosition());
0129     m_writeStmt->setString(8, getConfigFile());
0130 
0131     // and now the clob
0132     oracle::occi::Clob clob(m_conn);
0133     clob.setEmpty();
0134     m_writeStmt->setClob(9, clob);
0135     m_writeStmt->executeUpdate();
0136     m_ID = next_id;
0137 
0138     m_conn->terminateStatement(m_writeStmt);
0139     std::cout << "SRP Clob inserted into CONFIGURATION with id=" << next_id << std::endl;
0140 
0141     // now we read and update it
0142     m_writeStmt = m_conn->createStatement();
0143     m_writeStmt->setSQL(
0144         "SELECT srp_configuration FROM ECAL_SRP_CONFIGURATION WHERE"
0145         " srp_configuration_id=:1 FOR UPDATE");
0146 
0147     std::cout << "updating the clob 0" << std::endl;
0148 
0149   } catch (SQLException &e) {
0150     throw(std::runtime_error(std::string("ODSRPConfig::prepareWrite():  ") + e.getMessage()));
0151   }
0152 
0153   std::cout << "updating the clob 1 " << std::endl;
0154 }
0155 
0156 void ODSRPConfig::writeDB() noexcept(false) {
0157   std::cout << "updating the clob 2" << std::endl;
0158 
0159   try {
0160     m_writeStmt->setInt(1, m_ID);
0161     ResultSet *rset = m_writeStmt->executeQuery();
0162 
0163     while (rset->next()) {
0164       oracle::occi::Clob clob = rset->getClob(1);
0165       cout << "Opening the clob in read write mode" << endl;
0166       cout << "Populating the clob" << endl;
0167       populateClob(clob, getConfigFile(), m_size);
0168       int clobLength = clob.length();
0169       cout << "Length of the clob after writing is: " << clobLength << endl;
0170     }
0171 
0172     m_writeStmt->executeUpdate();
0173 
0174     m_writeStmt->closeResultSet(rset);
0175 
0176   } catch (SQLException &e) {
0177     throw(std::runtime_error(std::string("ODSRPConfig::writeDB():  ") + e.getMessage()));
0178   }
0179   // Now get the ID
0180   if (!this->fetchID()) {
0181     throw(std::runtime_error("ODSRPConfig::writeDB:  Failed to write"));
0182   }
0183 }
0184 
0185 void ODSRPConfig::fetchData(ODSRPConfig *result) noexcept(false) {
0186   this->checkConnection();
0187   //  result->clear();
0188   if (result->getId() == 0 && (result->getConfigTag().empty())) {
0189     //    throw(std::runtime_error("ODSRPConfig::fetchData(): no Id defined for this ODSRPConfig "));
0190     result->fetchID();
0191   }
0192 
0193   try {
0194     m_readStmt->setSQL(
0195         "SELECT  * "
0196         " FROM ECAL_SRP_CONFIGURATION  "
0197         " where (srp_configuration_id = :1 or srp_tag=:2 )");
0198     m_readStmt->setInt(1, result->getId());
0199     m_readStmt->setString(2, result->getConfigTag());
0200     ResultSet *rset = m_readStmt->executeQuery();
0201 
0202     rset->next();
0203     // 1 is the id and 2 is the config tag
0204 
0205     result->setId(rset->getInt(1));
0206     result->setConfigTag(rset->getString(2));
0207 
0208     result->setDebugMode(rset->getInt(3));
0209     result->setDummyMode(rset->getInt(4));
0210     result->setPatternDirectory(rset->getString(5));
0211     result->setAutomaticMasks(rset->getInt(6));
0212     result->setSRP0BunchAdjustPosition(rset->getInt(7));
0213     result->setConfigFile(rset->getString(8));
0214 
0215     Clob clob = rset->getClob(9);
0216     m_size = clob.length();
0217     Stream *instream = clob.getStream(1, 0);
0218     unsigned char *buffer = new unsigned char[m_size];
0219     memset(buffer, 0, m_size);
0220     instream->readBuffer((char *)buffer, m_size);
0221     /*
0222     cout << "Opening the clob in Read only mode" << endl;
0223     clob.open (OCCI_LOB_READONLY);
0224     int clobLength=clob.length ();
0225     cout << "Length of the clob is: " << clobLength << endl;
0226     unsigned char* buffer = readClob (clob, clobLength);
0227     clob.close ();
0228     cout<< "the clob buffer is:"<<endl;  
0229     for (int i = 0; i < clobLength; ++i)
0230       cout << (char) buffer[i];
0231     cout << endl;
0232 
0233 
0234     */
0235     result->setSRPClob(buffer);
0236     result->setAutomaticSrpSelect(rset->getInt(10));
0237 
0238   } catch (SQLException &e) {
0239     throw(std::runtime_error(std::string("ODSRPConfig::fetchData():  ") + e.getMessage()));
0240   }
0241 }
0242 
0243 int ODSRPConfig::fetchID() noexcept(false) {
0244   // Return from memory if available
0245   if (m_ID != 0) {
0246     return m_ID;
0247   }
0248 
0249   this->checkConnection();
0250 
0251   try {
0252     Statement *stmt = m_conn->createStatement();
0253     stmt->setSQL(
0254         "SELECT srp_configuration_id FROM ecal_srp_configuration "
0255         "WHERE  srp_tag=:srp_tag ");
0256 
0257     stmt->setString(1, getConfigTag());
0258 
0259     ResultSet *rset = stmt->executeQuery();
0260 
0261     if (rset->next()) {
0262       m_ID = rset->getInt(1);
0263     } else {
0264       m_ID = 0;
0265     }
0266     m_conn->terminateStatement(stmt);
0267   } catch (SQLException &e) {
0268     throw(std::runtime_error(std::string("ODSRPConfig::fetchID:  ") + e.getMessage()));
0269   }
0270 
0271   return m_ID;
0272 }