From 05182c2214c5983c8ce7376c42805ad9802c75e5 Mon Sep 17 00:00:00 2001
From: Bent Bisballe Nyeng <deva@aasimon.org>
Date: Thu, 9 Aug 2012 12:04:43 +0200
Subject: Added session files to admin session list.

---
 server/src/admin_connection.cc    | 15 +++++++++++
 server/src/sessionheaderparser.cc | 22 +++++++++-------
 server/src/sessionheaderparser.h  | 13 +++++++---
 server/src/sessionserialiser.cc   | 53 +++++++++++++++++++++++++++++++++++----
 server/src/sessionserialiser.h    |  5 ++++
 5 files changed, 90 insertions(+), 18 deletions(-)

diff --git a/server/src/admin_connection.cc b/server/src/admin_connection.cc
index 1522627..76c88ea 100644
--- a/server/src/admin_connection.cc
+++ b/server/src/admin_connection.cc
@@ -35,6 +35,7 @@
 #include "debug.h"
 
 #include "configuration.h"
+#include "sessionserialiser.h"
 
 static std::string admin_sessionunlock(Environment &env, std::string id)
 {
@@ -67,6 +68,20 @@ static std::string admin_listactivesessions(Environment &env)
     i++;
   }
 
+  SessionSerialiser ser(&env, Conf::session_path);
+  std::map<std::string, SessionHeaderParser::Header> files = ser.sessionFiles();
+
+  std::map<std::string, SessionHeaderParser::Header>::iterator j = files.begin();
+  while(j != files.end()) {
+    std::string file = j->first;
+    SessionHeaderParser::Header header = j->second;
+
+    str += "Session " + header.id + ": " + header.templ + " on "
+      + header.patientid + " [session file: " + file + "]\n";
+
+    j++;
+  }
+
   return str;
 }
 
diff --git a/server/src/sessionheaderparser.cc b/server/src/sessionheaderparser.cc
index 93bd80b..c9a8e67 100644
--- a/server/src/sessionheaderparser.cc
+++ b/server/src/sessionheaderparser.cc
@@ -107,8 +107,17 @@ void SessionHeaderParser::startTag(std::string name, attributes_t &attr)
 
   if(name == "session") {
     done = true;
-    if(attr.find("patientid") != attr.end()) patientid = attr["patientid"];
-    if(attr.find("template") != attr.end()) templ = attr["template"];
+    if(attr.find("patientid") != attr.end()) {
+      header.patientid = attr["patientid"];
+    }
+
+    if(attr.find("template") != attr.end()) {
+      header.templ = attr["template"];
+    }
+
+    if(attr.find("id") != attr.end()) {
+      header.id = attr["id"];
+    }
   } else {
     throw Exception("Missing root tag 'session' - found '" + name + "'");
   }
@@ -149,14 +158,9 @@ void SessionHeaderParser::parseError(const char *buf, size_t len, std::string er
   }
 }
 
-std::string SessionHeaderParser::getPatientID()
-{
-  return patientid;
-}
-
-std::string SessionHeaderParser::getTemplate()
+SessionHeaderParser::Header SessionHeaderParser::getHeader()
 {
-  return templ;
+  return header;
 }
 
 #ifdef TEST_SESSIONHEADERPARSER
diff --git a/server/src/sessionheaderparser.h b/server/src/sessionheaderparser.h
index a56215a..5f2d3cf 100644
--- a/server/src/sessionheaderparser.h
+++ b/server/src/sessionheaderparser.h
@@ -44,6 +44,13 @@
  */
 class SessionHeaderParser : public SAXParser {
 public:
+  class Header {
+  public:
+    std::string patientid;
+    std::string templ;
+    std::string id;
+  };
+
   /**
    * Constructor.
    * @param sessionfile A std::string containing the name of the file to parse.
@@ -71,8 +78,7 @@ public:
    * freed upon parser deletion.
    * @return A pointer to the macro or NULL on error.
    */
-  std::string getPatientID();
-  std::string getTemplate();
+  Header getHeader();
 
 protected:
   /**
@@ -85,8 +91,7 @@ private:
 
   bool done;
  
-  std::string patientid;
-  std::string templ;
+  Header header;
 
   std::string file;
   // Error callback function.
diff --git a/server/src/sessionserialiser.cc b/server/src/sessionserialiser.cc
index d5150ac..6288ddd 100644
--- a/server/src/sessionserialiser.cc
+++ b/server/src/sessionserialiser.cc
@@ -216,6 +216,11 @@ void SessionSerialiser::save(Session *session)
 
   // write xml to file
   FILE *fp =  fopen(filename.c_str(), "w");
+  if(!fp) {
+    ERR(sessionserialiser, "Could not write session to file %s\n",
+        filename.c_str());
+    return;
+  }
   fwrite(xml.data(), xml.size(), 1, fp);
   fclose(fp);
 }
@@ -256,19 +261,17 @@ Session *SessionSerialiser::findFromTupple(const std::string &patientid,
 
       DEBUG(sessionserialiser, "Is xml file\n");
 
-      std::string pid;
-      std::string tpl;
+      SessionHeaderParser::Header header;
 
       SessionHeaderParser p(filename);
       try {
         p.parse();
-        pid = p.getPatientID();
-        tpl = p.getTemplate();
+        header = p.getHeader();
       } catch( ... ) {
         continue;
       }
 
-      if(patientid == pid && templ == tpl) {
+      if(header.patientid == patientid && header.templ == templ) {
         // Load session file
         FILE *fp =  fopen(filename.c_str(), "r");
         std::string xml;
@@ -298,6 +301,46 @@ Session *SessionSerialiser::findFromTupple(const std::string &patientid,
   return NULL;
 }
 
+std::map<std::string, SessionHeaderParser::Header>
+SessionSerialiser::sessionFiles()
+{
+  std::map<std::string, SessionHeaderParser::Header> list;
+
+  DIR *dir = opendir(path.c_str());
+  if(!dir) {
+    ERR(sessionserialiser, "Could not open directory: %s - %s\n",
+        path.c_str(), strerror(errno));
+    return list;
+  }
+
+  struct dirent *dirent;
+  while( (dirent = readdir(dir)) != NULL ) {
+
+    std::string filename = path+"/"+dirent->d_name;
+    
+    if(isxmlfile(filename)) {
+
+      SessionHeaderParser::Header header;
+
+      SessionHeaderParser p(filename);
+      try {
+        p.parse();
+        header = p.getHeader();
+      } catch( ... ) {
+        continue;
+      }
+      
+      list[filename] = header;
+    }
+  }
+
+  closedir(dir);
+
+  return list;
+  
+}
+
+
 #ifdef TEST_SESSIONSERIALISER
 //deps: session.cc journal.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc xml_encode_decode.cc database.cc pracrodaopgsql.cc pracrodaotest.cc pracrodao.cc journal_uploadserver.cc log.cc environment.cc semaphore.cc artefact.cc macrolist.cc templatelist.cc entitylist.cc inotify.cc versionstr.cc exception.cc macroheaderparser.cc templateheaderparser.cc
 //cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) $(PQXX_CFLAGS) -DWITHOUT_ARTEFACT
diff --git a/server/src/sessionserialiser.h b/server/src/sessionserialiser.h
index a85fc63..2c4e7ea 100644
--- a/server/src/sessionserialiser.h
+++ b/server/src/sessionserialiser.h
@@ -29,6 +29,9 @@
 #define __PRACRO_SESSIONSERIALISER_H__
 
 #include <string>
+#include <map>
+
+#include "sessionheaderparser.h"
 
 #include "session.h"
 
@@ -47,6 +50,8 @@ public:
   Session *load(const std::string &sessionid);
   void save(Session *session);
 
+  std::map<std::string, SessionHeaderParser::Header> sessionFiles();
+
 private:
   std::string path;
   Environment *env;
-- 
cgit v1.2.3