changeset 1067:ace99e272203

access metadata in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Jul 2014 11:09:03 +0200
parents bb82e5e818e9
children f54a9c6fea5d
files OrthancServer/ServerContext.cpp Plugins/Engine/PluginsHttpHandler.cpp Plugins/OrthancCPlugin/OrthancCPlugin.h Plugins/Samples/Basic/Plugin.c
diffstat 4 files changed, 146 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/ServerContext.cpp	Fri Jul 25 18:39:02 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Mon Jul 28 11:09:03 2014 +0200
@@ -306,10 +306,20 @@
       attachments.push_back(dicomInfo);
       attachments.push_back(jsonInfo);
 
-      std::map<MetadataType, std::string> instanceMetadata;
+      typedef std::map<MetadataType, std::string>  InstanceMetadata;
+      InstanceMetadata  instanceMetadata;
       StoreStatus status = index_.Store(instanceMetadata, dicom.GetSummary(), attachments, 
                                         dicom.GetRemoteAet(), dicom.GetMetadata());
 
+      dicom.GetMetadata().clear();
+
+      for (InstanceMetadata::const_iterator it = instanceMetadata.begin();
+           it != instanceMetadata.end(); it++)
+      {
+        dicom.GetMetadata().insert(std::make_pair(std::make_pair(ResourceType_Instance, it->first),
+                                                  it->second));
+      }
+            
       if (status != StoreStatus_Success)
       {
         storage_.Remove(dicomInfo.GetUuid());
--- a/Plugins/Engine/PluginsHttpHandler.cpp	Fri Jul 25 18:39:02 2014 +0200
+++ b/Plugins/Engine/PluginsHttpHandler.cpp	Mon Jul 28 11:09:03 2014 +0200
@@ -595,6 +595,60 @@
   }
 
 
+  static void AccessInstanceMetadataInternal(bool checkExistence,
+                                             const _OrthancPluginAccessDicomInstance& params,
+                                             const DicomInstanceToStore& instance)
+  {
+    MetadataType metadata;
+
+    try
+    {
+      metadata = StringToMetadata(params.key);
+    }
+    catch (OrthancException&)
+    {
+      // Unknown metadata
+      if (checkExistence)
+      {
+        *params.resultInt64 = -1;
+      }
+      else
+      {
+        *params.resultString = NULL;
+      }
+
+      return;
+    }
+
+    ServerIndex::MetadataMap::const_iterator it = 
+      instance.GetMetadata().find(std::make_pair(ResourceType_Instance, metadata));
+
+    if (checkExistence)
+    {
+      if (it != instance.GetMetadata().end())
+      {
+        *params.resultInt64 = 1;
+      }
+      else
+      {
+        *params.resultInt64 = 0;
+      }
+    }
+    else
+    {
+      if (it != instance.GetMetadata().end())
+      {      
+        *params.resultString = it->second.c_str();
+      }
+      else
+      {
+        // Error: Missing metadata
+        *params.resultString = NULL;
+      }
+    }
+  }
+
+
   static void AccessDicomInstance(_OrthancPluginService service,
                                   const void* parameters)
   {
@@ -618,6 +672,14 @@
         *p.resultString = instance.GetBufferData();
         return;
 
+      case _OrthancPluginService_HasInstanceMetadata:
+        AccessInstanceMetadataInternal(true, p, instance);
+        return;
+
+      case _OrthancPluginService_GetInstanceMetadata:
+        AccessInstanceMetadataInternal(false, p, instance);
+        return;
+
       case _OrthancPluginService_GetInstanceJson:
       case _OrthancPluginService_GetInstanceSimplifiedJson:
       {
@@ -727,6 +789,8 @@
       case _OrthancPluginService_GetInstanceData:
       case _OrthancPluginService_GetInstanceJson:
       case _OrthancPluginService_GetInstanceSimplifiedJson:
+      case _OrthancPluginService_HasInstanceMetadata:
+      case _OrthancPluginService_GetInstanceMetadata:
         AccessDicomInstance(service, parameters);
         return true;
 
--- a/Plugins/OrthancCPlugin/OrthancCPlugin.h	Fri Jul 25 18:39:02 2014 +0200
+++ b/Plugins/OrthancCPlugin/OrthancCPlugin.h	Mon Jul 28 11:09:03 2014 +0200
@@ -265,9 +265,9 @@
     _OrthancPluginService_GetInstanceSize = 4001,
     _OrthancPluginService_GetInstanceData = 4002,
     _OrthancPluginService_GetInstanceJson = 4003,
-    _OrthancPluginService_GetInstanceSimplifiedJson = 4004
-    
-    /* + METADATA !!! */
+    _OrthancPluginService_GetInstanceSimplifiedJson = 4004,
+    _OrthancPluginService_HasInstanceMetadata = 4005,
+    _OrthancPluginService_GetInstanceMetadata = 4006
   } _OrthancPluginService;
 
 
@@ -889,6 +889,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_LookupPatient, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -920,6 +921,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_LookupStudy, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -951,6 +953,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_LookupSeries, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -982,6 +985,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_LookupInstance, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -1122,6 +1126,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_GetInstanceRemoteAet, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -1144,6 +1149,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_GetInstanceSize, &params))
     {
+      /* Error */
       return -1;
     }
     else
@@ -1166,6 +1172,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_GetInstanceData, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -1188,6 +1195,7 @@
 
     if (context->InvokeService(context, _OrthancPluginService_GetInstanceJson, &params))
     {
+      /* Error */
       return NULL;
     }
     else
@@ -1210,6 +1218,57 @@
 
     if (context->InvokeService(context, _OrthancPluginService_GetInstanceSimplifiedJson, &params))
     {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
+
+  ORTHANC_PLUGIN_INLINE int  OrthancPluginHasInstanceMetadata(
+    OrthancPluginContext*        context,
+    OrthancPluginDicomInstance*  instance,
+    const char*                  key)
+  {
+    int64_t result;
+
+    _OrthancPluginAccessDicomInstance params;
+    memset(&params, 0, sizeof(params));
+    params.resultInt64 = &result;
+    params.instance = instance;
+    params.key = key;
+
+    if (context->InvokeService(context, _OrthancPluginService_HasInstanceMetadata, &params))
+    {
+      /* Error */
+      return -1;
+    }
+    else
+    {
+      return (result != 0);
+    }
+  }
+
+
+  ORTHANC_PLUGIN_INLINE const char* OrthancPluginGetInstanceMetadata(
+    OrthancPluginContext*        context,
+    OrthancPluginDicomInstance*  instance,
+    const char*                  key)
+  {
+    const char* result;
+
+    _OrthancPluginAccessDicomInstance params;
+    memset(&params, 0, sizeof(params));
+    params.resultString = &result;
+    params.instance = instance;
+    params.key = key;
+
+    if (context->InvokeService(context, _OrthancPluginService_GetInstanceMetadata, &params))
+    {
+      /* Error */
       return NULL;
     }
     else
@@ -1220,13 +1279,6 @@
 
 
 
-
-  /*
-    TODO :  METADATA !!!
-    TODO : DOCUMENTATION !!!
-  */
-
-
 #ifdef  __cplusplus
 }
 #endif
--- a/Plugins/Samples/Basic/Plugin.c	Fri Jul 25 18:39:02 2014 +0200
+++ b/Plugins/Samples/Basic/Plugin.c	Mon Jul 28 11:09:03 2014 +0200
@@ -187,6 +187,15 @@
   printf("[%s]\n", json);
   OrthancPluginFreeString(context, json);
 
+  if (OrthancPluginHasInstanceMetadata(context, instance, "ReceptionDate"))
+  {
+    printf("Received on [%s]\n", OrthancPluginGetInstanceMetadata(context, instance, "ReceptionDate"));
+  }
+  else
+  {
+    OrthancPluginLogError(context, "Instance has no reception date, should never happen!");
+  }
+
   return 0;
 }