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
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
00068
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
00094 std::cout << "Going to read SRP file: " << fname << endl;
00095
00096 ifstream inpFile;
00097 inpFile.open(fname.c_str());
00098
00099
00100 int bufsize = 0;
00101 inpFile.seekg( 0,ios::end );
00102 bufsize = inpFile.tellg();
00103 std::cout <<" bufsize ="<<bufsize<< std::endl;
00104
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
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
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
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
00208 if(result->getId()==0 && (result->getConfigTag()=="") ){
00209
00210 result->fetchID();
00211 }
00212
00213 try {
00214
00215 m_readStmt->setSQL("SELECT * "
00216 " FROM ECAL_SRP_CONFIGURATION "
00217 " where (srp_configuration_id = :1 or srp_tag=:2 )" );
00218 m_readStmt->setInt(1, result->getId());
00219 m_readStmt->setString(2, result->getConfigTag());
00220 ResultSet* rset = m_readStmt->executeQuery();
00221
00222 rset->next();
00223
00224
00225 result->setId(rset->getInt(1));
00226 result->setConfigTag(rset->getString(2));
00227
00228 result->setDebugMode(rset->getInt(3));
00229 result->setDummyMode(rset->getInt(4));
00230 result->setPatternDirectory(rset->getString(5));
00231 result->setAutomaticMasks(rset->getInt(6));
00232 result->setSRP0BunchAdjustPosition(rset->getInt(7));
00233 result->setConfigFile(rset->getString(8));
00234
00235 Clob clob = rset->getClob(9);
00236 m_size = clob.length();
00237 Stream *instream = clob.getStream (1,0);
00238 unsigned char *buffer = new unsigned char[m_size];
00239 memset (buffer, 0, m_size);
00240 instream->readBuffer ((char*)buffer, m_size);
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 result->setSRPClob(buffer );
00256 result->setAutomaticSrpSelect(rset->getInt(10));
00257
00258 } catch (SQLException &e) {
00259 throw(std::runtime_error("ODSRPConfig::fetchData(): "+e.getMessage()));
00260 }
00261 }
00262
00263
00264
00265 int ODSRPConfig::fetchID() throw(std::runtime_error)
00266 {
00267
00268 if (m_ID!=0) {
00269 return m_ID;
00270 }
00271
00272 this->checkConnection();
00273
00274 try {
00275 Statement* stmt = m_conn->createStatement();
00276 stmt->setSQL("SELECT srp_configuration_id FROM ecal_srp_configuration "
00277 "WHERE srp_tag=:srp_tag "
00278 );
00279
00280 stmt->setString(1, getConfigTag() );
00281
00282 ResultSet* rset = stmt->executeQuery();
00283
00284 if (rset->next()) {
00285 m_ID = rset->getInt(1);
00286 } else {
00287 m_ID = 0;
00288 }
00289 m_conn->terminateStatement(stmt);
00290 } catch (SQLException &e) {
00291 throw(std::runtime_error("ODSRPConfig::fetchID: "+e.getMessage()));
00292 }
00293
00294 return m_ID;
00295 }