diff Plugins/Engine/OrthancPluginDatabase.cpp @ 1629:bad4772b605c

OrthancPluginErrorCode in database plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 18 Sep 2015 17:45:59 +0200
parents c17b1142caab
children ffd23c0104af
line wrap: on
line diff
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Sep 18 16:46:35 2015 +0200
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Sep 18 17:45:59 2015 +0200
@@ -35,6 +35,7 @@
 
 #include "../../Core/OrthancException.h"
 #include "../../Core/Logging.h"
+#include "PluginsEnumerations.h"
 
 #include <cassert>
 
@@ -224,9 +225,11 @@
     tmp.compressedSize = attachment.GetCompressedSize();
     tmp.compressedHash = attachment.GetCompressedMD5().c_str();
 
-    if (backend_.addAttachment(payload_, id, &tmp) != 0)
+    OrthancPluginErrorCode error = backend_.addAttachment(payload_, id, &tmp);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -234,27 +237,33 @@
   void OrthancPluginDatabase::AttachChild(int64_t parent,
                                           int64_t child)
   {
-    if (backend_.attachChild(payload_, parent, child) != 0)
+    OrthancPluginErrorCode error = backend_.attachChild(payload_, parent, child);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
 
   void OrthancPluginDatabase::ClearChanges()
   {
-    if (backend_.clearChanges(payload_) != 0)
+    OrthancPluginErrorCode error = backend_.clearChanges(payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
 
   void OrthancPluginDatabase::ClearExportedResources()
   {
-    if (backend_.clearExportedResources(payload_) != 0)
+    OrthancPluginErrorCode error = backend_.clearExportedResources(payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -264,9 +273,11 @@
   {
     int64_t id;
 
-    if (backend_.createResource(&id, payload_, publicId.c_str(), Convert(type)) != 0)
+    OrthancPluginErrorCode error = backend_.createResource(&id, payload_, publicId.c_str(), Convert(type));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return id;
@@ -276,9 +287,11 @@
   void OrthancPluginDatabase::DeleteAttachment(int64_t id,
                                                FileContentType attachment)
   {
-    if (backend_.deleteAttachment(payload_, id, static_cast<int32_t>(attachment)) != 0)
+    OrthancPluginErrorCode error = backend_.deleteAttachment(payload_, id, static_cast<int32_t>(attachment));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -286,18 +299,22 @@
   void OrthancPluginDatabase::DeleteMetadata(int64_t id,
                                              MetadataType type)
   {
-    if (backend_.deleteMetadata(payload_, id, static_cast<int32_t>(type)) != 0)
+    OrthancPluginErrorCode error = backend_.deleteMetadata(payload_, id, static_cast<int32_t>(type));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
 
   void OrthancPluginDatabase::DeleteResource(int64_t id)
   {
-    if (backend_.deleteResource(payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.deleteResource(payload_, id);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -329,9 +346,11 @@
   {
     ResetAnswers();
 
-    if (backend_.getAllPublicIds(GetContext(), payload_, Convert(resourceType)) != 0)
+    OrthancPluginErrorCode error = backend_.getAllPublicIds(GetContext(), payload_, Convert(resourceType));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     ForwardAnswers(target);
@@ -348,9 +367,12 @@
       // This extension is available since Orthanc 0.9.4
       ResetAnswers();
 
-      if (extensions_.getAllPublicIdsWithLimit(GetContext(), payload_, Convert(resourceType), since, limit) != 0)
+      OrthancPluginErrorCode error = extensions_.getAllPublicIdsWithLimit
+        (GetContext(), payload_, Convert(resourceType), since, limit);
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
 
       ForwardAnswers(target);
@@ -399,9 +421,11 @@
     answerDone_ = &done;
     done = false;
 
-    if (backend_.getChanges(GetContext(), payload_, since, maxResults) != 0)
+    OrthancPluginErrorCode error = backend_.getChanges(GetContext(), payload_, since, maxResults);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -411,9 +435,11 @@
   {
     ResetAnswers();
 
-    if (backend_.getChildrenInternalId(GetContext(), payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.getChildrenInternalId(GetContext(), payload_, id);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     ForwardAnswers(target);
@@ -425,9 +451,11 @@
   {
     ResetAnswers();
 
-    if (backend_.getChildrenPublicId(GetContext(), payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.getChildrenPublicId(GetContext(), payload_, id);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     ForwardAnswers(target);
@@ -444,9 +472,11 @@
     answerDone_ = &done;
     done = false;
 
-    if (backend_.getExportedResources(GetContext(), payload_, since, maxResults) != 0)
+    OrthancPluginErrorCode error = backend_.getExportedResources(GetContext(), payload_, since, maxResults);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -459,9 +489,11 @@
     answerChanges_ = &target;
     answerDone_ = &ignored;
 
-    if (backend_.getLastChange(GetContext(), payload_) != 0)
+    OrthancPluginErrorCode error = backend_.getLastChange(GetContext(), payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -474,9 +506,11 @@
     answerExportedResources_ = &target;
     answerDone_ = &ignored;
 
-    if (backend_.getLastExportedResource(GetContext(), payload_) != 0)
+    OrthancPluginErrorCode error = backend_.getLastExportedResource(GetContext(), payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -487,9 +521,11 @@
     ResetAnswers();
     answerDicomMap_ = &map;
 
-    if (backend_.getMainDicomTags(GetContext(), payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.getMainDicomTags(GetContext(), payload_, id);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -499,8 +535,14 @@
     ResetAnswers();
     std::string s;
 
-    if (backend_.getPublicId(GetContext(), payload_, resourceId) != 0 ||
-        !ForwardSingleAnswer(s))
+    OrthancPluginErrorCode error = backend_.getPublicId(GetContext(), payload_, resourceId);
+
+    if (error != OrthancPluginErrorCode_Success)
+    {
+      throw OrthancException(Plugins::Convert(error));
+    }
+    
+    if (!ForwardSingleAnswer(s))
     {
       throw OrthancException(ErrorCode_Plugin);
     }
@@ -513,9 +555,11 @@
   {
     uint64_t count;
 
-    if (backend_.getResourceCount(&count, payload_, Convert(resourceType)) != 0)
+    OrthancPluginErrorCode error = backend_.getResourceCount(&count, payload_, Convert(resourceType));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return count;
@@ -526,9 +570,11 @@
   {
     OrthancPluginResourceType type;
 
-    if (backend_.getResourceType(&type, payload_, resourceId) != 0)
+    OrthancPluginErrorCode error = backend_.getResourceType(&type, payload_, resourceId);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return Convert(type);
@@ -539,9 +585,11 @@
   {
     uint64_t size;
 
-    if (backend_.getTotalCompressedSize(&size, payload_) != 0)
+    OrthancPluginErrorCode error = backend_.getTotalCompressedSize(&size, payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return size;
@@ -552,9 +600,11 @@
   {
     uint64_t size;
 
-    if (backend_.getTotalUncompressedSize(&size, payload_) != 0)
+    OrthancPluginErrorCode error = backend_.getTotalUncompressedSize(&size, payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return size;
@@ -565,9 +615,11 @@
   {
     int32_t existing;
 
-    if (backend_.isExistingResource(&existing, payload_, internalId) != 0)
+    OrthancPluginErrorCode error = backend_.isExistingResource(&existing, payload_, internalId);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return (existing != 0);
@@ -578,9 +630,11 @@
   {
     int32_t isProtected;
 
-    if (backend_.isProtectedPatient(&isProtected, payload_, internalId) != 0)
+    OrthancPluginErrorCode error = backend_.isProtectedPatient(&isProtected, payload_, internalId);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return (isProtected != 0);
@@ -592,9 +646,11 @@
   {
     ResetAnswers();
 
-    if (backend_.listAvailableMetadata(GetContext(), payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.listAvailableMetadata(GetContext(), payload_, id);
+ 
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     if (type_ != _OrthancPluginDatabaseAnswerType_None &&
@@ -621,9 +677,11 @@
   {
     ResetAnswers();
 
-    if (backend_.listAvailableAttachments(GetContext(), payload_, id) != 0)
+    OrthancPluginErrorCode error = backend_.listAvailableAttachments(GetContext(), payload_, id);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     if (type_ != _OrthancPluginDatabaseAnswerType_None &&
@@ -655,9 +713,11 @@
     tmp.publicId = change.GetPublicId().c_str();
     tmp.date = change.GetDate().c_str();
 
-    if (backend_.logChange(payload_, &tmp) != 0)
+    OrthancPluginErrorCode error = backend_.logChange(payload_, &tmp);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -675,9 +735,11 @@
     tmp.seriesInstanceUid = resource.GetSeriesInstanceUid().c_str();
     tmp.sopInstanceUid = resource.GetSopInstanceUid().c_str();
 
-    if (backend_.logExportedResource(payload_, &tmp) != 0)
+    OrthancPluginErrorCode error = backend_.logExportedResource(payload_, &tmp);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -688,9 +750,12 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupAttachment(GetContext(), payload_, id, static_cast<int32_t>(contentType)))
+    OrthancPluginErrorCode error = backend_.lookupAttachment
+      (GetContext(), payload_, id, static_cast<int32_t>(contentType));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     if (type_ == _OrthancPluginDatabaseAnswerType_None)
@@ -715,10 +780,12 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupGlobalProperty(GetContext(), payload_, 
-                                      static_cast<int32_t>(property)))
+    OrthancPluginErrorCode error = backend_.lookupGlobalProperty
+      (GetContext(), payload_, static_cast<int32_t>(property));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return ForwardSingleAnswer(target);
@@ -736,9 +803,11 @@
     tmp.element = tag.GetElement();
     tmp.value = value.c_str();
 
-    if (backend_.lookupIdentifier(GetContext(), payload_, &tmp) != 0)
+    OrthancPluginErrorCode error = backend_.lookupIdentifier(GetContext(), payload_, &tmp);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     ForwardAnswers(target);
@@ -750,9 +819,11 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupIdentifier2(GetContext(), payload_, value.c_str()) != 0)
+    OrthancPluginErrorCode error = backend_.lookupIdentifier2(GetContext(), payload_, value.c_str());
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     ForwardAnswers(target);
@@ -765,9 +836,11 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupMetadata(GetContext(), payload_, id, static_cast<int32_t>(type)))
+    OrthancPluginErrorCode error = backend_.lookupMetadata(GetContext(), payload_, id, static_cast<int32_t>(type));
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return ForwardSingleAnswer(target);
@@ -779,9 +852,11 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupParent(GetContext(), payload_, resourceId))
+    OrthancPluginErrorCode error = backend_.lookupParent(GetContext(), payload_, resourceId);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return ForwardSingleAnswer(parentId);
@@ -794,9 +869,11 @@
   {
     ResetAnswers();
 
-    if (backend_.lookupResource(GetContext(), payload_, publicId.c_str()))
+    OrthancPluginErrorCode error = backend_.lookupResource(GetContext(), payload_, publicId.c_str());
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     if (type_ == _OrthancPluginDatabaseAnswerType_None)
@@ -821,9 +898,11 @@
   {
     ResetAnswers();
 
-    if (backend_.selectPatientToRecycle(GetContext(), payload_))
+    OrthancPluginErrorCode error = backend_.selectPatientToRecycle(GetContext(), payload_);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return ForwardSingleAnswer(internalId);
@@ -835,9 +914,11 @@
   {
     ResetAnswers();
 
-    if (backend_.selectPatientToRecycle2(GetContext(), payload_, patientIdToAvoid))
+    OrthancPluginErrorCode error = backend_.selectPatientToRecycle2(GetContext(), payload_, patientIdToAvoid);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
 
     return ForwardSingleAnswer(internalId);
@@ -847,10 +928,12 @@
   void OrthancPluginDatabase::SetGlobalProperty(GlobalProperty property,
                                                 const std::string& value)
   {
-    if (backend_.setGlobalProperty(payload_, static_cast<int32_t>(property), 
-                                   value.c_str()) != 0)
+    OrthancPluginErrorCode error = backend_.setGlobalProperty
+      (payload_, static_cast<int32_t>(property), value.c_str());
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -859,24 +942,25 @@
                                               const DicomTag& tag,
                                               const std::string& value)
   {
-    int32_t status;
     OrthancPluginDicomTag tmp;
     tmp.group = tag.GetGroup();
     tmp.element = tag.GetElement();
     tmp.value = value.c_str();
 
+    OrthancPluginErrorCode error;
+
     if (tag.IsIdentifier())
     {
-      status = backend_.setIdentifierTag(payload_, id, &tmp);
+      error = backend_.setIdentifierTag(payload_, id, &tmp);
     }
     else
     {
-      status = backend_.setMainDicomTag(payload_, id, &tmp);
+      error = backend_.setMainDicomTag(payload_, id, &tmp);
     }
 
-    if (status != 0)
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -885,10 +969,12 @@
                                           MetadataType type,
                                           const std::string& value)
   {
-    if (backend_.setMetadata(payload_, id, static_cast<int32_t>(type), 
-                             value.c_str()) != 0)
+    OrthancPluginErrorCode error = backend_.setMetadata
+      (payload_, id, static_cast<int32_t>(type), value.c_str());
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -896,9 +982,11 @@
   void OrthancPluginDatabase::SetProtectedPatient(int64_t internalId, 
                                                   bool isProtected)
   {
-    if (backend_.setProtectedPatient(payload_, internalId, isProtected) != 0)
+    OrthancPluginErrorCode error = backend_.setProtectedPatient(payload_, internalId, isProtected);
+
+    if (error != OrthancPluginErrorCode_Success)
     {
-      throw OrthancException(ErrorCode_Plugin);
+      throw OrthancException(Plugins::Convert(error));
     }
   }
 
@@ -919,25 +1007,31 @@
 
     virtual void Begin()
     {
-      if (backend_.startTransaction(payload_) != 0)
+      OrthancPluginErrorCode error = backend_.startTransaction(payload_);
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
     }
 
     virtual void Rollback()
     {
-      if (backend_.rollbackTransaction(payload_) != 0)
+      OrthancPluginErrorCode error = backend_.rollbackTransaction(payload_);
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
     }
 
     virtual void Commit()
     {
-      if (backend_.commitTransaction(payload_) != 0)
+      OrthancPluginErrorCode error = backend_.commitTransaction(payload_);
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
     }
   };
@@ -988,9 +1082,11 @@
     if (extensions_.getDatabaseVersion != NULL)
     {
       uint32_t version;
-      if (extensions_.getDatabaseVersion(&version, payload_) != 0)
+      OrthancPluginErrorCode error = extensions_.getDatabaseVersion(&version, payload_);
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
 
       return version;
@@ -1010,11 +1106,13 @@
   {
     if (extensions_.upgradeDatabase != NULL)
     {
-      if (extensions_.upgradeDatabase(
-            payload_, targetVersion, 
-            reinterpret_cast<OrthancPluginStorageArea*>(&storageArea)) != 0)
+      OrthancPluginErrorCode error = extensions_.upgradeDatabase(
+        payload_, targetVersion, 
+        reinterpret_cast<OrthancPluginStorageArea*>(&storageArea));
+
+      if (error != OrthancPluginErrorCode_Success)
       {
-        throw OrthancException(ErrorCode_Plugin);
+        throw OrthancException(Plugins::Convert(error));
       }
     }
   }