public override UIPickMap BusinessToUI(BUSPickMap businessEntity)
        {
            UIPickMap UIEntity = base.BusinessToUI(businessEntity);

            UIEntity.BusCompFieldName  = businessEntity.BusCompFieldName;
            UIEntity.PickListFieldName = businessEntity.PickListFieldName;
            UIEntity.Constrain         = businessEntity.Constrain;
            return(UIEntity);
        }
        public override PickMap BusinessToData(PickMap pickMap, BUSPickMap businessEntity, TContext context, bool NewRecord)
        {
            PickMap dataEntity = base.BusinessToData(pickMap, businessEntity, context, NewRecord);

            dataEntity.Field           = businessEntity.Field;
            dataEntity.FieldId         = businessEntity.FieldId;
            dataEntity.BusCompFieldId  = businessEntity.BusCompFieldId;
            dataEntity.PickListFieldId = businessEntity.PickListFieldId;
            dataEntity.Constrain       = businessEntity.Constrain;
            return(dataEntity);
        }
        public override BUSPickMap Init(TContext context)
        {
            BUSPickMap businessEntity = base.Init(context);
            Field      field          = context.Fields
                                        .AsNoTracking()
                                        .Select(f => new
            {
                id      = f.Id,
                busComp = f.BusComp,
                pl      = new
                {
                    id      = f.PickListId,
                    busComp = f.PickList.BusComp
                }
            })
                                        .Select(f => new Field
            {
                Id        = f.id,
                BusComp   = f.busComp,
                BusCompId = f.busComp.Id,
                PickList  = new PickList
                {
                    Id        = (Guid)f.pl.id,
                    BusComp   = f.pl.busComp,
                    BusCompId = f.pl.busComp.Id
                }
            })
                                        .FirstOrDefault(i => i.Id.ToString() == ComponentsRecordsInfo.GetSelectedRecord("Field"));

            if (field != null)
            {
                businessEntity.BusComp   = field.BusComp;
                businessEntity.BusCompId = field.BusCompId;
                if (field.PickList?.BusComp != null)
                {
                    businessEntity.PickListBusComp   = field.PickList.BusComp;
                    businessEntity.PickListBusCompId = field.PickList.BusCompId;
                }
            }
            return(businessEntity);
        }
        public ActionResult <object> PickListRecords()
        {
            Control control = viewInfo.CurrentPopupControl ?? viewInfo.CurrentControl;

            if (control?.Field != null)
            {
                // Получение необходимых сущностей
                BusinessComponent busComp = context.BusinessComponents
                                            .AsNoTracking()
                                            .Include(f => f.Fields)
                                            .ThenInclude(pl => pl.PickList)
                                            .Include(f => f.Fields)
                                            .ThenInclude(pl => pl.PickMaps)
                                            .FirstOrDefault(i => i.Id == control.Field.BusCompId);
                TBUSFactory               busFactory       = new TBUSFactory();
                TDataBUSFactory           dataBUSFactory   = new TDataBUSFactory();
                List <TBusinessComponent> businessEntities = new List <TBusinessComponent>();
                Field          field              = busComp.Fields.FirstOrDefault(i => i.Id == control.FieldId);
                PickList       pickList           = field.PickList;
                List <PickMap> pickMaps           = field.PickMaps;
                PickMap        mapForCurrentField = pickMaps.FirstOrDefault(f => f.BusCompFieldId == field.Id);

                // Текущая запись
                dynamic currentRecord = busFactory.GetRecord(null, context, viewInfo, busComp);
                DataBUSPickMapFR <TContext>  dataBUSPickMapFR  = new DataBUSPickMapFR <TContext>();
                DataBUSPickListFR <TContext> dataBUSPickListFR = new DataBUSPickListFR <TContext>();
                BUSUIPickListFR <TContext>   busUIPickListFR   = new BUSUIPickListFR <TContext>();

                // Мапа, на основании которой принимается решение о том, данные какого поля будут отображаться в пиклисте
                if (mapForCurrentField != null)
                {
                    IEnumerable <TTable> dataEntities      = orderedEntities.ToList();
                    List <PickMap>       constrainPickMaps = pickMaps.Where(c => c.Constrain).ToList();
                    if (currentRecord != null)
                    {
                        // Ограничение отбираемых для пиклсита записей по constrain пик мапам
                        constrainPickMaps.ForEach(constrainMap =>
                        {
                            BUSPickMap businessEntity            = dataBUSPickMapFR.DataToBusiness(constrainMap, context);
                            string constrainPickListField        = businessEntity.PickListFieldName;
                            string constrainComponentField       = businessEntity.BusCompFieldName;
                            dynamic constrainComponentFieldValue = currentRecord.GetType().GetProperty(constrainComponentField).GetValue(currentRecord);
                            if (constrainComponentFieldValue != null)
                            {
                                constrainComponentFieldValue = constrainComponentFieldValue.ToString();
                                dataEntities = dataEntities.AsQueryable().Where($"{constrainPickListField} = \"{constrainComponentFieldValue}\"").ToList();
                            }
                            else
                            {
                                dataEntities = new List <TTable>();
                            }
                        });
                    }
                    // Ограничение отбираемых для пиклсита записей по search spec на пиклисте
                    if (!string.IsNullOrWhiteSpace(pickList.SearchSpecification))
                    {
                        var conversionSearchSpecification = Utils.SearchSpecificationConversion(pickList.SearchSpecification, currentRecord);
                        dataEntities = dataEntities.AsQueryable().Where($"{conversionSearchSpecification}").ToList();
                    }

                    // Получение названий для филды с бизнес компоненты и пиклиста для отбора отображаемых записей и получение текущей
                    BUSPickMap businessEntity = dataBUSPickMapFR.DataToBusiness(mapForCurrentField, context);
                    string     componentField = businessEntity.BusCompFieldName;
                    string     pickListField  = businessEntity.PickListFieldName;
                    dataEntities.ToList().ForEach(dataEntity => businessEntities.Add(dataBUSFactory.DataToBusiness(dataEntity, context)));
                    ComponentsContext <TBusinessComponent> .SetPickListContext(businessEntities);

                    IQueryable displayedPickListRecords = businessEntities.AsQueryable().Select($"new(Id, {pickListField} as Value)");
                    UIPickList pickListInfo             = busUIPickListFR.BusinessToUI(dataBUSPickListFR.DataToBusiness(pickList, context));

                    // Установка текущей выбранной записи в пиклисте
                    if (currentRecord != null)
                    {
                        pickListInfo.CurrentRecordId = businessEntities.AsQueryable()
                                                       .FirstOrDefault($"{pickListField} = \"{currentRecord.GetType().GetProperty(componentField).GetValue(currentRecord)}\"")?.Id;
                    }

                    return(JsonConvert.SerializeObject(
                               new Dictionary <string, object>
                    {
                        { "PickListInfo", pickListInfo },
                        { "DisplayedPickListRecords", displayedPickListRecords }
                    }, Formatting.Indented, new JsonSerializerSettings
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    }));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        public override BUSPickMap DataToBusiness(PickMap dataEntity, TContext context)
        {
            BUSPickMap businessEntity = base.DataToBusiness(dataEntity, context);

            // Field
            Field field = context.Fields
                          .AsNoTracking()
                          .Select(f => new
            {
                id        = f.Id,
                busComp   = f.BusComp,
                busCompId = f.BusCompId
            })
                          .Select(f => new Field
            {
                Id        = f.id,
                BusComp   = f.busComp,
                BusCompId = f.busCompId
            })
                          .FirstOrDefault(i => i.Id == dataEntity.FieldId);

            businessEntity.Field     = field;
            businessEntity.FieldId   = field.Id;
            businessEntity.BusComp   = field.BusComp;
            businessEntity.BusCompId = field.BusCompId;

            // BusCompField
            Field busCompField = context.Fields.AsNoTracking().FirstOrDefault(i => i.Id == dataEntity.BusCompFieldId);

            if (busCompField != null)
            {
                businessEntity.BusCompField     = busCompField;
                businessEntity.BusCompFieldId   = busCompField.Id;
                businessEntity.BusCompFieldName = busCompField.Name;
            }

            // PickListField
            Field pickListField = context.Fields
                                  .AsNoTracking()
                                  .Select(f => new
            {
                id        = f.Id,
                name      = f.Name,
                busComp   = f.BusComp,
                busCompId = f.BusCompId
            })
                                  .Select(f => new Field
            {
                Id        = f.id,
                Name      = f.name,
                BusComp   = f.busComp,
                BusCompId = f.busCompId
            })
                                  .FirstOrDefault(i => i.Id == dataEntity.PickListFieldId);

            if (pickListField != null)
            {
                businessEntity.PickListField     = pickListField;
                businessEntity.PickListFieldId   = pickListField.Id;
                businessEntity.PickListFieldName = pickListField.Name;
                businessEntity.PickListBusComp   = pickListField.BusComp;
                businessEntity.PickListBusCompId = pickListField.BusCompId;
            }

            businessEntity.Constrain = dataEntity.Constrain;
            return(businessEntity);
        }
        public override BUSPickMap UIToBusiness(UIPickMap UIEntity, TContext context, IViewInfo viewInfo, bool isNewRecord)
        {
            BUSPickMap businessEntity = base.UIToBusiness(UIEntity, context, viewInfo, isNewRecord);
            Field      field          = context.Fields.FirstOrDefault(i => i.Id.ToString() == ComponentsRecordsInfo.GetSelectedRecord("Field"));

            if (field == null)
            {
                businessEntity.ErrorMessage = "First you need create field.";
            }
            else
            {
                // Если запись новая и она не уникальна, записывается ошибка
                PickMap pickMap = field.PickMaps?.FirstOrDefault(n => n.Name == UIEntity.Name);
                if (pickMap != null && pickMap.Id != UIEntity.Id)
                {
                    businessEntity.ErrorMessage = $"Field pick map with this name is already exists in field {UIEntity.Name}.";
                }
                else
                {
                    businessEntity.Field   = field;
                    businessEntity.FieldId = field.Id;

                    // BusComp and pickList
                    PickList pickList = context.PickLists
                                        .AsNoTracking()
                                        .Select(pl => new
                    {
                        id      = pl.Id,
                        busComp = new
                        {
                            id     = pl.BusCompId,
                            fields = pl.BusComp.Fields.Select(field => new
                            {
                                id   = field.Id,
                                name = field.Name,
                            })
                        }
                    })
                                        .Select(pl => new PickList
                    {
                        Id      = pl.id,
                        BusComp = new BusinessComponent
                        {
                            Id     = pl.busComp.id,
                            Fields = pl.busComp.fields.Select(field => new Field
                            {
                                Id   = field.id,
                                Name = field.name
                            }).ToList()
                        }
                    })
                                        .FirstOrDefault(i => i.Id == field.PickListId);

                    // PickList
                    if (pickList != null)
                    {
                        businessEntity.PickList   = pickList;
                        businessEntity.PickListId = pickList.Id;

                        BusinessComponent busComp = context.BusinessComponents
                                                    .AsNoTracking()
                                                    .Select(bc => new
                        {
                            id     = bc.Id,
                            fields = bc.Fields.Select(field => new
                            {
                                id   = field.Id,
                                name = field.Name,
                            })
                        })
                                                    .Select(bc => new BusinessComponent
                        {
                            Id     = bc.id,
                            Fields = bc.fields.Select(field => new Field
                            {
                                Id   = field.id,
                                Name = field.name
                            }).ToList()
                        })
                                                    .FirstOrDefault(i => i.Id == field.BusCompId);

                        BusinessComponent pickListBusComp = pickList.BusComp;
                        businessEntity.BusComp   = busComp;
                        businessEntity.BusCompId = busComp.Id;

                        if (pickListBusComp != null)
                        {
                            businessEntity.PickListBusComp   = pickListBusComp;
                            businessEntity.PickListBusCompId = pickListBusComp.Id;

                            // BusCompField
                            Field busCompField = busComp.Fields.FirstOrDefault(n => n.Name == UIEntity.BusCompFieldName);
                            if (busCompField != null)
                            {
                                businessEntity.BusCompField     = busCompField;
                                businessEntity.BusCompFieldId   = busCompField.Id;
                                businessEntity.BusCompFieldName = busCompField.Name;
                            }

                            // PickListField
                            Field pickListField = pickListBusComp.Fields.FirstOrDefault(n => n.Name == UIEntity.PickListFieldName);
                            if (pickListField != null)
                            {
                                businessEntity.PickListField     = pickListField;
                                businessEntity.PickListFieldId   = pickListField.Id;
                                businessEntity.PickListFieldName = pickListField.Name;
                            }
                        }
                    }

                    businessEntity.Constrain = UIEntity.Constrain;
                }
            }
            return(businessEntity);
        }
        public override IEnumerable <ValidationResult> BUSUIValidate(TContext context, BUSPickMap businessComponent, UIPickMap UIEntity)
        {
            List <ValidationResult> result = base.BUSUIValidate(context, businessComponent, UIEntity).ToList();

            if (string.IsNullOrWhiteSpace(businessComponent.ErrorMessage))
            {
                if (businessComponent.PickList == null)
                {
                    result.Add(new ValidationResult(
                                   "At first you need to add a picklist to field " + businessComponent.Field.Name + ".",
                                   new List <string>()
                    {
                        "PickList"
                    }));
                }
                else if (businessComponent.PickListBusComp == null)
                {
                    result.Add(new ValidationResult(
                                   "At first you need to add a business component to picklist " + businessComponent.PickList.Name + ".",
                                   new List <string>()
                    {
                        "PickListBusComp"
                    }));
                }
                else
                {
                    if (businessComponent.BusCompField == null)
                    {
                        result.Add(new ValidationResult(
                                       "Field with this name not found.",
                                       new List <string>()
                        {
                            "BusCompField"
                        }));
                    }

                    if (businessComponent.PickListField == null)
                    {
                        result.Add(new ValidationResult(
                                       "Field with this name not found.",
                                       new List <string>()
                        {
                            "PickListField"
                        }));
                    }
                }
            }
            return(result);
        }