/// <summary>
        /// Tests the connection to the database
        /// </summary>
        internal bool TestDatabaseConnection()
        {
            // initial value
            bool connected = false;

            // locals
            string methodName = "TestDatabaseConnection";
            string objectName = "$safeprojectname$.Controller.System.SystemController";

            try
            {
                // Create Delegate For DataOperation
                ApplicationController.DataOperationMethod testDataConnection = this.DataBridge.DataOperations.SystemMethods.TestDataConnection;

                // Create null parameters object (not needed for this, but you have to have it to call the method).
                List <PolymorphicObject> parameters = null;

                // Perform DataOperation
                PolymorphicObject connectedObject = this.DataBridge.PerformDataOperation(methodName, objectName, testDataConnection, parameters);

                // If method returned "true" value.
                if (connectedObject.Boolean.Value == NullableBooleanEnum.True)
                {
                    // set connected to true.
                    connected = true;
                }
            }
            catch (Exception error)
            {
                // If ErrorProcessor exists
                if (this.ErrorProcessor != null)
                {
                    // Create Error
                    DataConnectionFailedException dataConnectionError = new DataConnectionFailedException(methodName, objectName, error);

                    // Log the current error
                    this.ErrorProcessor.LogError(methodName, objectName, dataConnectionError);
                }
            }

            // return value
            return(connected);
        }
        /// <summary>
        /// Performs an operation that required a connection to the database.
        /// This method uses a delegate to execute the method so that this is the
        /// only place in the application a connection to the database is opened.
        /// </summary>
        internal PolymorphicObject PerformDataOperation(string methodName, string objectName, ApplicationController.DataOperationMethod dataMethod, List <PolymorphicObject> parameters)
        {
            // Initial Value
            PolymorphicObject returnObject = null;

            // local
            bool dataConnectionNotAvailable = false;

            try
            {
                // set the last exception to null
                this.Exception = null;

                // Connect To Database
                this.LoginManager.ConnectToDatabase(this.DataManager);

                // Testing only
                string connectionString = this.DataManager.DataConnector.ConnectionString;

                // if connected
                if (this.DataManager.DataConnector.Connected)
                {
                    // verify dataMethod exists
                    if (dataMethod != null)
                    {
                        // Invoke Method
                        returnObject = dataMethod(parameters, this.DataManager.DataConnector);
                    }
                }
                else
                {
                    // set dataConnectionNotAvailable to true
                    dataConnectionNotAvailable = true;

                    // Raise Error Data Connection Not Available
                    throw new Exception("The database connection is not available.");
                }
            }
            catch (Exception error)
            {
                // Set exception
                this.Exception = error;

                // If ErrorProcessor exists
                if (this.ErrorProcessor != null)
                {
                    // If this is a dataConnection not available error.
                    if (dataConnectionNotAvailable)
                    {
                        // Create Error for data connection failed
                        DataConnectionFailedException dataConnectionError = new DataConnectionFailedException(methodName, objectName, error);

                        // Log the current dataConnectionError
                        this.ErrorProcessor.LogError(methodName, objectName, dataConnectionError);
                    }
                    else
                    {
                        // Log the exception
                        this.ErrorProcessor.LogError(methodName, objectName, error);
                    }
                }
            }
            finally
            {
                // Close Connection To Database
                this.DataManager.DataConnector.Close();
            }

            // return value
            return(returnObject);
        }
Example #3
0
        /// <summary>
        /// This method reads the app.config file to
        /// load the current configuration.
        /// </summary>
        /// <returns></returns>
        public void ConnectToDatabase(DataManager dataManager)
        {
            // locals
            string methodName = "ConnectToDatabase";
            string objectName = "AuthenticationManager";

            // Create variable for a possible CustomException
            CustomException exception = null;

            try
            {
                // If connection is not set
                if (String.IsNullOrEmpty(dataManager.DataConnector.ConnectionString))
                {
                    // if the connectionName is set
                    if (TextHelper.Exists(dataManager.ConnectionName))
                    {
                        // Set the ConnectionString (requires DataJuggler.Core.UltimateHelper version 1.3.5 or greater)
                        dataManager.DataConnector.ConnectionString = EnvironmentVariableHelper.GetEnvironmentVariableValue(dataManager.ConnectionName);
                    }
                    else
                    {
                        // this is for environments using a web.config or app.config.

                        // For Dot Net Core / Blazor or any new projects, Environment Variables are now the
                        // recommended way to store connection strings.

                        // Read Configuration File
                        this.Configuration.Read();

                        // If the configuration is valid
                        if (this.Configuration.IsValid)
                        {
                            // if the ConnectionString exists
                            if (this.Configuration.HasConnectionString)
                            {
                                // Set the connection string using Integrated Secrity (Windows Authentication)
                                dataManager.DataConnector.ConnectionString = this.Configuration.ConnectionString;
                            }
                            else
                            {
                                // set the server
                                string serverName = this.Configuration.DatabaseServer;

                                // set the databaseName
                                string databaseName = this.Configuration.DatabaseName;

                                // If integrated security is set to true
                                if (this.Configuration.IntegratedSecurity)
                                {
                                    // Set the connection string using Integrated Secrity (Windows Authentication)
                                    dataManager.DataConnector.ConnectionString = dataManager.DataConnector.BuildConnectionString(serverName, databaseName);
                                }
                                else
                                {
                                    // set the userName
                                    string userName = this.Configuration.DatabaseUserName;

                                    // set the password
                                    string password = this.Configuration.DatabasePassword;

                                    // build the connectionstring for Sql Server Authentication
                                    dataManager.DataConnector.ConnectionString = dataManager.DataConnector.BuildConnectionString(serverName, databaseName, userName, password);
                                }
                            }
                        }
                    }
                }

                // check if database is already connected
                if (dataManager.DataConnector.State == System.Data.ConnectionState.Open)
                {
                    // close connection and reopen (there should not be any open connections here)
                    // I have been thinking of a bulk insert feature in the future, but that is not for this method
                    // To Do: Log Error 'Database Connection Was Already Open'
                    dataManager.DataConnector.Close();
                }

                // Open Connection
                dataManager.DataConnector.Open();
            }
            catch (Exception error)
            {
                // If ErrorProcessor exists
                if (this.ErrorProcessor != null)
                {
                    // Only create a new configuration error if it does not already exist.
                    if (error as CustomException == null)
                    {
                        // If the configuration is not valid
                        if (!this.Configuration.IsValid)
                        {
                            // Create Instance of configurationError
                            exception = new InvalidConfigurationException(methodName, objectName, error);
                        }
                        else
                        {
                            // Create Instance of dataConnectionError
                            exception = new DataConnectionFailedException(methodName, objectName, error);
                        }
                    }

                    // Log this error
                    this.ErrorProcessor.LogError(methodName, objectName, exception);
                }
            }
        }