changeset 1244:a0e420c5f2b8

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Dec 2014 12:31:35 +0100
parents 3a3e7e3e244f
children aea9277dee75
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ExportedResource.h OrthancServer/ServerIndex.cpp
diffstat 4 files changed, 53 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Mon Dec 08 10:10:35 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Mon Dec 08 12:31:35 2014 +0100
@@ -619,6 +619,8 @@
                                            SQLite::Statement& s,
                                            unsigned int maxResults)
   {
+    target.clear();
+
     while (target.size() < maxResults && s.Step())
     {
       int64_t seq = s.ColumnInt64(0);
@@ -680,15 +682,14 @@
   }
 
 
-  void DatabaseWrapper::GetExportedResourcesInternal(Json::Value& target,
+  void DatabaseWrapper::GetExportedResourcesInternal(std::list<ExportedResource>& target,
+                                                     bool& done,
                                                      SQLite::Statement& s,
-                                                     int64_t since,
                                                      unsigned int maxResults)
   {
-    Json::Value changes = Json::arrayValue;
-    int64_t last = since;
+    target.clear();
 
-    while (changes.size() < maxResults && s.Step())
+    while (target.size() < maxResults && s.Step())
     {
       int64_t seq = s.ColumnInt64(0);
       ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(1));
@@ -704,47 +705,15 @@
                                 s.ColumnString(6),  // series instance UID
                                 s.ColumnString(7)); // sop instance UID
 
-      Json::Value item = Json::objectValue;
-      item["Seq"] = static_cast<int>(seq);
-      item["ResourceType"] = EnumerationToString(resourceType);
-      item["ID"] = publicId;
-      item["Path"] = GetBasePath(resourceType, publicId);
-      item["RemoteModality"] = s.ColumnString(3);
-      item["Date"] = s.ColumnString(8);
-
-      // WARNING: Do not add "break" below and do not reorder the case items!
-      switch (resourceType)
-      {
-      case ResourceType_Instance:
-        item["SopInstanceUid"] = s.ColumnString(7);
-
-      case ResourceType_Series:
-        item["SeriesInstanceUid"] = s.ColumnString(6);
-
-      case ResourceType_Study:
-        item["StudyInstanceUid"] = s.ColumnString(5);
-
-      case ResourceType_Patient:
-        item["PatientId"] = s.ColumnString(4);
-        break;
-
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-      }
-
-      last = seq;
-
-      changes.append(item);
+      target.push_back(resource);
     }
 
-    target = Json::objectValue;
-    target["Exports"] = changes;
-    target["Done"] = !(changes.size() == maxResults && s.Step());
-    target["Last"] = static_cast<int>(last);
+    done = !(target.size() == maxResults && s.Step());
   }
 
 
-  void DatabaseWrapper::GetExportedResources(Json::Value& target,
+  void DatabaseWrapper::GetExportedResources(std::list<ExportedResource>& target,
+                                             bool& done,
                                              int64_t since,
                                              unsigned int maxResults)
   {
@@ -752,15 +721,16 @@
                         "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?");
     s.BindInt64(0, since);
     s.BindInt(1, maxResults + 1);
-    GetExportedResourcesInternal(target, s, since, maxResults);
+    GetExportedResourcesInternal(target, done, s, maxResults);
   }
 
     
-  void DatabaseWrapper::GetLastExportedResource(Json::Value& target)
+  void DatabaseWrapper::GetLastExportedResource(std::list<ExportedResource>& target)
   {
+    bool done;  // Ignored
     SQLite::Statement s(db_, SQLITE_FROM_HERE, 
                         "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1");
-    GetExportedResourcesInternal(target, s, 0, 1);
+    GetExportedResourcesInternal(target, done, s, 1);
   }
 
 
--- a/OrthancServer/DatabaseWrapper.h	Mon Dec 08 10:10:35 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Mon Dec 08 12:31:35 2014 +0100
@@ -69,9 +69,9 @@
                             SQLite::Statement& s,
                             unsigned int maxResults);
 
-    void GetExportedResourcesInternal(Json::Value& target,
+    void GetExportedResourcesInternal(std::list<ExportedResource>& target,
+                                      bool& done,
                                       SQLite::Statement& s,
-                                      int64_t since,
                                       unsigned int maxResults);
 
   public:
@@ -158,11 +158,12 @@
                              const std::string& sopInstanceUid,
                              const boost::posix_time::ptime& date);
     
-    void GetExportedResources(Json::Value& target,
+    void GetExportedResources(std::list<ExportedResource>& target /* out */,
+                              bool& done /* out */,
                               int64_t since,
                               unsigned int maxResults);
 
-    void GetLastExportedResource(Json::Value& target);
+    void GetLastExportedResource(std::list<ExportedResource>& target /* out */);
 
     uint64_t GetTotalCompressedSize();
     
--- a/OrthancServer/ExportedResource.h	Mon Dec 08 10:10:35 2014 +0100
+++ b/OrthancServer/ExportedResource.h	Mon Dec 08 12:31:35 2014 +0100
@@ -114,16 +114,16 @@
       switch (resourceType_)
       {
       case ResourceType_Instance:
-        item["SopInstanceUid"] = sopInstanceUid_;
+        item["SOPInstanceUID"] = sopInstanceUid_;
 
       case ResourceType_Series:
-        item["SeriesInstanceUid"] = seriesInstanceUid_;
+        item["SeriesInstanceUID"] = seriesInstanceUid_;
 
       case ResourceType_Study:
-        item["StudyInstanceUid"] = studyInstanceUid_;
+        item["StudyInstanceUID"] = studyInstanceUid_;
 
       case ResourceType_Patient:
-        item["PatientId"] = patientId_;
+        item["PatientID"] = patientId_;
         break;
 
       default:
--- a/OrthancServer/ServerIndex.cpp	Mon Dec 08 10:10:35 2014 +0100
+++ b/OrthancServer/ServerIndex.cpp	Mon Dec 08 12:31:35 2014 +0100
@@ -1038,14 +1038,16 @@
   }
 
 
-  static void FormatChanges(Json::Value& target,
-                            const std::list<ServerIndexChange>& changes,
-                            bool done,
-                            int64_t since)
+  template <typename T>
+  static void FormatLog(Json::Value& target,
+                        const std::list<T>& log,
+                        const std::string& name,
+                        bool done,
+                        int64_t since)
   {
     Json::Value items = Json::arrayValue;
-    for (std::list<ServerIndexChange>::const_iterator
-           it = changes.begin(); it != changes.end(); it++)
+    for (typename std::list<T>::const_iterator
+           it = log.begin(); it != log.end(); it++)
     {
       Json::Value item;
       it->Format(item);
@@ -1053,10 +1055,10 @@
     }
 
     target = Json::objectValue;
-    target["Changes"] = items;
+    target[name] = items;
     target["Done"] = done;
 
-    int64_t last = (changes.size() == 0 ? since : changes.back().GetSeq());
+    int64_t last = (log.size() == 0 ? since : log.back().GetSeq());
     target["Last"] = static_cast<int>(last);
   }
 
@@ -1073,7 +1075,7 @@
       db_->GetChanges(changes, done, since, maxResults);
     }
 
-    FormatChanges(target, changes, done, since);
+    FormatLog(target, changes, "Changes", done, since);
     return true;
   }
 
@@ -1087,7 +1089,7 @@
       db_->GetLastChange(changes);
     }
 
-    FormatChanges(target, changes, true, 0);
+    FormatLog(target, changes, "Changes", true, 0);
     return true;
   }
 
@@ -1170,15 +1172,29 @@
                                          int64_t since,
                                          unsigned int maxResults)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-    db_->GetExportedResources(target, since, maxResults);
+    std::list<ExportedResource> exported;
+    bool done;
+
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      db_->GetExportedResources(exported, done, since, maxResults);
+    }
+
+    FormatLog(target, exported, "Exports", done, since);
     return true;
   }
 
+
   bool ServerIndex::GetLastExportedResource(Json::Value& target)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-    db_->GetLastExportedResource(target);
+    std::list<ExportedResource> exported;
+
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      db_->GetLastExportedResource(exported);
+    }
+
+    FormatLog(target, exported, "Exports", true, 0);
     return true;
   }