diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/admin_connection.cc | 4 | ||||
-rw-r--r-- | server/src/database.h | 8 | ||||
-rw-r--r-- | server/src/pracrodao.h | 6 | ||||
-rw-r--r-- | server/src/pracrodaopgsql.cc | 14 | ||||
-rw-r--r-- | server/src/pracrodaopgsql.h | 6 | ||||
-rw-r--r-- | server/src/pracrodaotest.cc | 158 | ||||
-rw-r--r-- | server/src/pracrodaotest.h | 12 | ||||
-rw-r--r-- | server/src/session.cc | 14 | ||||
-rw-r--r-- | server/src/session.h | 4 |
9 files changed, 179 insertions, 47 deletions
diff --git a/server/src/admin_connection.cc b/server/src/admin_connection.cc index e470946..ecc7c65 100644 --- a/server/src/admin_connection.cc +++ b/server/src/admin_connection.cc @@ -42,7 +42,7 @@ static std::string admin_sessionunlock(Environment &env, std::string id) env.sessions.deleteSession(id); return "Session " + id + " was 'readonly' and has been discarded."; } else { - session->setActive(false); + session->setIdle(true); return "Session " + id + " has been deactivated (set to idle)."; } } @@ -59,7 +59,7 @@ static std::string admin_listactivesessions(Environment &env) Session *s = env.sessions.session(*i); SessionAutolock lock(*s); str += "Session " + *i + ": "+s->templ+" on "+s->patientid+" "+ - std::string(s->active()?"[active]":"[idle]")+"\n"; + std::string(s->idle()?"[idle]":"[active]")+"\n"; i++; } diff --git a/server/src/database.h b/server/src/database.h index fe644bc..8876ae7 100644 --- a/server/src/database.h +++ b/server/src/database.h @@ -178,16 +178,16 @@ public: return dao->restore(data); } - bool active(std::string sessionid) + bool idle(std::string sessionid) { if(!dao || sessionid == "") return false; - return dao->active(sessionid); + return dao->idle(sessionid); } - void setActive(std::string sessionid, bool val) + void setIdle(std::string sessionid, bool val) { if(dao && sessionid != "") { - dao->setActive(sessionid, val); + dao->setIdle(sessionid, val); } } diff --git a/server/src/pracrodao.h b/server/src/pracrodao.h index 53c7db2..6cfd741 100644 --- a/server/src/pracrodao.h +++ b/server/src/pracrodao.h @@ -66,10 +66,12 @@ public: virtual void commit(std::string sessionid) = 0; virtual void nocommit(std::string sessionid) = 0; virtual void discard(std::string sessionid) = 0; + virtual std::string serialise() = 0; virtual void restore(const std::string &data) = 0; - virtual bool active(std::string sessionid) = 0; - virtual void setActive(std::string sessionid, bool active) = 0; + + virtual bool idle(std::string sessionid) = 0; + virtual void setIdle(std::string sessionid, bool idle) = 0; protected: std::string host; diff --git a/server/src/pracrodaopgsql.cc b/server/src/pracrodaopgsql.cc index b8fb505..8eb3d16 100644 --- a/server/src/pracrodaopgsql.cc +++ b/server/src/pracrodaopgsql.cc @@ -492,7 +492,7 @@ void PracroDAOPgsql::discard(std::string sessionid) } } -bool PracroDAOPgsql::active(std::string sessionid) +bool PracroDAOPgsql::idle(std::string sessionid) { std::string ts = "SELECT status FROM commits WHERE uid='"+sessionid+"';"; pqxx::work W(*conn); @@ -500,22 +500,18 @@ bool PracroDAOPgsql::active(std::string sessionid) pqxx::result::const_iterator ri = R.begin(); if(ri != R.end()) { std::string status = (*ri)[0].c_str(); - if(status == "idle") { - return false; - } else { - return true; - } + return status == "idle"; } return false; } -void PracroDAOPgsql::setActive(std::string sessionid, bool a) +void PracroDAOPgsql::setIdle(std::string sessionid, bool idle) { std::string ts; try { pqxx::work W(*conn); - if(a == false) { + if(idle) { ts = "UPDATE commits SET status='idle' WHERE uid="+sessionid+ " AND status='active';"; } else { @@ -526,7 +522,7 @@ void PracroDAOPgsql::setActive(std::string sessionid, bool a) W.commit(); } catch (std::exception &e) { - ERR_LOG(db, "setActive failed: %s: %s\n", e.what(), ts.c_str()); + ERR_LOG(db, "setIdle failed: %s: %s\n", e.what(), ts.c_str()); } } diff --git a/server/src/pracrodaopgsql.h b/server/src/pracrodaopgsql.h index c943e93..e21d39c 100644 --- a/server/src/pracrodaopgsql.h +++ b/server/src/pracrodaopgsql.h @@ -67,10 +67,12 @@ public: void commit(std::string sessionid); void nocommit(std::string sessionid); void discard(std::string sessionid); + std::string serialise() { return ""; } void restore(const std::string &data) {} - bool active(std::string sessionid); - void setActive(std::string sessionid, bool active); + + bool idle(std::string sessionid); + void setIdle(std::string sessionid, bool idle); private: pqxx::connection *conn; diff --git a/server/src/pracrodaotest.cc b/server/src/pracrodaotest.cc index 51ee24f..efd94f6 100644 --- a/server/src/pracrodaotest.cc +++ b/server/src/pracrodaotest.cc @@ -68,6 +68,58 @@ void PracroDAOTest::commitTransaction(std::string sessionid, std::string macro = _macro.attributes["name"]; std::stringstream timestamp; timestamp << now; + dbtable_t::iterator ci = data.commits.begin(); + while(ci != data.commits.end()) { + dbrow_t &c = *ci; + if(c["uid"] == sessionid) { + break; + } + ci++; + } + + if(ci == data.commits.end()) { + DEBUG(testdb, "Create new commit: %s", sessionid.c_str()); + dbrow_t c; + c["patientid"] = transaction.cpr; + c["template"] = commit.templ; + c["version"] = "1.0"; + c["timestamp"] = timestamp.str(); + c["uid"] = sessionid; + c["status"] = "active"; + data.commits.push_back(c); + } else { + dbrow_t &c = *ci; + if(c["status"] == "committed") { + ERR_LOG(db, "Attempt to add to committed session %s blocked!\n", + sessionid.c_str()); + return; + } + + DEBUG(testdb, "Working on old commit: %s", sessionid.c_str()); + c["status"] = "active"; + } + + + + + + + + + + + + + + + + + + + + + + dbrow_t t; t["uid"] = data.trseq.nextval(); t["patientid"] = transaction.cpr; @@ -123,9 +175,6 @@ Values PracroDAOTest::getLatestValues(std::string sessionid, oldest); Values values; - // TODO: Take Macro* into account. If supplied (not NULL) the macro name, and - // optionally version number should match the transaction. - Fieldnames::iterator fi = fieldnames.begin(); while(fi != fieldnames.end()) { std::string fieldname = *fi; @@ -133,19 +182,21 @@ Values PracroDAOTest::getLatestValues(std::string sessionid, // Find matching transactions dbtable_t::iterator ti = data.transactions.begin(); while(ti != data.transactions.end()) { - std::map<std::string, std::string> &transaction = *ti; + dbrow_t &transaction = *ti; time_t timestamp = atol(transaction["timestamp"].c_str()); - if(transaction["patientid"] == patientid && timestamp >= oldest && (transaction["macro"] == macro_name || macro == NULL)) { + if(transaction["patientid"] == patientid && timestamp >= oldest && + (transaction["macro"] == macro_name || macro == NULL)) { std::string tid = transaction["uid"]; // Find transaction values dbtable_t::iterator vi = data.fields.begin(); while(vi != data.fields.end()) { - std::map<std::string, std::string> &field = *vi; + dbrow_t &field = *vi; // Upon match, insert it into values if(field["transaction"] == tid && field["name"] == fieldname) { - if(values.find(fieldname) == values.end() || values[fieldname].timestamp < timestamp) { + if(values.find(fieldname) == values.end() || + values[fieldname].timestamp < timestamp) { values[fieldname].timestamp = timestamp; values[fieldname].value = field["value"]; values[fieldname].source = "testdb"; @@ -175,7 +226,7 @@ unsigned PracroDAOTest::nrOfCommits(std::string sessionid, // Find and count matching transactions dbtable_t::iterator ti = data.transactions.begin(); while(ti != data.transactions.end()) { - std::map<std::string, std::string> &transaction = *ti; + dbrow_t &transaction = *ti; time_t timestamp = atol(transaction["timestamp"].c_str()); if(transaction["patientid"] == patientid && transaction["macro"] == macroname && @@ -230,13 +281,13 @@ std::vector<Fieldname> PracroDAOTest::getFieldnames() return fieldnames; } -bool PracroDAOTest::active(std::string sessionid) +bool PracroDAOTest::idle(std::string sessionid) { dbtable_t::iterator i = data.commits.begin(); while(i != data.commits.end()) { dbrow_t &commit = *i; - if(commit["sessionid"] == sessionid) { - return commit["status"] != "idle"; + if(commit["uid"] == sessionid) { + return commit["status"] == "idle"; } i++; } @@ -244,13 +295,52 @@ bool PracroDAOTest::active(std::string sessionid) return false; } -void PracroDAOTest::setActive(std::string sessionid, bool active) +void PracroDAOTest::setIdle(std::string sessionid, bool idle) { dbtable_t::iterator i = data.commits.begin(); while(i != data.commits.end()) { dbrow_t &commit = *i; - if(commit["sessionid"] == sessionid && commit["status"] != "committed") { - commit["status"] = active?"active":"idle"; + if(commit["uid"] == sessionid && commit["status"] != "committed") { + commit["status"] = idle?"idle":"active"; + } + i++; + } +} + +void PracroDAOTest::commit(std::string sessionid) +{ + dbtable_t::iterator i = data.commits.begin(); + while(i != data.commits.end()) { + dbrow_t &commit = *i; + if(commit["uid"] == sessionid && commit["status"] != "committed") { + commit["status"] = "committed"; + return; + } + i++; + } +} + +void PracroDAOTest::nocommit(std::string sessionid) +{ + dbtable_t::iterator i = data.commits.begin(); + while(i != data.commits.end()) { + dbrow_t &commit = *i; + if(commit["uid"] == sessionid && commit["status"] != "committed") { + commit["status"] = "idle"; + return; + } + i++; + } +} + +void PracroDAOTest::discard(std::string sessionid) +{ + dbtable_t::iterator i = data.commits.begin(); + while(i != data.commits.end()) { + dbrow_t &commit = *i; + if(commit["uid"] == sessionid && commit["status"] != "committed") { + data.commits.erase(i); + return; } i++; } @@ -282,6 +372,8 @@ static bool vectorFind(std::vector<Fieldname> fs, TEST_BEGIN; +debug_parse("+all"); + Data data; PracroDAOTest db(data); @@ -328,6 +420,44 @@ TEST_EQUAL_INT(vals.size(), 1, "One value"); TEST_NOTEQUAL(vals.find("field1"), vals.end(), "find value"); +{ + std::string sid = db.newSessionId(); + db.commitTransaction(sid, transaction, commit, macro, now); + TEST_FALSE(db.idle(sid), "Session should not be idle."); + + db.setIdle(sid, true); + TEST_TRUE(db.idle(sid), "Session should be idle."); + + db.setIdle(sid, false); + TEST_FALSE(db.idle(sid), "Session1 should not be idle."); +} + +{ + std::string sid = db.newSessionId(); + db.commitTransaction(sid, transaction, commit, macro, now); + TEST_FALSE(db.idle(sid), "Session should not be idle."); + db.commit(sid); + TEST_FALSE(db.idle(sid), "Session is not idle (since committed != idle)."); +} + +{ + std::string sid = db.newSessionId(); + db.commitTransaction(sid, transaction, commit, macro, now); + TEST_FALSE(db.idle(sid), "Session should not be idle."); + db.nocommit(sid); + TEST_TRUE(db.idle(sid), "Session is idle."); +} + +{ + std::string sid = db.newSessionId(); + db.commitTransaction(sid, transaction, commit, macro, now); + TEST_FALSE(db.idle(sid), "Session should not be idle."); + db.discard(sid); + TEST_FALSE(db.idle(sid), "Session not idle (it doesn't exist)."); +} + +TEST_FALSE(db.idle("no such session"), "Missing session is not idle."); + /* time_t now = time(NULL); diff --git a/server/src/pracrodaotest.h b/server/src/pracrodaotest.h index 981b6d5..ac7ea2e 100644 --- a/server/src/pracrodaotest.h +++ b/server/src/pracrodaotest.h @@ -100,13 +100,15 @@ public: void delFieldname(std::string name); std::vector<Fieldname> getFieldnames(); - void commit(std::string sessionid) {} - void nocommit(std::string sessionid) {} - void discard(std::string sessionid) {} + void commit(std::string sessionid); + void nocommit(std::string sessionid); + void discard(std::string sessionid); + std::string serialise() { return ""; } void restore(const std::string &data) {} - bool active(std::string sessionid); - void setActive(std::string sessionid, bool active); + + bool idle(std::string sessionid); + void setIdle(std::string sessionid, bool idle); private: Data data; diff --git a/server/src/session.cc b/server/src/session.cc index 6880e73..24a9c12 100644 --- a/server/src/session.cc +++ b/server/src/session.cc @@ -84,23 +84,23 @@ void Session::unlock() mutex.unlock(); } -bool Session::active() +bool Session::idle() { - if(isreadonly) return true; + if(isreadonly) return false; { AutoBorrower<Database*> borrower(env->dbpool); Database *db = borrower.get(); - return db->active(id()); + return db->idle(id()); } } -void Session::setActive(bool a) +void Session::setIdle(bool idle) { if(isreadonly == false) { AutoBorrower<Database*> borrower(env->dbpool); Database *db = borrower.get(); - return db->setActive(id(), a); + return db->setIdle(id(), idle); } } @@ -185,7 +185,7 @@ Session *Sessions::newSession(std::string patientid, std::string templ) if(i->second->patientid == patientid && i->second->templ == templ) { Session *session = i->second; - if(session->active()) throw SessionAlreadyActive(session->id()); + if(!session->idle()) throw SessionAlreadyActive(session->id()); return session; } @@ -197,7 +197,7 @@ Session *Sessions::newSession(std::string patientid, std::string templ) Session *session = ser.findFromTupple(patientid, templ); if(session) { sessions[session->id()] = session; - if(session->active()) throw SessionAlreadyActive(session->id()); + if(!session->idle()) throw SessionAlreadyActive(session->id()); return session; } } diff --git a/server/src/session.h b/server/src/session.h index 8eace95..533f7a0 100644 --- a/server/src/session.h +++ b/server/src/session.h @@ -58,8 +58,8 @@ public: std::string patientid; std::string templ; - bool active(); - void setActive(bool active); + bool idle(); + void setIdle(bool idle); bool isreadonly; |