diff OrthancServer/ServerJobs/ResourceModificationJob.cpp @ 2868:abce036683cd

sharing code within OrthancRestAnonymizeModify
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Oct 2018 17:05:19 +0200
parents ff0ed5ea9e4e
children bbfd95a0c429
line wrap: on
line diff
--- a/OrthancServer/ServerJobs/ResourceModificationJob.cpp	Mon Oct 08 16:08:51 2018 +0200
+++ b/OrthancServer/ServerJobs/ResourceModificationJob.cpp	Mon Oct 08 17:05:19 2018 +0200
@@ -39,88 +39,100 @@
 
 namespace Orthanc
 {
-  ResourceModificationJob::Output::Output(ResourceType  level) :
-    level_(level),
-    isFirst_(true)
+  class ResourceModificationJob::Output : public boost::noncopyable
   {
-    if (level_ != ResourceType_Patient &&
-        level_ != ResourceType_Study &&
-        level_ != ResourceType_Series)
+  private:
+    ResourceType  level_;
+    bool          isFirst_;
+    std::string   id_;
+    std::string   patientId_;
+
+  public:
+    Output(ResourceType level) :
+      level_(level),
+      isFirst_(true)
+    {
+      if (level_ != ResourceType_Patient &&
+          level_ != ResourceType_Study &&
+          level_ != ResourceType_Series)
+      {
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }            
+    }
+
+    ResourceType GetLevel() const
     {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }            
-  }
+      return level_;
+    }
+    
+
+    void Update(DicomInstanceHasher& hasher)
+    {
+      if (isFirst_)
+      {
+        switch (level_)
+        {
+          case ResourceType_Series:
+            id_ = hasher.HashSeries();
+            break;
+
+          case ResourceType_Study:
+            id_ = hasher.HashStudy();
+            break;
+
+          case ResourceType_Patient:
+            id_ = hasher.HashPatient();
+            break;
+
+          default:
+            throw OrthancException(ErrorCode_InternalError);
+        }
+
+        patientId_ = hasher.HashPatient();
+        isFirst_ = false;
+      }
+    }
 
 
-  void ResourceModificationJob::Output::Update(DicomInstanceHasher& hasher)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-        
-    if (isFirst_)
+    bool Format(Json::Value& target)
     {
-      switch (level_)
+      if (isFirst_)
       {
-        case ResourceType_Series:
-          id_ = hasher.HashSeries();
-          break;
-
-        case ResourceType_Study:
-          id_ = hasher.HashStudy();
-          break;
-
-        case ResourceType_Patient:
-          id_ = hasher.HashPatient();
-          break;
-
-        default:
-          throw OrthancException(ErrorCode_InternalError);
+        return false;
       }
-
-      patientId_ = hasher.HashPatient();
-      isFirst_ = false;
+      else
+      {
+        target = Json::objectValue;
+        target["Type"] = EnumerationToString(level_);
+        target["ID"] = id_;
+        target["Path"] = GetBasePath(level_, id_);
+        target["PatientID"] = patientId_;
+        return true;
+      }
     }
-  }
-
-
-  bool ResourceModificationJob::Output::Format(Json::Value& target)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-        
-    if (isFirst_)
-    {
-      return false;
-    }
-    else
-    {
-      target = Json::objectValue;
-      target["Type"] = EnumerationToString(level_);
-      target["ID"] = id_;
-      target["Path"] = GetBasePath(level_, id_);
-      target["PatientID"] = patientId_;
-      return true;
-    }
-  }
 
   
-  bool ResourceModificationJob::Output::GetIdentifier(std::string& id)
-  {
-    boost::mutex::scoped_lock lock(mutex_);
-        
-    if (isFirst_)
+    bool GetIdentifier(std::string& id)
     {
-      return false;
+      if (isFirst_)
+      {
+        return false;
+      }
+      else
+      {
+        id = id_;
+        return true;
+      }
     }
-    else
-    {
-      id = id_;
-      return true;
-    }
-  }
+  };
+    
+
 
 
   bool ResourceModificationJob::HandleInstance(const std::string& instance)
   {
-    if (modification_.get() == NULL)
+    if (modification_.get() == NULL ||
+        output_.get() == NULL)
     {
       LOG(ERROR) << "No modification was provided for this job";
       throw OrthancException(ErrorCode_BadSequenceOfCalls);
@@ -204,14 +216,9 @@
       throw OrthancException(ErrorCode_CannotStoreInstance);
     }
 
-    // Sanity checks in debug mode
     assert(modifiedInstance == modifiedHasher.HashInstance());
 
-
-    if (output_.get() != NULL)
-    {
-      output_->Update(modifiedHasher);
-    }
+    output_->Update(modifiedHasher);
 
     return true;
   }
@@ -224,6 +231,7 @@
 
 
   void ResourceModificationJob::SetModification(DicomModification* modification,
+                                                ResourceType level,
                                                 bool isAnonymization)
   {
     if (modification == NULL)
@@ -237,24 +245,12 @@
     else
     {
       modification_.reset(modification);
+      output_.reset(new Output(level));
       isAnonymization_ = isAnonymization;
     }
   }
 
 
-  void ResourceModificationJob::SetOutput(boost::shared_ptr<Output>& output)
-  {
-    if (IsStarted())
-    {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);
-    }
-    else
-    {
-      output_ = output;
-    }
-  }
-
-  
   void ResourceModificationJob::SetOrigin(const DicomInstanceOrigin& origin)
   {
     if (IsStarted())
@@ -292,6 +288,11 @@
     SetOfInstancesJob::GetPublicContent(value);
 
     value["IsAnonymization"] = isAnonymization_;
+
+    if (output_.get() != NULL)
+    {
+      output_->Format(value);
+    }
   }