CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/OnlineDB/EcalCondDB/src/ODCond2ConfInfo.cc

Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <string>
00003 #include <string.h>
00004 #include "OnlineDB/Oracle/interface/Oracle.h"
00005 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00006 
00007 #include "OnlineDB/EcalCondDB/interface/ODCond2ConfInfo.h"
00008 
00009 using namespace std;
00010 using namespace oracle::occi;
00011 
00012 ODCond2ConfInfo::ODCond2ConfInfo()
00013 {
00014   m_env = NULL;
00015   m_conn = NULL;
00016   m_writeStmt = NULL;
00017   m_readStmt = NULL;
00018   m_config_tag="";
00019    m_ID=0;
00020    clear();   
00021 }
00022 
00023 
00024 void ODCond2ConfInfo::clear(){
00025   m_type="";
00026   m_loc="";
00027   m_run=0;
00028   m_desc="";
00029   m_rec_time = Tm();
00030   m_db_time = Tm();
00031   m_typ_id=0;
00032   m_loc_id=0;
00033 
00034 
00035 }
00036 
00037 
00038 
00039 ODCond2ConfInfo::~ODCond2ConfInfo()
00040 {
00041 }
00042 
00043 
00044 
00045 int ODCond2ConfInfo::fetchNextId()  throw(std::runtime_error) {
00046 
00047   int result=0;
00048   try {
00049     this->checkConnection();
00050 
00051     m_readStmt = m_conn->createStatement(); 
00052     m_readStmt->setSQL("select COND2CONF_INFO_SQ.NextVal from DUAL ");
00053     ResultSet* rset = m_readStmt->executeQuery();
00054     while (rset->next ()){
00055       result= rset->getInt(1);
00056     }
00057     result++;
00058     m_conn->terminateStatement(m_readStmt);
00059     return result; 
00060 
00061   } catch (SQLException &e) {
00062     throw(std::runtime_error("ODCond2ConfInfo::fetchNextId():  "+e.getMessage()));
00063   }
00064 
00065 }
00066 
00067 void ODCond2ConfInfo::fetchParents()  throw(std::runtime_error) {
00068 
00069 
00070 
00071   if(m_typ_id==0) {
00072   
00073     if(getType()!=""){
00074     try {
00075       this->checkConnection();
00076       m_readStmt = m_conn->createStatement(); 
00077       m_readStmt->setSQL("select def_id from COND2CONF_TYPE_DEF where rec_type="+getType());
00078       ResultSet* rset = m_readStmt->executeQuery();
00079       while (rset->next ()){
00080         m_typ_id= rset->getInt(1);
00081       }
00082       m_conn->terminateStatement(m_readStmt);
00083       
00084       
00085     } catch (SQLException &e) {
00086       throw(std::runtime_error("ODCond2ConfInfo::fetchParents():  "+e.getMessage()));
00087     }
00088   }
00089   }
00090   if(m_loc_id==0){
00091   if(getLocation()!=""){
00092     try {
00093       this->checkConnection();
00094       m_readStmt = m_conn->createStatement(); 
00095       m_readStmt->setSQL("select def_id from location_def where location="+getLocation());
00096       ResultSet* rset = m_readStmt->executeQuery();
00097       while (rset->next ()){
00098         m_loc_id= rset->getInt(1);
00099       }
00100       m_conn->terminateStatement(m_readStmt);
00101     } catch (SQLException &e) {
00102       throw(std::runtime_error("ODCond2ConfInfo::fetchParents():  "+e.getMessage()));
00103     }
00104   }
00105   }
00106 
00107 }
00108 
00109 void ODCond2ConfInfo::prepareWrite()
00110   throw(std::runtime_error)
00111 {
00112   this->checkConnection();
00113 
00114   int next_id=0;
00115   if(getId()==0){
00116     next_id=fetchNextId();
00117   }
00118 
00119   fetchParents();
00120   try {
00121     m_writeStmt = m_conn->createStatement();
00122     m_writeStmt->setSQL("INSERT INTO "+getTable()+" ( rec_id, rec_type_id, rec_date, "
00123                 "location_id, run_number, short_desc ) " 
00124                         " VALUES ( :1, :2, :3 , :4, :5, :6 ) " );
00125 
00126     m_writeStmt->setInt(1, next_id);
00127     m_writeStmt->setInt(3, m_typ_id);
00128     m_writeStmt->setInt(4, m_loc_id);
00129     
00130     m_ID=next_id;
00131 
00132   } catch (SQLException &e) {
00133     throw(std::runtime_error("ODCond2ConfInfo::prepareWrite():  "+e.getMessage()));
00134   }
00135 
00136 }
00137 
00138 void ODCond2ConfInfo::setParameters(std::map<string,string> my_keys_map){
00139   
00140   // parses the result of the XML parser that is a map of 
00141   // string string with variable name variable value 
00142   
00143   for( std::map<std::string, std::string >::iterator ci=
00144          my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
00145     
00146     //    if(ci->first==  "TAG") setConfigTag(ci->second);
00147     
00148   }
00149   std::cout<< "method not yet implemented"<< std::endl; 
00150 
00151 }
00152 
00153 
00154 void ODCond2ConfInfo::writeDB()
00155   throw(std::runtime_error)
00156 {
00157   this->checkConnection();
00158   this->checkPrepare();
00159 
00160 
00161   DateHandler dh(m_env, m_conn);
00162   if (m_rec_time.isNull()) {
00163     int very_old_time=0;
00164     m_rec_time = Tm(very_old_time);
00165   }
00166 
00167   try {
00168 
00169     m_writeStmt->setDate(3, dh.tmToDate(this->m_rec_time));
00170     m_writeStmt->setInt(5, this->getRunNumber());
00171     m_writeStmt->setString(6, this->getDescription());
00172 
00173     m_writeStmt->executeUpdate();
00174 
00175 
00176   } catch (SQLException &e) {
00177     throw(std::runtime_error("ODCond2ConfInfo::writeDB():  "+e.getMessage()));
00178   }
00179   // Now get the ID
00180   if (!this->fetchID()) {
00181     throw(std::runtime_error("ODCond2ConfInfo::writeDB:  Failed to write"));
00182   }
00183 
00184 }
00185 
00186 
00187 void ODCond2ConfInfo::fetchData(ODCond2ConfInfo * result)
00188   throw(std::runtime_error)
00189 {
00190   this->checkConnection();
00191   result->clear();
00192 
00193   DateHandler dh(m_env, m_conn);
00194 
00195 
00196   if(result->getId()==0 ){
00197     throw(std::runtime_error("ODCond2ConfInfo::fetchData(): no Id defined for this ODCond2ConfInfo "));
00198   }
00199 
00200   try {
00201 
00202     m_readStmt->setSQL("SELECT rec_id, REC_TYPE, rec_date, location, "
00203                        "run_number, short_desc, db_timestamp FROM " + getTable() + " , COND2CONF_TYPE_DEF , location_def "  
00204                        " where  rec_id = :1  AND COND2CONF_TYPE_DEF.def_id="
00205                        +getTable()+".REC_TYPE_ID AND location_def.def_id=LOCATION_ID ");
00206     m_readStmt->setInt(1, result->getId());
00207     ResultSet* rset = m_readStmt->executeQuery();
00208 
00209     rset->next();
00210 
00211     // 1 is the id and 2 is the config tag and 3 is the version
00212 
00213          
00214  
00215 
00216     //    result->setId(rset->getInt(1));
00217 
00218 
00219     result->setType(rset->getString(2));
00220     Date startDate = rset->getDate(3);
00221     result->setLocation(rset->getString(4));
00222     result->setRunNumber(rset->getInt(5));
00223     result->setDescription(rset->getString(6));
00224     Date endDate = rset->getDate(7);
00225 
00226     m_rec_time = dh.dateToTm( startDate );
00227     m_db_time = dh.dateToTm( endDate );
00228 
00229   } catch (SQLException &e) {
00230     throw(std::runtime_error("ODCond2ConfInfo::fetchData():  "+e.getMessage()));
00231   }
00232 }
00233 
00234 int ODCond2ConfInfo::fetchID()    throw(std::runtime_error)
00235 {
00236   // Return from memory if available
00237   if (m_ID!=0) {
00238     return m_ID;
00239   }
00240 
00241   this->checkConnection();
00242 
00243 
00244   fetchParents();
00245 
00246   try {
00247     Statement* stmt = m_conn->createStatement();
00248     stmt->setSQL("SELECT rec_id FROM "+ getTable()+
00249                  "WHERE  rec_type_id=:1 and (run_number=:2 or short_desc=:3 ) order by rec_id DESC " );
00250 
00251     stmt->setInt(1, m_typ_id );
00252     stmt->setInt(2, getRunNumber() );
00253     stmt->setString(3, getDescription() );
00254 
00255     ResultSet* rset = stmt->executeQuery();
00256 
00257     if (rset->next()) {
00258       m_ID = rset->getInt(1);
00259     } else {
00260       m_ID = 0;
00261     }
00262     m_conn->terminateStatement(stmt);
00263   } catch (SQLException &e) {
00264     throw(std::runtime_error("ODCond2ConfInfo::fetchID:  "+e.getMessage()));
00265   }
00266 
00267   return m_ID;
00268 }