public async Task <RuleBO> GetAsync(Guid ruleId)
        {
            var rule = await _ruleQueries
                       .GetAsync(ruleId);

            return(rule != null?MapEntityToModel(rule) : null);
        }
Example #2
0
        public async Task <RuleExecutionLogDetailExportToTableBO> ExportToTableByRuleExecutionLogIdAsync(int id)
        {
            RuleExecutionLogDetailExportToTableBO result = new RuleExecutionLogDetailExportToTableBO();

            var ruleExecutionLog = await _queriesRuleExecutionLog.GetAsync(id);

            if (ruleExecutionLog != null && !string.IsNullOrEmpty(ruleExecutionLog.DetailsTableName))
            {
                result.TableName    = ruleExecutionLog.DetailsTableName;
                result.AlreadyExist = await _queries.ExistExportTableFromRuleExecutionLogAsync(ruleExecutionLog.DetailsTableName, "destination");
            }

            if (!result.AlreadyExist)
            {
                Persistence.Rules.Rule ruleFromLog = await _queriesRule.GetAsync(ruleExecutionLog.RuleId);

                string ruleName = ruleFromLog.Name;

                ruleName = Regex.Replace(ruleName, @"[^\w\.@-]", "_", RegexOptions.None, TimeSpan.FromSeconds(1.5));
                string tableName = $"T_{id}-{ruleName}";
                if (tableName.Length > 128)
                {
                    tableName = tableName.Substring(0, 128);
                }

                Dictionary <string, string> columns = new Dictionary <string, string>();
                columns = JsonConvert.DeserializeObject <Dictionary <string, string> >(ruleExecutionLog.DetailsSchema);

                List <string> sqlColumns = new List <string>();
                foreach (var column in columns)
                {
                    if (column.Value == "string")
                    {
                        sqlColumns.Add(string.Format($"[{column.Key}] [nvarchar](max) NULL"));
                    }
                    else
                    {
                        sqlColumns.Add(string.Format($"[{column.Key}] [datetime2](7) NULL"));
                    }
                }

                string sqlCreate = $"CREATE TABLE [destination].[{tableName}]({string.Join(",", sqlColumns)}) ";
                await _commandRuleExecutionLogDetail.ExecuteSqlAsync(sqlCreate);

                result.TableName = tableName;
                result.Created   = await _queries.ExistExportTableFromRuleExecutionLogAsync(tableName, "destination");

                if (result.Created)
                {
                    var ruleExecutionLogInfo = await GetByRuleExecutionLogIdAsync(id);

                    DataTable infoToInsert = Utils.GetTableForSqlBulk(ruleExecutionLogInfo.Rows, columns);
                    await _commandRuleExecutionLogDetail.ExecuteSqlBulkCopy(infoToInsert, $"[destination].[{tableName}]");

                    ruleExecutionLog.DetailsTableName = tableName;
                    await _commandRuleExecutionLog.UpdateAsync(ruleExecutionLog);
                }
            }

            return(result);
        }