CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/OnlineDB/EcalCondDB/src/LMFRunIOV.cc

Go to the documentation of this file.
00001 #include "OnlineDB/EcalCondDB/interface/LMFRunIOV.h"
00002 #include "OnlineDB/EcalCondDB/interface/LMFDefFabric.h"
00003 #include "OnlineDB/EcalCondDB/interface/DateHandler.h"
00004 
00005 using namespace std;
00006 using namespace oracle::occi;
00007 
00008 void LMFRunIOV::initialize() {
00009   Tm tm;
00010   tm.setToCurrentGMTime();
00011 
00012   m_intFields["lmr"] = 0;
00013   m_intFields["tag_id"] = 0;
00014   m_intFields["seq_id"] = 0;
00015   m_intFields["color_id"] = 0;
00016   m_intFields["trigType_id"] = 0;
00017   m_stringFields["subrun_start"] = tm.str();
00018   m_stringFields["subrun_end"] = tm.str();
00019   m_stringFields["db_timestamp"] = tm.str();
00020   m_stringFields["subrun_type"] = "none";
00021   m_className = "LMFRunIOV";
00022   
00023   _fabric = NULL;
00024 }
00025 
00026 LMFRunIOV::LMFRunIOV() : LMFUnique()
00027 {
00028   initialize();
00029 }
00030 
00031 LMFRunIOV::LMFRunIOV(oracle::occi::Environment* env,
00032                      oracle::occi::Connection* conn) : LMFUnique(env, conn)
00033 {
00034   initialize();
00035 }
00036 
00037 LMFRunIOV::LMFRunIOV(EcalDBConnection *c) : LMFUnique(c)
00038 {
00039   initialize();
00040 }
00041 
00042 LMFRunIOV::LMFRunIOV(const LMFRunIOV &r) {
00043   initialize();
00044   *this = r;
00045 }
00046 
00047 LMFRunIOV::~LMFRunIOV()
00048 {
00049   if (_fabric != NULL) {
00050     delete _fabric;
00051   }
00052 }
00053 
00054 LMFRunIOV& LMFRunIOV::setLMFRunTag(const LMFRunTag &tag)
00055 {
00056   setInt("tag_id", tag.getID());
00057   return *this;
00058 }
00059 
00060 LMFRunIOV& LMFRunIOV::setLMFRunTag(int tag_id)
00061 {
00062   setInt("tag_id", tag_id);
00063   return *this;
00064 }
00065 
00066 LMFRunTag LMFRunIOV::getLMFRunTag() const
00067 {
00068   LMFRunTag rtag = LMFRunTag(m_env, m_conn);
00069   rtag.setByID(getInt("tag_id"));
00070   return rtag;
00071 }
00072 
00073 LMFRunIOV& LMFRunIOV::setColor(const LMFColor &color)
00074 {
00075   setInt("color_id", color.getID());
00076   return *this;
00077 }
00078 
00079 LMFRunIOV& LMFRunIOV::setColor(int color_id)
00080 {
00081   setInt("color_id", color_id);
00082   return *this;
00083 }
00084 
00085 void LMFRunIOV::checkFabric() {
00086   if (_fabric == NULL) {
00087     _fabric = new LMFDefFabric(m_env, m_conn);
00088   }
00089 }
00090 
00091 LMFRunIOV& LMFRunIOV::setColorIndex(int color_index)
00092 {
00093   checkFabric();
00094   setInt("color_id", _fabric->getColorID(color_index));
00095   return *this;
00096 }
00097 
00098 LMFRunIOV& LMFRunIOV::setColor(std::string name)
00099 {
00100   checkFabric();
00101   setInt("color_id", _fabric->getColorID(name));
00102   return *this;
00103 }
00104 
00105 LMFColor LMFRunIOV::getLMFColor() const
00106 {
00107   LMFColor rcol = LMFColor(m_env, m_conn);
00108   rcol.setByID(getInt("color_id"));
00109   return rcol;
00110 }
00111 
00112 std::string LMFRunIOV::getColorShortName() const {
00113   LMFColor rcol = getLMFColor();
00114   return rcol.getShortName();
00115 }
00116 
00117 std::string LMFRunIOV::getColorLongName() const {
00118   LMFColor rcol = getLMFColor();
00119   return rcol.getLongName();
00120 }
00121 
00122 LMFRunIOV& LMFRunIOV::setTriggerType(LMFTrigType &trigType)
00123 {
00124   setInt("trigType_id", trigType.getID());
00125   return *this;
00126 }
00127 
00128 LMFRunIOV& LMFRunIOV::setTriggerType(std::string sname)
00129 {
00130   checkFabric();
00131   setInt("trigType_id", _fabric->getTrigTypeID(sname));
00132   return *this;
00133 }
00134 
00135 LMFRunIOV& LMFRunIOV::setTriggerType(int id) {
00136   setInt("trigType_id", id);
00137   return *this;
00138 }
00139 
00140 LMFTrigType LMFRunIOV::getTriggerType() const
00141 {
00142   LMFTrigType rt = LMFTrigType(m_env, m_conn);
00143   rt.setByID(getInt("trigType_id"));
00144   return rt;
00145 }
00146 
00147 LMFRunIOV& LMFRunIOV::setLmr(int n) {
00148   setInt("lmr", n);
00149   return *this;
00150 }
00151 
00152 int LMFRunIOV::getLmr() const {
00153   return getInt("lmr");
00154 }
00155 
00156 LMFRunIOV& LMFRunIOV::setSubRunStart(Tm start) {
00157   setString("subrun_start", start.str());
00158   return *this;
00159 }
00160 
00161 Tm LMFRunIOV::getSubRunStart() const {
00162   Tm t;
00163   t.setToString(getString("subrun_start"));
00164   return t;
00165 }
00166 
00167 LMFRunIOV& LMFRunIOV::setSubRunEnd(Tm stop) {
00168   setString("subrun_end", stop.str());
00169   return *this;
00170 }
00171 
00172 Tm LMFRunIOV::getSubRunEnd() const {
00173   Tm t;
00174   t.setToString(getString("subrun_end"));
00175   return t;
00176 }
00177 
00178 Tm LMFRunIOV::getDBInsertionTime() const {
00179   Tm t;
00180   t.setToString(getString("db_timestamp"));
00181   return t;
00182 }
00183 
00184 LMFRunIOV& LMFRunIOV::setSubRunType(const std::string &s) {
00185   setString("subrun_type", s);
00186   return *this;
00187 }
00188 
00189 std::string LMFRunIOV::getSubRunType() const {
00190   return getString("subrun_type");
00191 }
00192 
00193 LMFRunIOV& LMFRunIOV::setSequence(LMFSeqDat &seq)
00194 {
00195   LMFSeqDat *seqdat = new LMFSeqDat();
00196   *seqdat = seq;
00197   attach("sequence", seqdat);
00198   setInt("seq_id", seqdat->getID());
00199   return *this;
00200 }
00201 
00202 LMFSeqDat LMFRunIOV::getSequence() const
00203 {
00204   LMFSeqDat rs = LMFSeqDat(m_env, m_conn);
00205   rs.setByID(getInt("seq_id"));
00206   return rs;
00207 }
00208 
00209 void LMFRunIOV::dump() const {
00210   LMFUnique::dump();
00211   std::cout << "# Fabric Address: " << _fabric << std::endl;
00212   if (m_debug) {
00213     _fabric->dump();
00214   }
00215 }
00216 
00217 std::string LMFRunIOV::fetchIdSql(Statement *stmt)
00218 {
00219   std::string sql = "";
00220   
00221   sql = "SELECT LMF_IOV_ID FROM LMF_RUN_IOV WHERE SEQ_ID = :1 "
00222     "AND LMR = :2 ";
00223   if (m_intFields["tag_id"] > 0) {
00224     sql += "AND TAG_ID = :3";
00225   }
00226   stmt->setSQL(sql);
00227   stmt->setInt(1, m_intFields["seq_id"]);
00228   stmt->setInt(2, m_intFields["lmr"]);
00229   if (m_intFields["tag_id"] > 0) {
00230     stmt->setInt(3, m_intFields["tag_id"]);
00231   }
00232   return sql;
00233 }
00234 
00235 std::string LMFRunIOV::setByIDSql(Statement *stmt, int id) 
00236 {
00237    DateHandler dh(m_env, m_conn);
00238    std::string sql = "SELECT TAG_ID, SEQ_ID, LMR, COLOR_ID, TRIG_TYPE, "
00239      "SUBRUN_START, SUBRUN_END, SUBRUN_TYPE, DB_TIMESTAMP FROM LMF_RUN_IOV "
00240      "WHERE LMF_IOV_ID = :1";
00241    stmt->setSQL(sql);
00242    stmt->setInt(1, id);
00243    return sql;  
00244 }
00245 
00246 void LMFRunIOV::getParameters(ResultSet *rset) {
00247   DateHandler dh(m_env, m_conn);
00248   setLMFRunTag(rset->getInt(1));
00249   LMFSeqDat *seq;
00250   if (m_foreignKeys.find("sequence") != m_foreignKeys.end()) {
00251     seq = (LMFSeqDat*)m_foreignKeys["sequence"];
00252     setInt("seq_id", seq->getID());
00253   } else {
00254     seq = new LMFSeqDat;
00255     seq->setConnection(m_env, m_conn);
00256     seq->setByID(rset->getInt(2));
00257     setInt("seq_id", seq->getID());
00258     delete seq;
00259   }
00260   setInt("lmr", rset->getInt(3));
00261   setColor(rset->getInt(4));
00262   setTriggerType(rset->getInt(5));
00263   Date start = rset->getDate(6);
00264   setString("subrun_start", dh.dateToTm(start).str());
00265   Date stop = rset->getDate(7);
00266   setString("subrun_end", dh.dateToTm(stop).str());
00267   setString("subrun_type", rset->getString(8));
00268   setString("db_timestamp", rset->getTimestamp(9).toText("YYYY-MM-DD HH24:MI:SS", 0));
00269 }
00270 
00271 bool LMFRunIOV::isValid() {
00272   bool ret = true;
00273   if (!getLMFRunTag().isValid()) {
00274     ret = false;
00275   }
00276   if (!getSequence().isValid()) {
00277     ret = false;
00278   }
00279   if (!getTriggerType().isValid()) {
00280     ret = false;
00281   }
00282   if ((getLmr() < 0) || (getLmr() > 92)) {
00283     ret = false;
00284   }
00285   if (!getLMFColor().isValid()) {
00286     ret = false;
00287   }
00288   // subrun start and end are by definition valid
00289   return ret;
00290 }
00291 
00292 std::string LMFRunIOV::writeDBSql(Statement *stmt) 
00293 {
00294   // check that everything has been setup
00295   int tag_id = getInt("tag_id");
00296   int seq_id = getInt("seq_id");
00297   int color_id = getInt("color_id");
00298   int tt = getInt("trigType_id");
00299   std::string sp = sequencePostfix(getSubRunStart());
00300   std::string sql = "INSERT INTO LMF_RUN_IOV (LMF_IOV_ID, TAG_ID, SEQ_ID, "
00301     "LMR, COLOR_ID, TRIG_TYPE, SUBRUN_START, SUBRUN_END, SUBRUN_TYPE) VALUES "
00302     "(lmf_run_iov_" + sp + "_sq.NextVal, :1, :2, :3, :4, :5, :6, :7, :8)";
00303   stmt->setSQL(sql);
00304   DateHandler dm(m_env, m_conn);
00305   stmt->setInt(1, tag_id);
00306   stmt->setInt(2, seq_id);
00307   stmt->setInt(3, getInt("lmr"));
00308   stmt->setInt(4, color_id);
00309   stmt->setInt(5, tt);
00310   stmt->setDate(6, dm.tmToDate(getSubRunStart()));
00311   stmt->setDate(7, dm.tmToDate(getSubRunEnd()));
00312   stmt->setString(8, getSubRunType());
00313   return sql;
00314 }
00315 
00316 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(vector<int> par, 
00317                                                 const std::string &sql,
00318                                                 const std::string &method) 
00319   throw(std::runtime_error)
00320 {
00321   std::list<LMFRunIOV> l;
00322   this->checkConnection();
00323   try {
00324     Statement *stmt = m_conn->createStatement();
00325     stmt->setSQL(sql);
00326     for (unsigned int i = 0; i < par.size(); i++) {
00327       stmt->setInt(i + 1, par[i]);
00328     }
00329     ResultSet *rset = stmt->executeQuery();
00330     while (rset->next()) {
00331       int lmf_iov_id = rset->getInt(1);
00332       LMFRunIOV iov;
00333       iov.setConnection(m_env, m_conn);
00334       iov.setByID(lmf_iov_id);
00335       l.push_back(iov);
00336     }
00337     m_conn->terminateStatement(stmt);
00338   } catch (SQLException &e) {
00339     throw(std::runtime_error(m_className + "::" + method + ": " +
00340                              e.getMessage()));
00341   }
00342   return l;
00343 }
00344 
00345 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s) {
00346   int seq_id = s.getID();
00347   vector<int> parameters;
00348   parameters.push_back(seq_id);
00349   return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM LMF_RUN_IOV "
00350                          "WHERE SEQ_ID = :1", "fetchBySequence");
00351 }
00352 
00353 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr) {
00354   int seq_id = s.getID();
00355   vector<int> parameters;
00356   parameters.push_back(seq_id);
00357   parameters.push_back(lmr);
00358   return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM LMF_RUN_IOV "
00359                          "WHERE SEQ_ID = :1 AND LMR = :2", 
00360                          "fetchBySequence");
00361 }
00362 
00363 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr,
00364                                                 int type, int color) {
00365   int seq_id = s.getID();
00366   vector<int> parameters;
00367   parameters.push_back(seq_id);
00368   parameters.push_back(lmr);
00369   parameters.push_back(color);
00370   parameters.push_back(type);
00371   return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM LMF_RUN_IOV "
00372                          "WHERE SEQ_ID = :1 AND LMR = :2 AND COLOR_ID = :3 "
00373                          "AND TRIG_TYPE = :4",
00374                          "fetchBySequence");
00375 }
00376 
00377 std::list<LMFRunIOV> LMFRunIOV::fetchLastBeforeSequence(const LMFSeqDat &s, 
00378                                                         int lmr, int type, 
00379                                                         int color) {
00380   int seq_id = s.getID();
00381   vector<int> parameters;
00382   parameters.push_back(seq_id);
00383   parameters.push_back(lmr);
00384   parameters.push_back(color);
00385   parameters.push_back(type);
00386   return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM (SELECT "
00387                          "SEQ_ID, LMF_IOV_ID FROM LMF_RUN_IOV "
00388                          "WHERE SEQ_ID < :1 AND LMR = :2 AND COLOR_ID = :3 "
00389                          "AND TRIG_TYPE = :4 ORDER BY SEQ_ID DESC) WHERE "
00390                          "ROWNUM <= 1",
00391                          "fetchBySequence");
00392 }
00393 
00394 LMFRunIOV& LMFRunIOV::operator=(const LMFRunIOV &r) {
00395   if (this != &r) {
00396     LMFUnique::operator=(r);
00397     if (r._fabric != NULL) {
00398       checkFabric();//      _fabric = new LMFDefFabric;
00399       if (m_debug) {
00400         _fabric->debug();
00401         std::cout << "COPYING INTO " << _fabric << std::endl;
00402       }
00403       *_fabric = *(r._fabric);
00404     }
00405   }
00406   return *this;
00407 }