changeset 198:663cc6c46d0a

before refactoring of ServerIndex::GetXXX
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Nov 2012 15:49:42 +0100
parents 530a25320461
children dfa2899d9960
files Core/Enumerations.h Core/OrthancException.cpp OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h UnitTests/ServerIndex.cpp
diffstat 8 files changed, 194 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.h	Tue Nov 27 14:59:55 2012 +0100
+++ b/Core/Enumerations.h	Tue Nov 27 15:49:42 2012 +0100
@@ -52,7 +52,8 @@
     ErrorCode_InexistentFile,
     ErrorCode_CannotWriteFile,
     ErrorCode_BadFileFormat,
-    ErrorCode_Timeout
+    ErrorCode_Timeout,
+    ErrorCode_UnknownResource
   };
 
   enum PixelFormat
--- a/Core/OrthancException.cpp	Tue Nov 27 14:59:55 2012 +0100
+++ b/Core/OrthancException.cpp	Tue Nov 27 15:49:42 2012 +0100
@@ -84,6 +84,9 @@
     case ErrorCode_Timeout:
       return "Timeout";
 
+    case ErrorCode_UnknownResource:
+      return "Unknown resource";
+
     case ErrorCode_Custom:
     default:
       return "???";
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Nov 27 14:59:55 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Tue Nov 27 15:49:42 2012 +0100
@@ -231,6 +231,43 @@
     }
   }
 
+  bool DatabaseWrapper::LookupParent(int64_t& parentId,
+                                     int64_t resourceId)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT parentId FROM Resources WHERE internalId=?");
+    s.BindInt(0, resourceId);
+
+    if (!s.Step())
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    if (s.ColumnIsNull(0))
+    {
+      return false;
+    }
+    else
+    {
+      parentId = s.ColumnInt(0);
+      return true;
+    }
+  }
+
+  std::string DatabaseWrapper::GetPublicId(int64_t resourceId)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT publicId FROM Resources WHERE internalId=?");
+    s.BindInt(0, resourceId);
+    
+    if (!s.Step())
+    { 
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    return s.ColumnString(0);
+  }
+
   void DatabaseWrapper::AttachChild(int64_t parent,
                                     int64_t child)
   {
--- a/OrthancServer/DatabaseWrapper.h	Tue Nov 27 14:59:55 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Tue Nov 27 15:49:42 2012 +0100
@@ -78,6 +78,11 @@
                         int64_t& id,
                         ResourceType& type);
 
+    bool LookupParent(int64_t& parentId,
+                      int64_t resourceId);
+
+    std::string GetPublicId(int64_t resourceId);
+
     void AttachChild(int64_t parent,
                      int64_t child);
 
--- a/OrthancServer/OrthancRestApi.cpp	Tue Nov 27 14:59:55 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Tue Nov 27 15:49:42 2012 +0100
@@ -514,6 +514,7 @@
         if (uri[0] == "patients")
         {
           existingResource = index_.GetPatient(result, uri[1]);
+          assert(result["Type"] == "Patient");
         }
         else if (uri[0] == "studies")
         {
--- a/OrthancServer/ServerIndex.cpp	Tue Nov 27 14:59:55 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Tue Nov 27 15:49:42 2012 +0100
@@ -787,6 +787,121 @@
   }
 
 
+  void ServerIndex::MainDicomTagsToJson2(Json::Value& target,
+                                         int64_t resourceId)
+  {
+    DicomMap tags;
+    db2_->GetMainDicomTags(tags, resourceId);
+    target["MainDicomTags"] = Json::objectValue;
+    FromDcmtkBridge::ToJson(target["MainDicomTags"], tags);
+  }
+
+  bool ServerIndex::LookupResource(Json::Value& result,
+                                   const std::string& publicId)
+  {
+    result = Json::objectValue;
+
+    // Lookup for the requested resource
+    int64_t id;
+    ResourceType type;
+    if (!db2_->LookupResource(publicId, id, type))
+    {
+      return false;
+    }
+
+    // Find the parent resource (if it exists)
+    if (type != ResourceType_Patient)
+    {
+      int64_t parentId;
+      if (!db2_->LookupParent(parentId, id))
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      std::string parent = db2_->GetPublicId(parentId);
+
+      switch (type)
+      {
+      case ResourceType_Study:
+        result["ParentPatient"] = parent;
+        break;
+
+      case ResourceType_Series:
+        result["ParentStudy"] = parent;
+        break;
+
+      case ResourceType_Instance:
+        result["ParentSeries"] = parent;
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+
+    // List the children resources
+    std::list<std::string> children;
+    db2_->GetChildrenPublicId(children, id);
+
+    if (type != ResourceType_Instance)
+    {
+      Json::Value c = Json::arrayValue;
+
+      for (std::list<std::string>::const_iterator
+             it = children.begin(); it != children.end(); it++)
+      {
+        c.append(*it);
+      }
+
+      switch (type)
+      {
+      case ResourceType_Patient:
+        result["Studies"] = c;
+        break;
+
+      case ResourceType_Study:
+        result["Series"] = c;
+        break;
+
+      case ResourceType_Series:
+        result["Instances"] = c;
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+
+    // Set the resource type
+    switch (type)
+    {
+    case ResourceType_Patient:
+      result["Type"] = "Patient";
+      break;
+
+    case ResourceType_Study:
+      result["Type"] = "Study";
+      break;
+
+    case ResourceType_Series:
+      result["Type"] = "Series";
+      break;
+
+    case ResourceType_Instance:
+      result["Type"] = "Instance";
+      break;
+
+    default:
+      throw OrthancException(ErrorCode_InternalError);
+    }
+
+    // Record the remaining information
+    result["ID"] = publicId;
+    MainDicomTagsToJson2(result, id);
+
+    return true;
+  }
+
 
   bool ServerIndex::GetInstance(Json::Value& result,
                                 const std::string& instanceUuid)
@@ -817,6 +932,8 @@
         result["IndexInSeries"] = s.ColumnInt(4);
       }
 
+      result["Type"] = "Instance";
+
       return true;
     }
   }
@@ -880,6 +997,8 @@
       break;
     }
 
+    result["Type"] = "Series";
+
     return true;
   }
 
@@ -910,6 +1029,7 @@
     }
       
     result["Series"] = series;
+    result["Type"] = "Study";
     return true;
   }
 
@@ -939,7 +1059,10 @@
     }
       
     result["Studies"] = studies;
+    result["Type"] = "Patient";
     return true;
+
+    //return LookupResource(result, patientUuid);
   }
 
 
--- a/OrthancServer/ServerIndex.h	Tue Nov 27 14:59:55 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Tue Nov 27 15:49:42 2012 +0100
@@ -111,6 +111,9 @@
     void MainDicomTagsToJson(Json::Value& target,
                              const std::string& uuid);
 
+    void MainDicomTagsToJson2(Json::Value& result,
+                              int64_t resourceId);
+
     bool DeleteInternal(Json::Value& target,
                         const std::string& uuid,
                         const std::string& tableName);
@@ -121,6 +124,9 @@
                        const std::string& jsonUuid,
                        const std::string& remoteAet);
 
+    bool LookupResource(Json::Value& result,
+                        const std::string& publicId);
+
   public:
     ServerIndex(const std::string& storagePath);
 
--- a/UnitTests/ServerIndex.cpp	Tue Nov 27 14:59:55 2012 +0100
+++ b/UnitTests/ServerIndex.cpp	Tue Nov 27 15:49:42 2012 +0100
@@ -54,6 +54,14 @@
     index.CreateResource("g", ResourceType_Study)      // 6
   };
 
+  ASSERT_EQ("a", index.GetPublicId(a[0]));
+  ASSERT_EQ("b", index.GetPublicId(a[1]));
+  ASSERT_EQ("c", index.GetPublicId(a[2]));
+  ASSERT_EQ("d", index.GetPublicId(a[3]));
+  ASSERT_EQ("e", index.GetPublicId(a[4]));
+  ASSERT_EQ("f", index.GetPublicId(a[5]));
+  ASSERT_EQ("g", index.GetPublicId(a[6]));
+
   {
     Json::Value t;
     index.GetAllPublicIds(t, ResourceType_Patient);
@@ -80,6 +88,15 @@
   index.AttachChild(a[2], a[4]);
   index.AttachChild(a[6], a[5]);
 
+  int64_t parent;
+  ASSERT_FALSE(index.LookupParent(parent, a[0]));
+  ASSERT_TRUE(index.LookupParent(parent, a[1])); ASSERT_EQ(a[0], parent);
+  ASSERT_TRUE(index.LookupParent(parent, a[2])); ASSERT_EQ(a[1], parent);
+  ASSERT_TRUE(index.LookupParent(parent, a[3])); ASSERT_EQ(a[2], parent);
+  ASSERT_TRUE(index.LookupParent(parent, a[4])); ASSERT_EQ(a[2], parent);
+  ASSERT_TRUE(index.LookupParent(parent, a[5])); ASSERT_EQ(a[6], parent);
+  ASSERT_FALSE(index.LookupParent(parent, a[6]));
+
   std::string s;
   
   ASSERT_FALSE(index.GetParentPublicId(s, a[0]));