public override BUSTableColumn Init(TContext context)
        {
            BUSTableColumn businessEntity = base.Init(context);

            businessEntity.ForeignTableId = Guid.Empty;
            businessEntity.Length         = 0;
            return(businessEntity);
        }
Example #2
0
        public override BUSTableColumn DataToBusiness(TableColumn dataEntity, TContext context)
        {
            BUSTableColumn businessEntity = base.DataToBusiness(dataEntity, context);

            // Table
            businessEntity.Table   = context.Tables.FirstOrDefault(i => i.Id == dataEntity.TableId);
            businessEntity.TableId = dataEntity.TableId;

            // Foreign Key
            Table foreignTable = context.Tables
                                 .AsNoTracking()
                                 .Select(t => new
            {
                id           = t.Id,
                name         = t.Name,
                tableColumns = t.TableColumns.Select(tableColumn => new
                {
                    id   = tableColumn.Id,
                    name = tableColumn.Name,
                })
            })
                                 .Select(t => new Table
            {
                Id           = t.id,
                Name         = t.name,
                TableColumns = t.tableColumns.Select(tableColumn => new TableColumn
                {
                    Id   = tableColumn.id,
                    Name = tableColumn.name
                }).ToList()
            })
                                 .FirstOrDefault(i => i.Id == dataEntity.ForeignTableId);

            if (foreignTable != null)
            {
                businessEntity.ForeignTable     = foreignTable;
                businessEntity.ForeignTableId   = foreignTable.Id;
                businessEntity.ForeignTableName = foreignTable.Name;

                TableColumn foreignTableKey = foreignTable.TableColumns.FirstOrDefault(i => i.Id == dataEntity.ForeignTableKeyId);
                if (foreignTableKey != null)
                {
                    businessEntity.ForeignTableKey     = foreignTableKey;
                    businessEntity.ForeignTableKeyId   = foreignTableKey.Id;
                    businessEntity.ForeignTableKeyName = foreignTableKey.Name;
                }
                businessEntity.ExtencionType = dataEntity.ExtencionType;
            }

            businessEntity.Type         = dataEntity.Type;
            businessEntity.IsForeignKey = dataEntity.IsForeignKey;
            businessEntity.IsNullable   = dataEntity.IsNullable;
            businessEntity.Length       = dataEntity.Length;
            businessEntity.OnDelete     = dataEntity.OnDelete;
            businessEntity.OnUpdate     = dataEntity.OnUpdate;

            return(businessEntity);
        }
        public override UITableColumn BusinessToUI(BUSTableColumn businessEntity)
        {
            UITableColumn UIEntity = base.BusinessToUI(businessEntity);

            UIEntity.ForeignTableName    = businessEntity.ForeignTableName;
            UIEntity.ForeignTableKeyName = businessEntity.ForeignTableKeyName;
            UIEntity.IsForeignKey        = businessEntity.IsForeignKey;
            UIEntity.ExtencionType       = businessEntity.ExtencionType;
            UIEntity.Type       = businessEntity.Type;
            UIEntity.IsNullable = businessEntity.IsNullable;
            UIEntity.Length     = businessEntity.Length.ToString();
            UIEntity.OnDelete   = businessEntity.OnDelete;
            UIEntity.OnUpdate   = businessEntity.OnUpdate;
            return(UIEntity);
        }
Example #4
0
        public override TableColumn BusinessToData(TableColumn tableColumn, BUSTableColumn businessEntity, TContext context, bool NewRecord)
        {
            TableColumn dataEntity = base.BusinessToData(tableColumn, businessEntity, context, NewRecord);

            dataEntity.Table             = businessEntity.Table;
            dataEntity.TableId           = businessEntity.TableId;
            dataEntity.ForeignTableId    = businessEntity.ForeignTableId;
            dataEntity.ForeignTableKeyId = businessEntity.ForeignTableKeyId;
            dataEntity.IsForeignKey      = businessEntity.IsForeignKey;
            dataEntity.ExtencionType     = businessEntity.ExtencionType;
            dataEntity.Type       = businessEntity.Type;
            dataEntity.IsNullable = businessEntity.IsNullable;
            dataEntity.Length     = businessEntity.Length;
            dataEntity.OnDelete   = businessEntity.OnDelete;
            dataEntity.OnUpdate   = businessEntity.OnUpdate;
            return(dataEntity);
        }
        public override IEnumerable <ValidationResult> BUSUIValidate(TContext context, BUSTableColumn businessComponent, UITableColumn UIEntity)
        {
            List <ValidationResult> result = base.BUSUIValidate(context, businessComponent, UIEntity).ToList();

            if (string.IsNullOrWhiteSpace(businessComponent.ErrorMessage))
            {
                if (businessComponent.IsForeignKey)
                {
                    if (businessComponent.ForeignTable == null)
                    {
                        result.Add(new ValidationResult(
                                       "Table with this name not found.",
                                       new List <string>()
                        {
                            "ForeignTable"
                        }));
                    }

                    if (businessComponent.ForeignTableKey == null)
                    {
                        result.Add(new ValidationResult(
                                       "Table column with this name not found.",
                                       new List <string>()
                        {
                            "ForeignTable"
                        }));
                    }
                }
            }
            return(result);
        }
        public override BUSTableColumn UIToBusiness(UITableColumn UIEntity, TContext context, IViewInfo viewInfo, bool isNewRecord)
        {
            BUSTableColumn businessEntity = base.UIToBusiness(UIEntity, context, viewInfo, isNewRecord);
            Table          table          = context.Tables
                                            .AsNoTracking()
                                            .Select(t => new
            {
                id           = t.Id,
                name         = t.Name,
                tableColumns = t.TableColumns.Select(tableColumn => new
                {
                    id   = tableColumn.Id,
                    name = tableColumn.Name,
                })
            })
                                            .Select(t => new Table
            {
                Id           = t.id,
                Name         = t.name,
                TableColumns = t.tableColumns.Select(tableColumn => new TableColumn
                {
                    Id   = tableColumn.id,
                    Name = tableColumn.name
                }).ToList()
            })
                                            .FirstOrDefault(n => n.Id.ToString() == ComponentsRecordsInfo.GetSelectedRecord("Table"));

            if (table == null)
            {
                businessEntity.ErrorMessage = "First you need create table.";
            }
            else
            {
                // Если запись новая и она не уникальна, записывается ошибка
                TableColumn tableColumn = table.TableColumns?.FirstOrDefault(n => n.Name == UIEntity.Name);
                if (table.TableColumns != null && tableColumn != null && tableColumn.Id != UIEntity.Id)
                {
                    businessEntity.ErrorMessage = $"Table column with this name is already exists in table {table.Name}.";
                }
                else
                {
                    // Table
                    businessEntity.Table   = table;
                    businessEntity.TableId = table.Id;

                    // Foreign Key
                    if (UIEntity.IsForeignKey)
                    {
                        // Внешняя таблица
                        Table foreignTable = context.Tables
                                             .AsNoTracking()
                                             .Select(t => new
                        {
                            id           = t.Id,
                            name         = t.Name,
                            tableColumns = t.TableColumns.Select(tableColumn => new
                            {
                                id   = tableColumn.Id,
                                name = tableColumn.Name,
                            })
                        })
                                             .Select(t => new Table
                        {
                            Id           = t.id,
                            Name         = t.name,
                            TableColumns = t.tableColumns.Select(tableColumn => new TableColumn
                            {
                                Id   = tableColumn.id,
                                Name = tableColumn.name
                            }).ToList()
                        })
                                             .FirstOrDefault(n => n.Name == UIEntity.ForeignTableName);

                        if (foreignTable != null)
                        {
                            businessEntity.ForeignTable     = foreignTable;
                            businessEntity.ForeignTableId   = foreignTable.Id;
                            businessEntity.ForeignTableName = foreignTable.Name;

                            // Внешний ключ для таблицы
                            TableColumn foreignTableColumn = foreignTable.TableColumns.FirstOrDefault(n => n.Name == UIEntity.ForeignTableKeyName);
                            if (foreignTableColumn != null)
                            {
                                businessEntity.ForeignTableKey     = foreignTableColumn;
                                businessEntity.ForeignTableKeyId   = foreignTableColumn.Id;
                                businessEntity.ForeignTableKeyName = foreignTableColumn.Name;
                                businessEntity.ExtencionType       = UIEntity.ExtencionType;
                                businessEntity.OnDelete            = UIEntity.OnDelete;
                                businessEntity.OnUpdate            = UIEntity.OnUpdate;
                            }
                        }
                    }

                    businessEntity.Type         = UIEntity.Type;
                    businessEntity.IsForeignKey = UIEntity.IsForeignKey;
                    businessEntity.IsNullable   = UIEntity.IsNullable;
                    businessEntity.Length       = Convert.ToInt32(UIEntity.Length);
                }
            }
            return(businessEntity);
        }