changeset 192:c56dc32266e0

refactoring getfile
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 13 Nov 2012 11:29:43 +0100
parents bff0b77b02fa
children a1b9d1e1497b
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/PrepareDatabase2.sql OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 6 files changed, 45 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Tue Nov 13 11:29:43 2012 +0100
@@ -302,7 +302,7 @@
   }
 
   void DatabaseWrapper::AttachFile(int64_t id,
-                                   const std::string& name,
+                                   const std::string& contentName,
                                    const std::string& fileUuid,
                                    uint64_t compressedSize,
                                    uint64_t uncompressedSize,
@@ -310,7 +310,7 @@
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)");
     s.BindInt(0, id);
-    s.BindString(1, name);
+    s.BindString(1, contentName);
     s.BindString(2, fileUuid);
     s.BindInt(3, compressedSize);
     s.BindInt(4, uncompressedSize);
@@ -319,16 +319,16 @@
   }
 
   bool DatabaseWrapper::LookupFile(int64_t id,
-                                   const std::string& name,
+                                   const std::string& contentName,
                                    std::string& fileUuid,
                                    uint64_t& compressedSize,
                                    uint64_t& uncompressedSize,
                                    CompressionType& compressionType)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, 
-                        "SELECT uuid, compressedSize, uncompressedSize, compressionType FROM AttachedFiles WHERE id=? AND name=?");
+                        "SELECT uuid, compressedSize, uncompressedSize, compressionType FROM AttachedFiles WHERE id=? AND contentName=?");
     s.BindInt(0, id);
-    s.BindString(1, name);
+    s.BindString(1, contentName);
 
     if (!s.Step())
     {
--- a/OrthancServer/DatabaseWrapper.h	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Tue Nov 13 11:29:43 2012 +0100
@@ -96,22 +96,22 @@
                             const std::string& defaultValue = "");
 
     void AttachFile(int64_t id,
-                    const std::string& name,
+                    const std::string& contentName,
                     const std::string& fileUuid,
                     uint64_t compressedSize,
                     uint64_t uncompressedSize,
                     CompressionType compressionType);
 
     void AttachFile(int64_t id,
-                    const std::string& name,
+                    const std::string& contentName,
                     const std::string& fileUuid,
                     uint64_t fileSize)
     {
-      AttachFile(id, name, fileUuid, fileSize, fileSize, CompressionType_None);
+      AttachFile(id, contentName, fileUuid, fileSize, fileSize, CompressionType_None);
     }
 
     bool LookupFile(int64_t id,
-                    const std::string& name,
+                    const std::string& contentName,
                     std::string& fileUuid,
                     uint64_t& compressedSize,
                     uint64_t& uncompressedSize,
--- a/OrthancServer/OrthancRestApi.cpp	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Tue Nov 13 11:29:43 2012 +0100
@@ -571,14 +571,14 @@
       std::string fileUuid, contentType, filename;
       if (uri[2] == "file")
       {
-        existingResource = index_.GetDicomFile(fileUuid, uri[1]);
+        existingResource = index_.GetFile(fileUuid, uri[1], "dicom");
         contentType = "application/dicom";
         filename = fileUuid + ".dcm";
       }
       else if (uri[2] == "tags" ||
                uri[2] == "simplified-tags")
       {
-        existingResource = index_.GetJsonFile(fileUuid, uri[1]);
+        existingResource = index_.GetFile(fileUuid, uri[1], "json");
         contentType = "application/json";
         filename = fileUuid + ".json";
       }
@@ -642,7 +642,7 @@
                 uri[4] == "image-uint16"))))
     {
       std::string uuid;
-      existingResource = index_.GetDicomFile(uuid, uri[1]);
+      existingResource = index_.GetFile(uuid, uri[1], "dicom");
 
       std::string action = uri[2];
 
--- a/OrthancServer/PrepareDatabase2.sql	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/PrepareDatabase2.sql	Tue Nov 13 11:29:43 2012 +0100
@@ -27,12 +27,12 @@
 
 CREATE TABLE AttachedFiles(
        id INTEGER REFERENCES Resources(internalId) ON DELETE CASCADE,
-       name TEXT,
+       contentName TEXT,
        uuid TEXT,
        compressedSize INTEGER,
        uncompressedSize INTEGER,
        compressionType INTEGER,
-       PRIMARY KEY(id, name)
+       PRIMARY KEY(id, contentName)
        );              
 
 CREATE TABLE Changes(
--- a/OrthancServer/ServerIndex.cpp	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Tue Nov 13 11:29:43 2012 +0100
@@ -589,8 +589,8 @@
       }
 
       // Attach the files to the newly created instance
-      db2_->AttachFile(instance, "_dicom", fileUuid, uncompressedFileSize);
-      db2_->AttachFile(instance, "_json", jsonUuid, 0);  // TODO "0"
+      db2_->AttachFile(instance, "dicom", fileUuid, uncompressedFileSize);
+      db2_->AttachFile(instance, "json", jsonUuid, 0);  // TODO "0"
 
       // Attach the metadata
       db2_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, Toolbox::GetNowIsoString());
@@ -980,6 +980,26 @@
   }
 
 
+  bool ServerIndex::GetFile(std::string& fileUuid,
+                            const std::string& instanceUuid,
+                            const std::string& contentName)
+  {
+    if (contentName == "json")
+    {
+      return GetJsonFile(fileUuid, instanceUuid);
+    }
+    else if (contentName == "dicom")
+    {
+      return GetDicomFile(fileUuid, instanceUuid);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+  }
+
+
+
   void ServerIndex::GetAllUuids(Json::Value& target,
                                 ResourceType resourceType)
   {
--- a/OrthancServer/ServerIndex.h	Tue Nov 13 11:22:33 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Tue Nov 13 11:29:43 2012 +0100
@@ -121,6 +121,12 @@
                        const std::string& jsonUuid,
                        const std::string& remoteAet);
 
+    bool GetJsonFile(std::string& fileUuid,
+                     const std::string& instanceUuid);
+
+    bool GetDicomFile(std::string& fileUuid,
+                      const std::string& instanceUuid);
+
   public:
     ServerIndex(const std::string& storagePath);
 
@@ -156,11 +162,9 @@
     bool GetPatient(Json::Value& result,
                     const std::string& patientUuid);
 
-    bool GetJsonFile(std::string& fileUuid,
-                     const std::string& instanceUuid);
-
-    bool GetDicomFile(std::string& fileUuid,
-                      const std::string& instanceUuid);
+    bool GetFile(std::string& fileUuid,
+                 const std::string& instanceUuid,
+                 const std::string& contentName);
 
     void GetAllUuids(Json::Value& target,
                      ResourceType resourceType);