Пример #1
0
        /// <summary>
        /// Updates the <see cref="DbRuleObject"/> list in the database. So  therefore all <see cref="DbRule"/> objects and <see cref="DbCondition"/> objects contained in the argument are updated.
        /// </summary>
        /// <param name="dbRuleObjectList">The <see cref="DbRuleObject"/> list to update.</param>
        /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
        /// <returns>The count of records affected</returns>
        public static int UpdateDbRuleObjectsToDatabase(List <DbRuleObject> dbRuleObjectList, CancellationTokenSource cancellationTokenSource)
        {
            CancellationToken cancellationToken = cancellationTokenSource.Token;

            int           count      = 0;
            List <DbRule> dbRuleList = dbRuleObjectList.Select(o => o.DbRule).ToList();

            ConnectionInfo connectionInfo = new ConnectionInfo(Globals.ConfigurationManager.DatabaseConnectionString, Globals.ConfigurationManager.DatabaseProviderType);

            try
            {
                // Update all the DbRules
                Task updateRules = DbRuleService.UpdateAsync(connectionInfo, dbRuleList, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
                count += dbRuleList.Count;

                List <DbCondition> dbConditionList = new List <DbCondition>();
                foreach (DbRuleObject dbRuleObject in dbRuleObjectList)
                {
                    dbConditionList.AddRange(dbRuleObject.DbConditions);
                }

                //Update all the DbConditions
                Task updateConditions = DbConditionService.UpdateAsync(connectionInfo, dbConditionList, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
                count += dbConditionList.Count;

                Task.WaitAll(updateRules, updateConditions);
            }
            catch (Exception)
            {
                cancellationTokenSource.Cancel();
                throw;
            }

            return(count);
        }
        /// <summary>
        /// Updates the <see cref="DbRuleObject"/> list in the database. So  therefore all <see cref="DbRule"/> objects and <see cref="DbCondition"/> objects contained in the argument are updated.
        /// </summary>
        /// <param name="dbRuleObjectList">The <see cref="DbRuleObject"/> list to update.</param>
        /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
        /// <returns>The count of records affected</returns>
        public static int UpdateDbRuleObjectsToDatabase(List <DbRuleObject> dbRuleObjectList, CancellationTokenSource cancellationTokenSource)
        {
            CancellationToken cancellationToken = cancellationTokenSource.Token;

            int           count      = 0;
            List <DbRule> dbRuleList = dbRuleObjectList.Select(o => o.DbRule).ToList();

            ConnectionInfo connectionInfo = new(Globals.ConfigurationManager.DatabaseConnectionString, Globals.ConfigurationManager.DatabaseProviderType);

            try
            {
                // Update all the DbRules
                Task updateRulesTask = DbRuleService.UpdateAsync(connectionInfo, dbRuleList, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
                count += dbRuleList.Count;

                // Get list of existing conditions to be deleted.
                List <DbCondition> dbConditionsToDelete = new();
                foreach (DbRuleObject dbRuleObject in dbRuleObjectList)
                {
                    dbConditionsToDelete.AddRange(dbRuleObject.DbConditionsToBeDeleted);
                }

                // Get list of conditions to be inserted.
                List <DbCondition> dbConditionsToInsert = new();
                foreach (DbRuleObject dbRuleObject in dbRuleObjectList)
                {
                    dbConditionsToInsert.AddRange(dbRuleObject.DbConditions);
                }

                // Delete all the DbConditions.
                Task deleteConditionsTask = DbConditionService.DeleteAsync(connectionInfo, dbConditionsToDelete, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);

                //Insert (re-create) all the DbConditions.
                Task insertConditionsTask = DbConditionService.InsertAsync(connectionInfo, dbConditionsToInsert, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
                count += dbConditionsToInsert.Count;

                Task.WaitAll(updateRulesTask, deleteConditionsTask, insertConditionsTask);
            }
            catch (Exception)
            {
                cancellationTokenSource.Cancel();
                throw;
            }

            return(count);
        }
        /// <summary>
        /// Inserts the <see cref="DbRuleObject"/> object into the database
        /// </summary>
        /// <param name="dbRuleObject">The <see cref="DbRuleObject"/> object to insert.</param>
        /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param>
        /// <returns></returns>
        public async static Task <bool> InsertDbRuleObjectAsync(DbRuleObject dbRuleObject, CancellationTokenSource cancellationTokenSource)
        {
            ConnectionInfo connectionInfo = new(Globals.ConfigurationManager.DatabaseConnectionString, Globals.ConfigurationManager.DatabaseProviderType);

            long ruleEntitiesInserted;

            try
            {
                ruleEntitiesInserted = await DbRuleService.InsertRuleAsync(connectionInfo, dbRuleObject.DbRule, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
            }
            catch (Exception)
            {
                cancellationTokenSource.Cancel();
                throw;
            }
            logger.Debug($"{ruleEntitiesInserted} rules inserted. Rule Name: {dbRuleObject.DbRule.Name}");

            long condEntitiesInserted;

            try
            {
                condEntitiesInserted = await DbConditionService.InsertAsync(connectionInfo, dbRuleObject.DbConditions, cancellationTokenSource, Globals.ConfigurationManager.TimeoutSecondsForDatabaseTasks);
            }
            catch (Exception)
            {
                cancellationTokenSource.Cancel();
                throw;
            }
            logger.Debug($"{condEntitiesInserted} conditions inserted");

            if ((ruleEntitiesInserted > 0) && (condEntitiesInserted > 0))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }