summaryrefslogtreecommitdiff
path: root/server/src/session.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/session.cc')
-rw-r--r--server/src/session.cc105
1 files changed, 68 insertions, 37 deletions
diff --git a/server/src/session.cc b/server/src/session.cc
index 24a9c12..6850e9b 100644
--- a/server/src/session.cc
+++ b/server/src/session.cc
@@ -84,6 +84,16 @@ void Session::unlock()
mutex.unlock();
}
+void Session::commitMacro(Transaction &transaction, Commit &commit,
+ Macro &macro)
+{
+ AutoBorrower<Database*> borrower(env->dbpool);
+ Database *db = borrower.get();
+ db->commitTransaction(transaction, commit, macro, id());
+ isreadonly = false;
+}
+
+
bool Session::idle()
{
if(isreadonly) return false;
@@ -149,17 +159,7 @@ Journal *Session::journal()
}
return _journal;
}
-/*
-Database *Session::database()
-{
- if(_database == NULL) {
- _database =
- new Database(Conf::database_backend, Conf::database_addr, "",
- Conf::database_user, Conf::database_passwd, "");
- }
- return _database;
-}
-*/
+
Sessions::Sessions(Environment *e) : env(e)
{
}
@@ -185,7 +185,10 @@ Session *Sessions::newSession(std::string patientid, std::string templ)
if(i->second->patientid == patientid &&
i->second->templ == templ) {
Session *session = i->second;
- if(!session->idle()) throw SessionAlreadyActive(session->id());
+ if(!session->idle()) {
+ DEBUG(session, "Patient/template matched session is already active.");
+ throw SessionAlreadyActive(session->id());
+ }
return session;
}
@@ -195,9 +198,12 @@ Session *Sessions::newSession(std::string patientid, std::string templ)
{ // Look up patientid / template tupple in session files.
SessionSerialiser ser(env, Conf::session_path);
Session *session = ser.findFromTupple(patientid, templ);
- if(session) {
+ if(session != NULL) {
sessions[session->id()] = session;
- if(!session->idle()) throw SessionAlreadyActive(session->id());
+ if(!session->idle()) {
+ DEBUG(session, "Looked up session by id is already active.");
+ throw SessionAlreadyActive(session->id());
+ }
return session;
}
}
@@ -307,13 +313,19 @@ SessionAutolock::~SessionAutolock()
TEST_BEGIN;
+debug_parse("+all");
+
Conf::database_backend = "testdb";
Conf::database_poolsize = 1;
-Conf::xml_basedir = "/tmp";
+Conf::xml_basedir = "../xml";
Environment env;
-Conf::session_path = "/tmp";
+// Make sure we start out on an empty session directory.
+Conf::session_path = "/tmp/test_session";
+while(mkdir(Conf::session_path.c_str(), 0777) == -1 && errno == EEXIST) {
+ Conf::session_path += "X";
+}
Session *s1 = env.sessions.newSession(PID, TMPL);
@@ -340,37 +352,56 @@ TEST_EQUAL_INT(env.sessions.size(), 0, "Testing if size is 0.");
Session *s5 = env.sessions.newSession(PID, TMPL);
TEST_NOTEQUAL(s5, NULL, "A new session was created.");
-s5->isreadonly = false;
+{
+ Transaction transaction;
+ transaction.cpr = PID;
+ transaction.user = "me";
+
+ Commit commit;
+ commit.fields["field1"] = "hello";
+ commit.fields["field2"] = "world";
+ commit.templ = TMPL;
+
+ Macro macro;
+ macro.attributes["version"] = "1.0";
+ macro.attributes["name"] = "somemacro";
+
+ s5->commitMacro(transaction, commit, macro);
+}
+id = s5->id();
env.sessions.store();
+// Resume session using session id.
Session *s6 = env.sessions.session(id);
-TEST_EQUAL_STR(s6->id(), s5->id(), "Did we get the stored session?");
+TEST_NOTEQUAL(s6, NULL, "We did get one right?");
+TEST_EQUAL_STR(s6->id(), id, "Did we get the stored session?");
-s6->isreadonly = false;
+{
+ Transaction transaction;
+ transaction.cpr = PID;
+ transaction.user = "me";
+
+ Commit commit;
+ commit.fields["field1"] = "hello";
+ commit.fields["field2"] = "world";
+ commit.templ = TMPL;
+
+ Macro macro;
+ macro.attributes["version"] = "1.0";
+ macro.attributes["name"] = "somemacro";
+
+ s6->commitMacro(transaction, commit, macro);
+}
+s6->nocommit();
env.sessions.store();
+// Resume session using patientid/template tupple.
Session *s7 = env.sessions.newSession(PID, TMPL);
-TEST_EQUAL_STR(s7->id(), s5->id(), "Did we get the stored session?");
-
-/*
-std::string sessionid = s1->id();
-SessionSerialiser ser(&env, Conf::session_path);
-ser.save(s1);
-
-sessions.deleteSession(sessionid);
-TEST_EQUAL(sessions.size(), 1, "Testing if size match.");
-
-s1 = sessions.session(sessionid);
-TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?");
-
-sessions.store();
-TEST_EQUAL(sessions.size(), 0, "Testing if size match.");
+TEST_NOTEQUAL(s7, NULL, "We did get one right?");
+TEST_EQUAL_STR(s7->id(), id, "Did we get the stored session?");
-s1 = sessions.session(sessionid);
-TEST_NOTEQUAL(s1, NULL, "Did we reload the session from disk?");
-*/
TEST_END;
#endif/*TEST_SESSION*/