public async Task <bool> MoveRuleToContainer(List <Guid> rules, ContainerBO container) { bool result = true; foreach (Guid idRule in rules) { Rule existRule = await _ruleQueries.GetAsync(idRule); if (existRule != null && existRule.ContainerId != container.Id) { existRule.ContainerId = container.Id; await _ruleCommands.UpdateContainerIdAsync(existRule); } } return(result); }
/// <summary> /// Get Containers /// </summary> /// <param name="OrderKey"></param> /// <returns></returns> public List <ContainerBO> GetContainers(string OrderKey) { try { var ContainerList = new List <ContainerBO>(); var sql = "dbo.fn_getcontainerlistbyorderkey"; conn = new NpgsqlConnection(connString); conn.Open(); using (var cmd = new NpgsqlCommand(sql, conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("_orderkey", NpgsqlTypes.NpgsqlDbType.Uuid, Guid.Parse(OrderKey)); var reader = cmd.ExecuteReader(); do { while (reader.Read()) { var BO = new ContainerBO { ContainerNo = Utils.CustomParse <string>(reader["containerno"]), ContainerSize = Utils.CustomParse <short>(reader["containersize"]), ContainerSizeDesc = Utils.CustomParse <string>(reader["containersizedesc"]), Chassis = Utils.CustomParse <string>(reader["chassis"]), SealNo = Utils.CustomParse <string>(reader["sealno"]), Weight = Utils.CustomParse <string>(reader["weight"]) }; ContainerList.Add(BO); } }while (reader.NextResult()); reader.Close(); } return(ContainerList); } catch (Exception msg) { throw msg; } finally { conn.Close(); } }
private async Task ProcessToDuplicate(ContainerBO container) { container.CommunityUser = null; container.Id = new Guid(); if (container.Rules != null) { container.Rules.ForEach(m => m.Id = new Guid()); } container.IsDefault = false; if (container.ChildContainers != null) { foreach (var m in container.ChildContainers) { await ProcessToDuplicate(m); } } }
public override bool Delete(params object[] keys) { try { this.ConnectionHandler.StartTransaction(IsolationLevel.ReadUncommitted); var containerBO = new ContainerBO(); var obj = containerBO.Get(this.ConnectionHandler, keys); var contents = new ContentBO().Any(ConnectionHandler, container => container.ContainerId == obj.Id); if (contents) { throw new Exception("این قالب در محتوا استفاده شده است و قابل حذف نیست"); } if (!containerBO.Delete(this.ConnectionHandler, keys)) { throw new Exception("خطایی در حذف قالب وجود دارد"); } this.ConnectionHandler.CommitTransaction(); return(true); } catch (KnownException ex) { this.ConnectionHandler.RollBack(); Log.Save(ex.Message, LogType.ApplicationError, ex.Source, ex.StackTrace); throw new KnownException(ex.Message, ex); } catch (Exception ex) { this.ConnectionHandler.RollBack(); Log.Save(ex.Message, LogType.ApplicationError, ex.Source, ex.StackTrace); throw new KnownException(ex.Message, ex); } }
public async Task <ActionResult> DownloadCollectionJson([FromBody] ContainerBO model) { var result = await _containerService.GetCollectionAsJson(model.Id); return(Ok(result)); }
public async Task <ActionResult> GetByNameAsync([FromBody] ContainerBO container) { ContainerBO result = await _containerService.GetByNameAsync(container); return(Ok(result)); }
public async Task <ActionResult> AddContainerFromCommunity([FromBody] ContainerBO container) { string message = await _containerService.AddFromCommunityAsync(container); return(Ok(message)); }
public async Task <string> AddFromCommunityAsync(ContainerBO model) { string result = string.Empty; try { if (!model.CreateNewCollection) { ContainerBO collectionToDelete = await GetByNameAsync(model); if (collectionToDelete != null) { await DeleteAsync(collectionToDelete.Id); } } string collectionName = model.Name; int counter = 1; while (true) { var existCollection = await _collectionQueries.GetByNameAsync(model.Name.ToLower()); if (existCollection == null) { break; } model.Name = $"{collectionName} - ({counter})"; counter++; } var catalogsInformation = await _catalogQueries.GetAsync(); var existEnvironment = catalogsInformation.FirstOrDefault(rec => rec.CatalogType == "EnvironmentType" && rec.Name.ToLower() == model.CatalogEnvironmentType.Name.ToLower()); if (existEnvironment != null) { model.EnvironmentType = existEnvironment.Id; } else { var newEnvironmentType = await _catalogService.AddAsync(new CatalogBO { CatalogType = "EnvironmentType", Description = model.CatalogEnvironmentType.Name, Name = model.CatalogEnvironmentType.Name }); model.EnvironmentType = newEnvironmentType.Id; } if (model.RuleDetailsDestinationId != null && model.ContainerDestination != null && !string.IsNullOrEmpty(model.ContainerDestination.DestinationName)) { bool createDestinationTable = true; var existDestinationTable = catalogsInformation.FirstOrDefault(rec => rec.CatalogType == "RuleDetailsDestinationType" && rec.Name.ToLower() == model.ContainerDestination.DestinationName.ToLower()); if (existDestinationTable != null) { List <DestinationTableColumn> destinationTableInDbColumns = JsonConvert.DeserializeObject <List <DestinationTableColumn> >(model.ContainerDestination.DestinationStructure); var listColumnsFromDestination = await _edFiRuleExecutionLogDetailQueries.GetColumnsByTableAsync(model.ContainerDestination.DestinationName, "destination"); if (destinationTableInDbColumns.Count == listColumnsFromDestination.Count) { createDestinationTable = false; foreach (var columnInDestinationTable in destinationTableInDbColumns) { var existColumn = listColumnsFromDestination.FirstOrDefault(rec => rec.Name.ToLower() == columnInDestinationTable.Name.ToLower() && rec.Type == columnInDestinationTable.Type.ToLower() && rec.IsNullable == columnInDestinationTable.IsNullable); if (existColumn == null) { createDestinationTable = true; break; } } if (!createDestinationTable) { model.RuleDetailsDestinationId = existDestinationTable.Id; } else { int counterTable = 1; string newDestinationTableName = $"{model.ContainerDestination.DestinationName}_{counterTable}"; while (true) { existDestinationTable = catalogsInformation.FirstOrDefault(rec => rec.CatalogType == "RuleDetailsDestinationType" && rec.Name.ToLower() == newDestinationTableName.ToLower()); if (existDestinationTable == null) { break; } counterTable++; newDestinationTableName = $"{model.ContainerDestination.DestinationName}_{counterTable}"; } model.ContainerDestination.DestinationName = newDestinationTableName; } } } if (createDestinationTable) { var newDestinationTable = await _catalogService.AddAsync(new CatalogBO { CatalogType = "RuleDetailsDestinationType", Description = model.ContainerDestination.DestinationName, Name = model.ContainerDestination.DestinationName }); model.RuleDetailsDestinationId = newDestinationTable.Id; bool existDestinationTableInDb = await _edFiRuleExecutionLogDetailQueries.ExistExportTableFromRuleExecutionLogAsync(model.ContainerDestination.DestinationName, "destination"); if (!existDestinationTableInDb) { List <DestinationTableColumn> destinationTableInDbColumns = JsonConvert.DeserializeObject <List <DestinationTableColumn> >(model.ContainerDestination.DestinationStructure); List <string> sqlColumns = new List <string>(); foreach (var column in destinationTableInDbColumns) { string isNull = column.IsNullable ? "NULL" : "NOT NULL"; if (column.Name == "id" && column.Type == "int") { sqlColumns.Add("[Id] [int] IDENTITY(1,1) NOT NULL"); } else if (column.Type.Contains("varchar")) { sqlColumns.Add($"[{column.Name}] [{column.Type}](max) {isNull}"); } else if (column.Type.Contains("datetime")) { sqlColumns.Add($"[{column.Name}] [{column.Type}](7) {isNull}"); } else { sqlColumns.Add($"[{column.Name}] [{column.Type}] {isNull}"); } } string sqlCreate = $"CREATE TABLE [destination].[{model.ContainerDestination.DestinationName}]({string.Join(",", sqlColumns)}) "; await _edFiRuleExecutionLogDetailCommands.ExecuteSqlAsync(sqlCreate); } } } Container newCollection = new Container { ContainerTypeId = 1, Description = model.Description, Name = model.Name, IsDefault = false, EnvironmentType = model.EnvironmentType, RuleDetailsDestinationId = model.RuleDetailsDestinationId, ChildContainers = new List <Container>() }; if (model.ChildContainers != null && model.ChildContainers.Any()) { model.ChildContainers.ForEach(rec => { Container newChildContainer = new Container { ContainerTypeId = 2, Description = rec.Description, Name = rec.Name, IsDefault = false, EnvironmentType = 0, RuleDetailsDestinationId = null, Rules = new List <Rule>() }; if (rec.Rules != null && rec.Rules.Any()) { rec.Rules.ForEach(recRule => { newChildContainer.Rules.Add(new Rule { Description = recRule.Description, DiagnosticSql = recRule.DiagnosticSql, //Enabled = true, ErrorMessage = recRule.ErrorMessage, MaxNumberResults = recRule.MaxNumberResults, Name = recRule.Name, ErrorSeverityLevel = recRule.ErrorSeverityLevel, Resolution = recRule.Resolution, RuleIdentification = recRule.RuleIdentification, Version = recRule.Version }); }); } newCollection.ChildContainers.Add(newChildContainer); }); } var newCollectionFromDatabase = await _collectionCommands.AddAsync(newCollection); if (newCollectionFromDatabase != null) { var listTags = await _tagQueries.GetAsync(); if (model.Tags != null && model.Tags.Any()) { foreach (var tag in model.Tags) { var existTag = listTags.FirstOrDefault(rec => rec.Name.ToLower() == tag.Name.ToLower()); if (existTag == null) { existTag = await _tagCommands.AddAsync(new Tag { Description = tag.Description, IsPublic = true, Name = tag.Name.ToUpper() }); listTags.Add(existTag); } await _tagCommands.AddTagToEntityAsync(new TagEntity { ContainerId = newCollectionFromDatabase.Id, TagId = existTag.Id }); } } if (model.ChildContainers != null && model.ChildContainers.Any()) { foreach (var childContainer in model.ChildContainers) { var newChildContainer = newCollectionFromDatabase.ChildContainers.FirstOrDefault(rec => rec.Name == childContainer.Name); if (childContainer.Tags != null && childContainer.Tags.Any()) { foreach (var tag in childContainer.Tags) { var existTag = listTags.FirstOrDefault(rec => rec.Name.ToLower() == tag.Name.ToLower()); if (existTag == null) { existTag = await _tagCommands.AddAsync(new Tag { Description = tag.Description, IsPublic = true, Name = tag.Name.ToUpper() }); listTags.Add(existTag); } await _tagCommands.AddTagToEntityAsync(new TagEntity { ContainerId = newChildContainer.Id, TagId = existTag.Id }); } } if (childContainer.Rules != null && childContainer.Rules.Any()) { foreach (var rule in childContainer.Rules) { var newRule = newChildContainer.Rules.FirstOrDefault(rec => rec.Name == rule.Name); if (rule.Tags != null && rule.Tags.Any()) { foreach (var tag in rule.Tags) { var existTag = listTags.FirstOrDefault(rec => rec.Name.ToLower() == tag.Name.ToLower()); if (existTag == null) { existTag = await _tagCommands.AddAsync(new Tag { Description = tag.Description, IsPublic = true, Name = tag.Name.ToUpper() }); listTags.Add(existTag); } await _tagCommands.AddTagToEntityAsync(new TagEntity { RuleId = newRule.Id, TagId = existTag.Id }); } } } } } } } } catch (Exception ex) { result = ex.Message; } return(result); }
public async Task <ContainerBO> GetToCommunityAsync(Guid containerId) { Container container = await this._collectionQueries.GetAsync(containerId); var collectionCategory = new ContainerBO(); collectionCategory.Id = container.Id; collectionCategory.Name = container.Name; collectionCategory.Description = container.Description; collectionCategory.EnvironmentType = container.EnvironmentType; collectionCategory.ChildContainers = new List <ContainerBO>(); collectionCategory.ContainerTypeId = container.ContainerTypeId; //collectionCategory.CreatedByUserId = container.CreatedByUserId; collectionCategory.Tags = new List <TagBO>(); collectionCategory.Tags = (await _tagQueries.GetByContainerIdAsync(collectionCategory.Id)).Select(rec => new TagBO(rec)).ToList(); collectionCategory.RuleDetailsDestinationId = container.RuleDetailsDestinationId; var environmentCatalog = await _catalogQueries.GetAsync(container.EnvironmentType); if (environmentCatalog != null) { collectionCategory.CatalogEnvironmentType = new CatalogBO { CatalogType = environmentCatalog.CatalogType, Description = environmentCatalog.Description, Name = environmentCatalog.Name } } ; if (collectionCategory.Tags.Any()) { collectionCategory.Tags = collectionCategory.Tags.Where(rec => rec.IsPublic).ToList(); } if (collectionCategory.RuleDetailsDestinationId != null) { var existCatalog = await _catalogQueries.GetAsync(collectionCategory.RuleDetailsDestinationId.Value); if (existCatalog != null) { var listColumns = await _edFiRuleExecutionLogDetailQueries.GetColumnsByTableAsync(existCatalog.Name, "destination"); if (listColumns != null) { collectionCategory.ContainerDestination = new ContainerDestinationBO { CatalogId = existCatalog.Id, ContainerId = collectionCategory.Id, DestinationName = existCatalog.Name, DestinationStructure = JsonConvert.SerializeObject(listColumns) }; } } } foreach (var itemContainer in container.ChildContainers) { var category = new ContainerBO { Id = itemContainer.Id, Name = itemContainer.Name, Description = itemContainer.Description, ParentContainerId = itemContainer.ParentContainerId, Rules = new List <RuleBO>(), Tags = new List <TagBO>() }; category.Tags = (await _tagQueries.GetByContainerIdAsync(itemContainer.Id)).Select(rec => new TagBO(rec)).ToList(); category.Rules = await _ruleService.GetByContainerIdAsync(itemContainer.Id); if (category.Tags.Any()) { category.Tags = category.Tags.Where(rec => rec.IsPublic).ToList(); } if (category.Rules.Any()) { category.Rules.ForEach(rec => { if (rec.Tags != null && rec.Tags.Any()) { rec.Tags = rec.Tags.Where(recTags => recTags.IsPublic).ToList(); } }); } collectionCategory.ChildContainers.Add(category); } return(collectionCategory); }
private ContainerBO MapEntityToModel(Container entity) { ContainerBO model = new ContainerBO { ContainerTypeId = entity.ContainerTypeId, //CreatedByUserId = entity.CreatedByUserId, Description = entity.Description, Id = entity.Id, IsDefault = entity.IsDefault, Name = entity.Name, ParentContainerId = entity.ParentContainerId, EnvironmentType = entity.EnvironmentType, Tags = new List <TagBO>(), RuleDetailsDestinationId = entity.RuleDetailsDestinationId == 0 ? null : entity.RuleDetailsDestinationId, }; model.Tags = _tagQueries.GetByContainerIdAsync(model.Id).Result.Select(rec => new TagBO(rec)).ToList(); if (entity.ContainerType != null) { model.ContainerType = new ContainerTypeBO { Description = entity.ContainerType.Description, Id = entity.ContainerType.Id, Name = entity.ContainerType.Name } } ; //if (entity.CommunityUser != null) // model.CommunityUser = new CommunityUserBO // { // CommunityOrganizationId = entity.CommunityUser.CommunityOrganizationId, // CreateByUserId = entity.CommunityUser.CreateByUserId, // CreatedDate = entity.CommunityUser.CreatedDate, // Email = entity.CommunityUser.Email, // Id = entity.CommunityUser.Id, // LastUpdatedDate = entity.CommunityUser.LastUpdatedDate, // LastUpdatedUserId = entity.CommunityUser.LastUpdatedUserId, // Name = entity.Name // }; if (entity.Rules != null) { model.Rules = entity.Rules.Select(rec => new RuleBO { ContainerId = rec.ContainerId, //CreatedByUserId = rec.CreatedByUserId, Description = rec.Description, DiagnosticSql = rec.DiagnosticSql, //Enabled = rec.Enabled, ErrorMessage = rec.ErrorMessage, ErrorSeverityLevel = rec.ErrorSeverityLevel, Id = rec.Id, Name = rec.Name, Resolution = rec.Name, RuleIdentification = rec.RuleIdentification, Version = rec.Version }).ToList(); } if (entity.ChildContainers != null) { model.ChildContainers = entity.ChildContainers.Select(rec => MapEntityToModel(rec)).ToList(); } return(model); }
private Container MapModelToEntity(ContainerBO model) { Container entity = new Container { ContainerTypeId = model.ContainerTypeId, //CreatedByUserId = model.CreatedByUserId, Description = model.Description, Id = model.Id, IsDefault = model.IsDefault, Name = model.Name, ParentContainerId = model.ParentContainerId, EnvironmentType = model.EnvironmentType, RuleDetailsDestinationId = model.RuleDetailsDestinationId == 0 ? null : model.RuleDetailsDestinationId, }; if (model.ContainerType != null) { entity.ContainerType = new ContainerType { Description = model.ContainerType.Description, Id = model.ContainerType.Id, Name = model.ContainerType.Name } } ; //if (model.CommunityUser != null) // entity.CommunityUser = new CommunityUserModel // { // CommunityOrganizationId = model.CommunityUser.CommunityOrganizationId, // CreateByUserId = model.CommunityUser.CreateByUserId, // CreatedDate = model.CommunityUser.CreatedDate, // Email = model.CommunityUser.Email, // Id = model.CommunityUser.Id, // LastUpdatedDate = model.CommunityUser.LastUpdatedDate, // LastUpdatedUserId = model.CommunityUser.LastUpdatedUserId, // Name = model.Name // }; if (model.ChildContainers != null) { entity.ChildContainers = model.ChildContainers.Select(rec => MapModelToEntity(rec)).ToList(); } if (model.Rules != null) { entity.Rules = model.Rules.Select(rec => new Rule { ContainerId = rec.ContainerId, //CreatedByUserId = rec.CreatedByUserId, Description = rec.Description, DiagnosticSql = rec.DiagnosticSql, //Enabled = rec.Enabled, ErrorMessage = rec.ErrorMessage, ErrorSeverityLevel = rec.ErrorSeverityLevel, Id = rec.Id, Name = rec.Name, Resolution = rec.Name, RuleIdentification = rec.RuleIdentification, Version = rec.Version }).ToList(); } return(entity); }