00001 #include <fstream>
00002 #include <iostream>
00003 #include <stdio.h>
00004 #include <stdexcept>
00005 #include <string>
00006
00007 #include "OnlineDB/Oracle/interface/Oracle.h"
00008
00009 #include "OnlineDB/EcalCondDB/interface/ODTTCciConfig.h"
00010
00011 using namespace std;
00012 using namespace oracle::occi;
00013
00014 ODTTCciConfig::ODTTCciConfig()
00015 {
00016 m_env = NULL;
00017 m_conn = NULL;
00018 m_writeStmt = NULL;
00019 m_readStmt = NULL;
00020 m_config_tag="";
00021 m_configuration_script="";
00022 m_configuration_script_params="";
00023 m_ID=0;
00024 clear();
00025 m_size=0;
00026 }
00027
00028 void ODTTCciConfig::clear(){
00029 std::cout <<"entering clear" << std::endl;
00030 m_ttcci_file="";
00031 m_configuration_script="";
00032 m_configuration_script_params="";
00033 m_trg_mode="";
00034 m_trg_sleep=0;
00035
00036 }
00037
00038 ODTTCciConfig::~ODTTCciConfig()
00039 {
00040 }
00041
00042 int ODTTCciConfig::fetchNextId() throw(std::runtime_error) {
00043
00044 int result=0;
00045 try {
00046 this->checkConnection();
00047
00048 m_readStmt = m_conn->createStatement();
00049 m_readStmt->setSQL("select ecal_ttcci_config_sq.NextVal from dual");
00050 ResultSet* rset = m_readStmt->executeQuery();
00051 while (rset->next ()){
00052 result= rset->getInt(1);
00053 }
00054 m_conn->terminateStatement(m_readStmt);
00055 return result;
00056
00057 } catch (SQLException &e) {
00058 throw(runtime_error("ODTTCciConfig::fetchNextId(): "+e.getMessage()));
00059 }
00060
00061 }
00062
00063
00064
00065
00066 void ODTTCciConfig::prepareWrite()
00067 throw(runtime_error)
00068 {
00069 this->checkConnection();
00070
00071 int next_id=fetchNextId();
00072
00073 try {
00074 m_writeStmt = m_conn->createStatement();
00075 m_writeStmt->setSQL("INSERT INTO ECAL_TTCci_CONFIGURATION (ttcci_configuration_id, ttcci_tag, "
00076 " TTCCI_configuration_file, TRG_MODE, TRG_SLEEP, Configuration, configuration_script, configuration_script_params ) "
00077 "VALUES (:1, :2, :3, :4, :5, :6, :7, :8 )");
00078 m_writeStmt->setInt(1, next_id);
00079 m_writeStmt->setString(2, getConfigTag());
00080 m_writeStmt->setString(3, getTTCciConfigurationFile());
00081 m_writeStmt->setString(4, getTrgMode());
00082 m_writeStmt->setInt(5, getTrgSleep());
00083 m_writeStmt->setString(7, getConfigurationScript());
00084 m_writeStmt->setString(8, getConfigurationScriptParams());
00085
00086
00087 oracle::occi::Clob clob(m_conn);
00088 clob.setEmpty();
00089 m_writeStmt->setClob(6,clob);
00090 m_writeStmt->executeUpdate ();
00091 m_ID=next_id;
00092
00093 m_conn->terminateStatement(m_writeStmt);
00094 std::cout<<"TTCci Clob inserted into CONFIGURATION with id="<<next_id<<std::endl;
00095
00096
00097 m_writeStmt = m_conn->createStatement();
00098 m_writeStmt->setSQL ("SELECT Configuration FROM ECAL_TTCci_CONFIGURATION WHERE"
00099 " ttcci_configuration_id=:1 FOR UPDATE");
00100
00101 std::cout<<"updating the clob 0"<<std::endl;
00102
00103
00104 } catch (SQLException &e) {
00105 throw(runtime_error("ODTTCciConfig::prepareWrite(): "+e.getMessage()));
00106 }
00107
00108 std::cout<<"updating the clob 1 "<<std::endl;
00109
00110 }
00111
00112 void ODTTCciConfig::setParameters(std::map<string,string> my_keys_map){
00113
00114
00115
00116
00117 for( std::map<std::string, std::string >::iterator ci=
00118 my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00119
00120 if(ci->first== "TRG_MODE") setTrgMode(ci->second);
00121 if(ci->first== "TRG_SLEEP") setTrgSleep(atoi(ci->second.c_str()));
00122 if(ci->first== "TTCci_CONFIGURATION_ID") setConfigTag(ci->second);
00123 if(ci->first== "CONFIGURATION_SCRIPT" ) setConfigurationScript(ci->second);
00124 if(ci->first== "CONFIGURATION_SCRIPT_PARAMS" ) setConfigurationScriptParams(ci->second);
00125 if(ci->first== "CONFIGURATION_SCRIPT_PARAMETERS" ) setConfigurationScriptParams(ci->second);
00126 if(ci->first== "Configuration") {
00127 std::string fname=ci->second ;
00128 string str3;
00129 size_t pos, pose;
00130
00131 pos = fname.find("=");
00132 pose = fname.size();
00133 str3 = fname.substr (pos+1, pose-pos-2);
00134
00135 cout << "fname="<<fname<< " and reduced is: "<<str3 << endl;
00136 setTTCciConfigurationFile(str3 );
00137
00138
00139
00140 std::cout << "Going to read file: " << str3 << endl;
00141
00142 ifstream inpFile;
00143 inpFile.open(str3.c_str());
00144
00145
00146 int bufsize = 0;
00147 inpFile.seekg( 0,ios::end );
00148 bufsize = inpFile.tellg();
00149 std::cout <<" bufsize ="<<bufsize<< std::endl;
00150
00151 inpFile.seekg( 0,ios::beg );
00152
00153 inpFile.close();
00154 m_size=bufsize;
00155
00156 }
00157 }
00158
00159 }
00160
00161
00162 void ODTTCciConfig::writeDB()
00163 throw(runtime_error)
00164 {
00165
00166
00167 std::cout<<"updating the clob 2"<<std::endl;
00168
00169
00170 try {
00171
00172
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, getTTCciConfigurationFile(), m_size);
00182 int clobLength=clob.length ();
00183 cout << "Length of the clob is: " << clobLength << endl;
00184
00185 }
00186
00187 m_writeStmt->executeUpdate();
00188
00189 m_writeStmt->closeResultSet (rset);
00190
00191 } catch (SQLException &e) {
00192 throw(runtime_error("ODTTCciConfig::writeDB(): "+e.getMessage()));
00193 }
00194
00195 if (!this->fetchID()) {
00196 throw(runtime_error("ODTTCciConfig::writeDB: Failed to write"));
00197 }
00198
00199 }
00200
00201
00202
00203
00204
00205
00206 void ODTTCciConfig::fetchData(ODTTCciConfig * result)
00207 throw(runtime_error)
00208 {
00209 this->checkConnection();
00210 result->clear();
00211 if(result->getId()==0 && (result->getConfigTag()=="") ){
00212 throw(runtime_error("ODTTCciConfig::fetchData(): no Id defined for this ODTTCciConfig "));
00213 }
00214
00215 try {
00216
00217 m_readStmt->setSQL("SELECT * "
00218 "FROM ECAL_TTCci_CONFIGURATION "
00219 " where ( ttcci_configuration_id = :1 or ttcci_tag=:2 )" );
00220 m_readStmt->setInt(1, result->getId());
00221 m_readStmt->setString(2, result->getConfigTag());
00222 ResultSet* rset = m_readStmt->executeQuery();
00223
00224 rset->next();
00225
00226
00227 result->setId(rset->getInt(1));
00228 result->setConfigTag(rset->getString(2));
00229
00230
00231 result->setTTCciConfigurationFile(rset->getString(3));
00232 result->setTrgMode(rset->getString(4));
00233 result->setTrgSleep(rset->getInt(5));
00234
00235 result->setConfigurationScript(rset->getString(7));
00236 result->setConfigurationScriptParams(rset->getString(8));
00237
00238 Clob clob = rset->getClob (6);
00239 cout << "Opening the clob in Read only mode" << endl;
00240 clob.open (OCCI_LOB_READONLY);
00241 int clobLength=clob.length ();
00242 cout << "Length of the clob is: " << clobLength << endl;
00243 m_size=clobLength;
00244 unsigned char* buffer = readClob (clob, m_size);
00245 clob.close ();
00246 cout<< "the clob buffer is:"<<endl;
00247 for (int i = 0; i < clobLength; ++i)
00248 cout << (char) buffer[i];
00249 cout << endl;
00250
00251
00252 result->setTTCciClob(buffer );
00253
00254 } catch (SQLException &e) {
00255 throw(runtime_error("ODTTCciConfig::fetchData(): "+e.getMessage()));
00256 }
00257 }
00258
00259
00260
00261 int ODTTCciConfig::fetchID() throw(std::runtime_error)
00262 {
00263 if (m_ID!=0) {
00264 return m_ID;
00265 }
00266
00267 this->checkConnection();
00268
00269 try {
00270 Statement* stmt = m_conn->createStatement();
00271 stmt->setSQL("SELECT ttcci_configuration_id FROM ecal_ttcci_configuration "
00272 "WHERE ttcci_tag=:ttcci_tag "
00273 );
00274
00275 stmt->setString(1, getConfigTag() );
00276
00277 ResultSet* rset = stmt->executeQuery();
00278
00279 if (rset->next()) {
00280 m_ID = rset->getInt(1);
00281 } else {
00282 m_ID = 0;
00283 }
00284 m_conn->terminateStatement(stmt);
00285 } catch (SQLException &e) {
00286 throw(runtime_error("ODTTCciConfig::fetchID: "+e.getMessage()));
00287 }
00288 return m_ID;
00289 }