CMS 3D CMS Logo

ODSRPConfig.cc
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <stdexcept>
3 #include <string>
5 #include <algorithm>
6 #include <cctype>
7 
9 
10 using namespace std;
11 using namespace oracle::occi;
12 
14  m_env = nullptr;
15  m_conn = nullptr;
16  m_writeStmt = nullptr;
17  m_readStmt = nullptr;
18  m_config_tag = "";
19 
20  m_ID = 0;
21  clear();
22  m_size = 0;
23 }
24 
26  // strcpy((char *)m_srp_clob, "");
27  m_debug = 0;
28  m_dummy = 0;
29  m_file = "";
30  m_patdir = "";
31  m_auto = 0;
32  m_bnch = 0;
33 }
34 
36 
38  int result = 0;
39  try {
40  this->checkConnection();
41 
42  m_readStmt = m_conn->createStatement();
43  m_readStmt->setSQL("select ecal_srp_config_sq.NextVal from dual");
44  ResultSet *rset = m_readStmt->executeQuery();
45  while (rset->next()) {
46  result = rset->getInt(1);
47  }
48  m_conn->terminateStatement(m_readStmt);
49  return result;
50 
51  } catch (SQLException &e) {
52  throw(std::runtime_error(std::string("ODSRPConfig::fetchNextId(): ") + e.getMessage()));
53  }
54 }
55 
56 void ODSRPConfig::setParameters(const std::map<string, string> &my_keys_map) {
57  // parses the result of the XML parser that is a map of
58  // string string with variable name variable value
59 
60  for (std::map<std::string, std::string>::const_iterator ci = my_keys_map.begin(); ci != my_keys_map.end(); ci++) {
61  std::string name = ci->first;
62  std::transform(name.begin(), name.end(), name.begin(), (int (*)(int))std::toupper);
63 
64  if (name == "SRP_CONFIGURATION_ID")
65  setConfigTag(ci->second);
66  if (name == "DEBUGMODE")
67  setDebugMode(atoi(ci->second.c_str()));
68  if (name == "DUMMYMODE")
69  setDummyMode(atoi(ci->second.c_str()));
70  if (name == "PATTERNDIRECTORY")
71  setPatternDirectory(ci->second);
72  if (name == "PATTERN_DIRECTORY")
73  setPatternDirectory(ci->second);
74  if (name == "AUTOMATICMASKS")
75  setAutomaticMasks(atoi(ci->second.c_str()));
76  if (name == "AUTOMATIC_MASKS")
77  setAutomaticMasks(atoi(ci->second.c_str()));
78  if (name == "AUTOMATICSRPSELECT")
79  setAutomaticSrpSelect(atoi(ci->second.c_str()));
80  if (name == "SRP0BUNCHADJUSTPOSITION")
81  setSRP0BunchAdjustPosition(atoi(ci->second.c_str()));
82  if (name == "SRP_CONFIG_FILE") {
83  std::string fname = ci->second;
84 
85  cout << "fname=" << fname << endl;
86  setConfigFile(fname);
87 
88  // here we must open the file and read the LTC Clob
89  std::cout << "Going to read SRP file: " << fname << endl;
90 
91  ifstream inpFile;
92  inpFile.open(fname.c_str());
93 
94  // tell me size of file
95  int bufsize = 0;
96  inpFile.seekg(0, ios::end);
97  bufsize = inpFile.tellg();
98  std::cout << " bufsize =" << bufsize << std::endl;
99  // set file pointer to start again
100  inpFile.seekg(0, ios::beg);
101 
102  m_size = bufsize;
103 
104  inpFile.close();
105  }
106  }
107 }
108 
110  this->checkConnection();
111 
112  int next_id = fetchNextId();
113 
114  try {
115  m_writeStmt = m_conn->createStatement();
116  m_writeStmt->setSQL(
117  "INSERT INTO ECAL_SRP_CONFIGURATION (srp_configuration_id, srp_tag, "
118  " DEBUGMODE, DUMMYMODE, PATTERN_DIRECTORY, AUTOMATIC_MASKS,"
119  " SRP0BUNCHADJUSTPOSITION, SRP_CONFIG_FILE, SRP_CONFIGURATION, AUTOMATICSRPSELECT ) "
120  "VALUES (:1, :2, :3, :4, :5, :6, :7, :8, :9, :10 )");
121  m_writeStmt->setInt(1, next_id);
122  m_writeStmt->setString(2, getConfigTag());
123  m_writeStmt->setInt(3, getDebugMode());
124  m_writeStmt->setInt(4, getDummyMode());
125  m_writeStmt->setString(5, getPatternDirectory());
126  m_writeStmt->setInt(6, getAutomaticMasks());
127  m_writeStmt->setInt(10, getAutomaticSrpSelect());
128  m_writeStmt->setInt(7, getSRP0BunchAdjustPosition());
129  m_writeStmt->setString(8, getConfigFile());
130 
131  // and now the clob
132  oracle::occi::Clob clob(m_conn);
133  clob.setEmpty();
134  m_writeStmt->setClob(9, clob);
135  m_writeStmt->executeUpdate();
136  m_ID = next_id;
137 
138  m_conn->terminateStatement(m_writeStmt);
139  std::cout << "SRP Clob inserted into CONFIGURATION with id=" << next_id << std::endl;
140 
141  // now we read and update it
142  m_writeStmt = m_conn->createStatement();
143  m_writeStmt->setSQL(
144  "SELECT srp_configuration FROM ECAL_SRP_CONFIGURATION WHERE"
145  " srp_configuration_id=:1 FOR UPDATE");
146 
147  std::cout << "updating the clob 0" << std::endl;
148 
149  } catch (SQLException &e) {
150  throw(std::runtime_error(std::string("ODSRPConfig::prepareWrite(): ") + e.getMessage()));
151  }
152 
153  std::cout << "updating the clob 1 " << std::endl;
154 }
155 
156 void ODSRPConfig::writeDB() noexcept(false) {
157  std::cout << "updating the clob 2" << std::endl;
158 
159  try {
160  m_writeStmt->setInt(1, m_ID);
161  ResultSet *rset = m_writeStmt->executeQuery();
162 
163  while (rset->next()) {
164  oracle::occi::Clob clob = rset->getClob(1);
165  cout << "Opening the clob in read write mode" << endl;
166  cout << "Populating the clob" << endl;
167  populateClob(clob, getConfigFile(), m_size);
168  int clobLength = clob.length();
169  cout << "Length of the clob after writing is: " << clobLength << endl;
170  }
171 
172  m_writeStmt->executeUpdate();
173 
174  m_writeStmt->closeResultSet(rset);
175 
176  } catch (SQLException &e) {
177  throw(std::runtime_error(std::string("ODSRPConfig::writeDB(): ") + e.getMessage()));
178  }
179  // Now get the ID
180  if (!this->fetchID()) {
181  throw(std::runtime_error("ODSRPConfig::writeDB: Failed to write"));
182  }
183 }
184 
185 void ODSRPConfig::fetchData(ODSRPConfig *result) noexcept(false) {
186  this->checkConnection();
187  // result->clear();
188  if (result->getId() == 0 && (result->getConfigTag().empty())) {
189  // throw(std::runtime_error("ODSRPConfig::fetchData(): no Id defined for this ODSRPConfig "));
190  result->fetchID();
191  }
192 
193  try {
194  m_readStmt->setSQL(
195  "SELECT * "
196  " FROM ECAL_SRP_CONFIGURATION "
197  " where (srp_configuration_id = :1 or srp_tag=:2 )");
198  m_readStmt->setInt(1, result->getId());
199  m_readStmt->setString(2, result->getConfigTag());
200  ResultSet *rset = m_readStmt->executeQuery();
201 
202  rset->next();
203  // 1 is the id and 2 is the config tag
204 
205  result->setId(rset->getInt(1));
206  result->setConfigTag(rset->getString(2));
207 
208  result->setDebugMode(rset->getInt(3));
209  result->setDummyMode(rset->getInt(4));
210  result->setPatternDirectory(rset->getString(5));
211  result->setAutomaticMasks(rset->getInt(6));
212  result->setSRP0BunchAdjustPosition(rset->getInt(7));
213  result->setConfigFile(rset->getString(8));
214 
215  Clob clob = rset->getClob(9);
216  m_size = clob.length();
217  Stream *instream = clob.getStream(1, 0);
218  unsigned char *buffer = new unsigned char[m_size];
219  memset(buffer, 0, m_size);
220  instream->readBuffer((char *)buffer, m_size);
221  /*
222  cout << "Opening the clob in Read only mode" << endl;
223  clob.open (OCCI_LOB_READONLY);
224  int clobLength=clob.length ();
225  cout << "Length of the clob is: " << clobLength << endl;
226  unsigned char* buffer = readClob (clob, clobLength);
227  clob.close ();
228  cout<< "the clob buffer is:"<<endl;
229  for (int i = 0; i < clobLength; ++i)
230  cout << (char) buffer[i];
231  cout << endl;
232 
233 
234  */
235  result->setSRPClob(buffer);
236  result->setAutomaticSrpSelect(rset->getInt(10));
237 
238  } catch (SQLException &e) {
239  throw(std::runtime_error(std::string("ODSRPConfig::fetchData(): ") + e.getMessage()));
240  }
241 }
242 
243 int ODSRPConfig::fetchID() noexcept(false) {
244  // Return from memory if available
245  if (m_ID != 0) {
246  return m_ID;
247  }
248 
249  this->checkConnection();
250 
251  try {
252  Statement *stmt = m_conn->createStatement();
253  stmt->setSQL(
254  "SELECT srp_configuration_id FROM ecal_srp_configuration "
255  "WHERE srp_tag=:srp_tag ");
256 
257  stmt->setString(1, getConfigTag());
258 
259  ResultSet *rset = stmt->executeQuery();
260 
261  if (rset->next()) {
262  m_ID = rset->getInt(1);
263  } else {
264  m_ID = 0;
265  }
266  m_conn->terminateStatement(stmt);
267  } catch (SQLException &e) {
268  throw(std::runtime_error(std::string("ODSRPConfig::fetchID: ") + e.getMessage()));
269  }
270 
271  return m_ID;
272 }
int fetchNextId() noexcept(false)
Definition: ODSRPConfig.cc:37
oracle::occi::Stream Stream
Definition: IODConfig.h:22
int fetchID() noexcept(false)
Definition: ODSRPConfig.cc:243
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODSRPConfig.cc:56
~ODSRPConfig() override
Definition: ODSRPConfig.cc:35
void clear()
Definition: ODSRPConfig.cc:25
void fetchData(ODSRPConfig *result) noexcept(false)
Definition: ODSRPConfig.cc:185
void prepareWrite() noexcept(false) override
Definition: ODSRPConfig.cc:109
oracle::occi::Statement Statement
Definition: IODConfig.h:21
oracle::occi::Clob Clob
Definition: IODConfig.h:23
string fname
main script
oracle::occi::SQLException SQLException
Definition: IODConfig.h:20
void clear(HadCaloObj &c)
Definition: data.h:124
void writeDB() noexcept(false)
Definition: ODSRPConfig.cc:156
unsigned transform(const HcalDetId &id, unsigned transformCode)