changeset 12:5c2aae2ab8be

Options "EnableIndex" and "EnableStorage" to explicitly enable PostgreSQL
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Feb 2015 17:31:38 +0100
parents 1efb30985931
children 2d60b2cf6195
files Core/Configuration.cpp Core/Configuration.h IndexPlugin/Plugin.cpp NEWS StoragePlugin/Plugin.cpp
diffstat 5 files changed, 77 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Configuration.cpp	Fri Feb 27 17:12:58 2015 +0100
+++ b/Core/Configuration.cpp	Fri Feb 27 17:31:38 2015 +0100
@@ -107,15 +107,27 @@
   }
 
 
-  PostgreSQLConnection* CreateConnection(bool& useLock,
-                                         OrthancPluginContext* context)
+  bool GetBooleanValue(const Json::Value& configuration,
+                       const std::string& key,
+                       bool defaultValue)
   {
-    Json::Value configuration;
-    if (!ReadConfiguration(configuration, context))
+    if (configuration.type() != Json::objectValue ||
+        !configuration.isMember(key) ||
+        configuration[key].type() != Json::booleanValue)
+    {
+      return defaultValue;
+    }
+    else
     {
-      return NULL;
+      return configuration[key].asBool();
     }
+  }
 
+
+  PostgreSQLConnection* CreateConnection(bool& useLock,
+                                         OrthancPluginContext* context,
+                                         const Json::Value& configuration)
+  {
     useLock = true;  // Use locking by default
     std::auto_ptr<PostgreSQLConnection> connection(new PostgreSQLConnection);
 
@@ -135,11 +147,7 @@
         connection->SetPassword(GetStringValue(c, "Password", "orthanc"));
       }
 
-      if (c.isMember("Lock") &&
-          c["Lock"].type() == Json::booleanValue)
-      {
-        useLock = c["Lock"].asBool();
-      }
+      useLock = GetBooleanValue(c, "Lock", useLock);
     }
 
     if (!useLock)
--- a/Core/Configuration.h	Fri Feb 27 17:12:58 2015 +0100
+++ b/Core/Configuration.h	Fri Feb 27 17:31:38 2015 +0100
@@ -47,8 +47,13 @@
                       const std::string& key,
                       int defaultValue);
 
+  bool GetBooleanValue(const Json::Value& configuration,
+                       const std::string& key,
+                       bool defaultValue);
+  
   PostgreSQLConnection* CreateConnection(bool& useLock,
-                                         OrthancPluginContext* context);
+                                         OrthancPluginContext* context,
+                                         const Json::Value& configuration);
 
   std::string GenerateUuid();
 
--- a/IndexPlugin/Plugin.cpp	Fri Feb 27 17:12:58 2015 +0100
+++ b/IndexPlugin/Plugin.cpp	Fri Feb 27 17:31:38 2015 +0100
@@ -34,7 +34,6 @@
   ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
   {
     context_ = context;
-    OrthancPluginLogWarning(context_, "Using PostgreSQL index");
 
     /* Check the version of the Orthanc core */
     if (OrthancPluginCheckVersion(context_) == 0)
@@ -49,17 +48,38 @@
       return -1;
     }
 
-    bool allowUnlock = OrthancPlugins::IsFlagInCommandLineArguments(context_, FLAG_UNLOCK);
+    OrthancPluginSetDescription(context_, "Stores the Orthanc index into a PostgreSQL database.");
+
+
+    Json::Value configuration;
+    if (!OrthancPlugins::ReadConfiguration(configuration, context))
+    {
+      OrthancPluginLogError(context_, "Unable to read the configuration file");
+      return -1;
+    }
 
-    OrthancPluginSetDescription(context_, "Stores the Orthanc index into a PostgreSQL database.");
+    if (!configuration.isMember("PostgreSQL") ||
+        configuration["PostgreSQL"].type() != Json::objectValue ||
+        !OrthancPlugins::GetBooleanValue(configuration["PostgreSQL"], "EnableIndex", false))
+    {
+      OrthancPluginLogWarning(context_, "The PostgreSQL index is currently disabled, set \"EnableIndex\" to \"true\" in the \"PostgreSQL\" section of the configuration file of Orthanc");
+      return 0;
+    }
+    else
+    {
+      OrthancPluginLogWarning(context_, "Using PostgreSQL index");
+    }
+
+    bool allowUnlock = OrthancPlugins::IsFlagInCommandLineArguments(context_, FLAG_UNLOCK);
 
     try
     {
       /* Create the connection to PostgreSQL */
       bool useLock;
-      std::auto_ptr<OrthancPlugins::PostgreSQLConnection> pg(OrthancPlugins::CreateConnection(useLock, context_));
+      std::auto_ptr<OrthancPlugins::PostgreSQLConnection> 
+        pg(OrthancPlugins::CreateConnection(useLock, context_, configuration));
+
       pg->Open();
-      std::cout << "******** " << pg->GetConnectionUri() << std::endl;
       //pg->ClearAll();   // Reset the database
  
       /* Create the database back-end */
--- a/NEWS	Fri Feb 27 17:12:58 2015 +0100
+++ b/NEWS	Fri Feb 27 17:31:38 2015 +0100
@@ -3,6 +3,7 @@
 
 * Use of advisory locks
 * Support of connection URI in PostgreSQL
+* Options "EnableIndex" and "EnableStorage" to explicitly enable PostgreSQL
 
 
 2015/02/06
--- a/StoragePlugin/Plugin.cpp	Fri Feb 27 17:12:58 2015 +0100
+++ b/StoragePlugin/Plugin.cpp	Fri Feb 27 17:31:38 2015 +0100
@@ -89,7 +89,6 @@
   ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
   {
     context_ = context;
-    OrthancPluginLogWarning(context_, "Using PostgreSQL storage area");
 
     /* Check the version of the Orthanc core */
     if (OrthancPluginCheckVersion(context_) == 0)
@@ -104,15 +103,39 @@
       return -1;
     }
 
-    bool allowUnlock = OrthancPlugins::IsFlagInCommandLineArguments(context_, FLAG_UNLOCK);
+    OrthancPluginSetDescription(context_, "Stores the files received by Orthanc into a PostgreSQL database.");
+
+
+    Json::Value configuration;
+    if (!OrthancPlugins::ReadConfiguration(configuration, context))
+    {
+      OrthancPluginLogError(context_, "Unable to read the configuration file");
+      return -1;
+    }
+
 
-    OrthancPluginSetDescription(context_, "Stores the files received by Orthanc into a PostgreSQL database.");
+    if (!configuration.isMember("PostgreSQL") ||
+        configuration["PostgreSQL"].type() != Json::objectValue ||
+        !OrthancPlugins::GetBooleanValue(configuration["PostgreSQL"], "EnableStorage", false))
+    {
+      OrthancPluginLogWarning(context_, "The PostgreSQL storage area is currently disabled, set \"EnableStorage\" to \"true\" in the \"PostgreSQL\" section of the configuration file of Orthanc");
+      return 0;
+    }
+    else
+    {
+      OrthancPluginLogWarning(context_, "Using PostgreSQL storage are");
+    }
+
+
+    bool allowUnlock = OrthancPlugins::IsFlagInCommandLineArguments(context_, FLAG_UNLOCK);
 
     try
     {
       /* Create the connection to PostgreSQL */
       bool useLock;
-      std::auto_ptr<OrthancPlugins::PostgreSQLConnection> pg(OrthancPlugins::CreateConnection(useLock, context_));
+      std::auto_ptr<OrthancPlugins::PostgreSQLConnection> 
+        pg(OrthancPlugins::CreateConnection(useLock, context_, configuration));
+
       pg->Open();
       //pg->ClearAll();   // Reset the database