/// <summary> Refresh the settings object by pulling the data back from the database </summary>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool RefreshSettings(Database_Instance_Configuration DbInstance)
        {
            try
            {
                lock (settingsLock)
                {
                    if (settings == null)
                    {
                        settings = InstanceWide_Settings_Builder.Build_Settings(DbInstance);
                    }
                    else
                    {
                        InstanceWide_Settings newSettings = InstanceWide_Settings_Builder.Build_Settings(DbInstance);
                        settings = newSettings;
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        /// <summary> Refresh the settings object by pulling the data back from the database </summary>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public static bool RefreshSettings()
        {
            try
            {
                lock (settingsLock)
                {
                    if (settings == null)
                    {
                        settings = InstanceWide_Settings_Builder.Build_Settings();
                    }
                    else
                    {
                        InstanceWide_Settings newSettings = InstanceWide_Settings_Builder.Build_Settings();
                        settings = newSettings;
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        /// <summary> Refresh the settings and item list from the database </summary>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        public bool Refresh_Settings_And_Item_List()
        {
            // Create the tracer for this
            Custom_Tracer tracer = new Custom_Tracer();

            // Disable the cache
            CachedDataManager.Settings.Disabled = true;

            // Set all the database strings appropriately
            Engine_Database.Connection_String       = instanceInfo.DatabaseConnection.Connection_String;
            SobekCM_Item_Database.Connection_String = instanceInfo.DatabaseConnection.Connection_String;

            // Get the settings values directly from the database
            settings = InstanceWide_Settings_Builder.Build_Settings(instanceInfo.DatabaseConnection);
            if (settings == null)
            {
                Add_Error_To_Log("Unable to pull the newest settings from the database", String.Empty, String.Empty, -1);
                return(false);
            }

            // If this was not refreshed yet, ensure [BASEURL] is replaced
            if (!refreshed)
            {
                // Determine the base url
                string baseUrl = String.IsNullOrWhiteSpace(settings.Servers.Base_URL) ? settings.Servers.Application_Server_URL : settings.Servers.Base_URL;
                List <MicroservicesClient_Endpoint> endpoints = instanceInfo.Microservices.Endpoints;
                foreach (MicroservicesClient_Endpoint thisEndpoint in endpoints)
                {
                    if (thisEndpoint.URL.IndexOf("[BASEURL]") > 0)
                    {
                        thisEndpoint.URL = thisEndpoint.URL.Replace("[BASEURL]", baseUrl).Replace("//", "/").Replace("http:/", "http://").Replace("https:/", "https://");
                    }
                    else if ((thisEndpoint.URL.IndexOf("http:/") < 0) && (thisEndpoint.URL.IndexOf("https:/") < 0))
                    {
                        thisEndpoint.URL = (baseUrl + thisEndpoint.URL).Replace("//", "/").Replace("http:/", "http://").Replace("https:/", "https://");
                    }
                }
                refreshed = true;
            }

            // Set the microservice endpoints
            SobekEngineClient.Set_Endpoints(instanceInfo.Microservices);

            // Load the necessary configuration objects into the engine application cache gateway
            try
            {
                Engine_ApplicationCache_Gateway.Configuration.OAI_PMH = SobekEngineClient.Admin.Get_OAI_PMH_Configuration(tracer);
            }
            catch (Exception ee)
            {
                Add_Error_To_Log("Unable to pull the OAI-PMH settings from the engine", String.Empty, String.Empty, -1);
                Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                return(false);
            }

            try
            {
                Engine_ApplicationCache_Gateway.Configuration.Metadata = SobekEngineClient.Admin.Get_Metadata_Configuration(tracer);
            }
            catch (Exception ee)
            {
                Add_Error_To_Log("Unable to pull the metadata settings from the engine", String.Empty, String.Empty, -1);
                Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                return(false);
            }

            try
            {
                Engine_ApplicationCache_Gateway.Configuration.Extensions = SobekEngineClient.Admin.Get_Extensions_Configuration(tracer);
            }
            catch (Exception ee)
            {
                Add_Error_To_Log("Unable to pull the extension settings from the engine", String.Empty, String.Empty, -1);
                Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                return(false);
            }


            // Check for any enabled extensions with assemblies
            ResourceObjectSettings.Clear_Assemblies();
            try
            {
                if ((Engine_ApplicationCache_Gateway.Configuration.Extensions.Extensions != null) && (Engine_ApplicationCache_Gateway.Configuration.Extensions.Extensions.Count > 0))
                {
                    // Step through each extension
                    foreach (ExtensionInfo extensionInfo in Engine_ApplicationCache_Gateway.Configuration.Extensions.Extensions)
                    {
                        // If not enabled, skip it
                        if (!extensionInfo.Enabled)
                        {
                            continue;
                        }

                        // Look for assemblies
                        if ((extensionInfo.Assemblies != null) && (extensionInfo.Assemblies.Count > 0))
                        {
                            // Step through each assembly
                            foreach (ExtensionAssembly assembly in extensionInfo.Assemblies)
                            {
                                // Find the relative file name
                                if (assembly.FilePath.IndexOf("plugins", StringComparison.OrdinalIgnoreCase) > 0)
                                {
                                    // Determine the network way to get there
                                    string from_plugins        = assembly.FilePath.Substring(assembly.FilePath.IndexOf("plugins", StringComparison.OrdinalIgnoreCase));
                                    string network_plugin_file = Path.Combine(settings.Servers.Application_Server_Network, from_plugins);

                                    // Get the plugin filename
                                    string plugin_filename = Path.GetFileName(assembly.FilePath);

                                    // Does this local plugin directory exist for this extension?
                                    string local_path = Path.Combine(pluginRootDirectory, instanceInfo.Name, extensionInfo.Code);
                                    if (!Directory.Exists(local_path))
                                    {
                                        try
                                        {
                                            Directory.CreateDirectory(local_path);
                                        }
                                        catch (Exception ee)
                                        {
                                            Add_Error_To_Log("Error creating the necessary plug-in subdirectory", String.Empty, String.Empty, -1);
                                            Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                                            return(false);
                                        }
                                    }

                                    // Determine if the assembly is here
                                    string local_file = Path.Combine(local_path, plugin_filename);
                                    if (!File.Exists(local_file))
                                    {
                                        File.Copy(network_plugin_file, local_file);
                                    }
                                    else
                                    {
                                        // Do a date check
                                        DateTime webFileDate   = File.GetLastWriteTime(network_plugin_file);
                                        DateTime localFileDate = File.GetLastWriteTime(local_file);

                                        if (webFileDate.CompareTo(localFileDate) > 0)
                                        {
                                            File.Copy(network_plugin_file, local_file, true);
                                        }
                                    }

                                    // Also, point the assembly to use the local file
                                    assembly.FilePath = local_file;
                                }
                            }
                        }
                    }

                    // Now, also set this all in the metadata portion
                    // Copy over all the extension information
                    foreach (ExtensionInfo thisExtension in Engine_ApplicationCache_Gateway.Configuration.Extensions.Extensions)
                    {
                        if ((thisExtension.Enabled) && (thisExtension.Assemblies != null))
                        {
                            foreach (ExtensionAssembly thisAssembly in thisExtension.Assemblies)
                            {
                                ResourceObjectSettings.Add_Assembly(thisAssembly.ID, thisAssembly.FilePath);
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Add_Error_To_Log("Unable to copy the extension files from the web", String.Empty, String.Empty, -1);
                Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                return(false);
            }

            // Finalize the metadata config
            Engine_ApplicationCache_Gateway.Configuration.Metadata.Finalize_Metadata_Configuration();

            // Set the metadata preferences for writing
            ResourceObjectSettings.MetadataConfig = Engine_ApplicationCache_Gateway.Configuration.Metadata;

            // Also, load the builder configuration this way
            try
            {
                builderSettings = SobekEngineClient.Builder.Get_Builder_Settings(false, tracer);
            }
            catch (Exception ee)
            {
                Add_Error_To_Log("Unable to pull the builder settings from the engine", String.Empty, String.Empty, -1);
                Add_Error_To_Log(ee.Message, String.Empty, String.Empty, -1);
                return(false);
            }


            // Build the modules
            builderModules = new Builder_Modules(builderSettings);
            List <string> errors = builderModules.Builder_Modules_From_Settings(instanceInfo.Name);

            if ((errors != null) && (errors.Count > 0))
            {
                long logId = Add_Error_To_Log("Error(s) builder the modules from the settings", String.Empty, String.Empty, -1);
                foreach (string thisError in errors)
                {
                    Add_Error_To_Log(thisError, String.Empty, String.Empty, logId);
                }
                return(false);
            }

            // Add the event listeners
            foreach (iPreProcessModule thisModule in builderModules.PreProcessModules)
            {
                thisModule.Error   += module_Error;
                thisModule.Process += module_Process;
            }
            foreach (iSubmissionPackageModule thisModule in builderModules.DeleteItemModules)
            {
                thisModule.Error   += module_Error;
                thisModule.Process += module_Process;
            }
            foreach (iSubmissionPackageModule thisModule in builderModules.ItemProcessModules)
            {
                thisModule.Error   += module_Error;
                thisModule.Process += module_Process;
            }
            foreach (iPostProcessModule thisModule in builderModules.PostProcessModules)
            {
                thisModule.Error   += module_Error;
                thisModule.Process += module_Process;
            }
            foreach (iFolderModule thisModule in builderModules.AllFolderModules)
            {
                thisModule.Error   += module_Error;
                thisModule.Process += module_Process;
            }

            return(true);
        }