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 CMS_ECAL_LASER_COND.LMF_RUN_IOV WHERE "
00222 "SEQ_ID = :1 "
00223 "AND LMR = :2 ";
00224 if (m_intFields["tag_id"] > 0) {
00225 sql += "AND TAG_ID = :3";
00226 }
00227 stmt->setSQL(sql);
00228 stmt->setInt(1, m_intFields["seq_id"]);
00229 stmt->setInt(2, m_intFields["lmr"]);
00230 if (m_intFields["tag_id"] > 0) {
00231 stmt->setInt(3, m_intFields["tag_id"]);
00232 }
00233 return sql;
00234 }
00235
00236 std::string LMFRunIOV::setByIDSql(Statement *stmt, int id)
00237 {
00238 DateHandler dh(m_env, m_conn);
00239 std::string sql = "SELECT TAG_ID, SEQ_ID, LMR, COLOR_ID, TRIG_TYPE, "
00240 "SUBRUN_START, SUBRUN_END, SUBRUN_TYPE, DB_TIMESTAMP FROM "
00241 "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
00242 "WHERE LMF_IOV_ID = :1";
00243 stmt->setSQL(sql);
00244 stmt->setInt(1, id);
00245 return sql;
00246 }
00247
00248 void LMFRunIOV::getParameters(ResultSet *rset) {
00249 DateHandler dh(m_env, m_conn);
00250 setLMFRunTag(rset->getInt(1));
00251 LMFSeqDat *seq;
00252 if (m_foreignKeys.find("sequence") != m_foreignKeys.end()) {
00253 seq = (LMFSeqDat*)m_foreignKeys["sequence"];
00254 setInt("seq_id", seq->getID());
00255 } else {
00256 seq = new LMFSeqDat;
00257 seq->setConnection(m_env, m_conn);
00258 seq->setByID(rset->getInt(2));
00259 setInt("seq_id", seq->getID());
00260 delete seq;
00261 }
00262 setInt("lmr", rset->getInt(3));
00263 setColor(rset->getInt(4));
00264 setTriggerType(rset->getInt(5));
00265 Date start = rset->getDate(6);
00266 setString("subrun_start", dh.dateToTm(start).str());
00267 Date stop = rset->getDate(7);
00268 setString("subrun_end", dh.dateToTm(stop).str());
00269 setString("subrun_type", rset->getString(8));
00270 setString("db_timestamp", rset->getTimestamp(9).toText("YYYY-MM-DD HH24:MI:SS", 0));
00271 }
00272
00273 bool LMFRunIOV::isValid() {
00274 bool ret = true;
00275 if (!getLMFRunTag().isValid()) {
00276 ret = false;
00277 }
00278 if (!getSequence().isValid()) {
00279 ret = false;
00280 }
00281 if (!getTriggerType().isValid()) {
00282 ret = false;
00283 }
00284 if ((getLmr() < 0) || (getLmr() > 92)) {
00285 ret = false;
00286 }
00287 if (!getLMFColor().isValid()) {
00288 ret = false;
00289 }
00290
00291 return ret;
00292 }
00293
00294 std::string LMFRunIOV::writeDBSql(Statement *stmt)
00295 {
00296
00297 int tag_id = getInt("tag_id");
00298 int seq_id = getInt("seq_id");
00299 int color_id = getInt("color_id");
00300 int tt = getInt("trigType_id");
00301 std::string sp = sequencePostfix(getSubRunStart());
00302 std::string sql = "INSERT INTO LMF_RUN_IOV (LMF_IOV_ID, TAG_ID, SEQ_ID, "
00303 "LMR, COLOR_ID, TRIG_TYPE, SUBRUN_START, SUBRUN_END, SUBRUN_TYPE) VALUES "
00304 "(lmf_run_iov_" + sp + "_sq.NextVal, :1, :2, :3, :4, :5, :6, :7, :8)";
00305 stmt->setSQL(sql);
00306 DateHandler dm(m_env, m_conn);
00307 stmt->setInt(1, tag_id);
00308 stmt->setInt(2, seq_id);
00309 stmt->setInt(3, getInt("lmr"));
00310 stmt->setInt(4, color_id);
00311 stmt->setInt(5, tt);
00312 stmt->setDate(6, dm.tmToDate(getSubRunStart()));
00313 stmt->setDate(7, dm.tmToDate(getSubRunEnd()));
00314 stmt->setString(8, getSubRunType());
00315 return sql;
00316 }
00317
00318 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(vector<int> par,
00319 const std::string &sql,
00320 const std::string &method)
00321 throw(std::runtime_error)
00322 {
00323 std::list<LMFRunIOV> l;
00324 this->checkConnection();
00325 try {
00326 Statement *stmt = m_conn->createStatement();
00327 stmt->setSQL(sql);
00328 for (unsigned int i = 0; i < par.size(); i++) {
00329 stmt->setInt(i + 1, par[i]);
00330 }
00331 ResultSet *rset = stmt->executeQuery();
00332 while (rset->next() != 0) {
00333 int lmf_iov_id = rset->getInt(1);
00334 LMFRunIOV iov;
00335 iov.setConnection(m_env, m_conn);
00336 iov.setByID(lmf_iov_id);
00337 l.push_back(iov);
00338 }
00339 m_conn->terminateStatement(stmt);
00340 } catch (SQLException &e) {
00341 throw(std::runtime_error(m_className + "::" + method + ": " +
00342 e.getMessage()));
00343 }
00344 return l;
00345 }
00346
00347 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s) {
00348 int seq_id = s.getID();
00349 vector<int> parameters;
00350 parameters.push_back(seq_id);
00351 return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM "
00352 "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
00353 "WHERE SEQ_ID = :1", "fetchBySequence");
00354 }
00355
00356 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr) {
00357 int seq_id = s.getID();
00358 vector<int> parameters;
00359 parameters.push_back(seq_id);
00360 parameters.push_back(lmr);
00361 return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM "
00362 "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
00363 "WHERE SEQ_ID = :1 AND LMR = :2",
00364 "fetchBySequence");
00365 }
00366
00367 std::list<LMFRunIOV> LMFRunIOV::fetchBySequence(const LMFSeqDat &s, int lmr,
00368 int type, int color) {
00369 int seq_id = s.getID();
00370 vector<int> parameters;
00371 parameters.push_back(seq_id);
00372 parameters.push_back(lmr);
00373 parameters.push_back(color);
00374 parameters.push_back(type);
00375 return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM "
00376 "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
00377 "WHERE SEQ_ID = :1 AND LMR = :2 AND COLOR_ID = :3 "
00378 "AND TRIG_TYPE = :4",
00379 "fetchBySequence");
00380 }
00381
00382 std::list<LMFRunIOV> LMFRunIOV::fetchLastBeforeSequence(const LMFSeqDat &s,
00383 int lmr, int type,
00384 int color) {
00385 int seq_id = s.getID();
00386 vector<int> parameters;
00387 parameters.push_back(seq_id);
00388 parameters.push_back(lmr);
00389 parameters.push_back(color);
00390 parameters.push_back(type);
00391 return fetchBySequence(parameters, "SELECT LMF_IOV_ID FROM (SELECT "
00392 "SEQ_ID, LMF_IOV_ID FROM "
00393 "CMS_ECAL_LASER_COND.LMF_RUN_IOV "
00394 "WHERE SEQ_ID < :1 AND LMR = :2 AND COLOR_ID = :3 "
00395 "AND TRIG_TYPE = :4 ORDER BY SEQ_ID DESC) WHERE "
00396 "ROWNUM <= 1",
00397 "fetchBySequence");
00398 }
00399
00400 LMFRunIOV& LMFRunIOV::operator=(const LMFRunIOV &r) {
00401 if (this != &r) {
00402 LMFUnique::operator=(r);
00403 if (r._fabric != NULL) {
00404 checkFabric();
00405 if (m_debug) {
00406 _fabric->debug();
00407 std::cout << "COPYING INTO " << _fabric << std::endl;
00408 }
00409 *_fabric = *(r._fabric);
00410 }
00411 }
00412 return *this;
00413 }