public override UIPickList BusinessToUI(BUSPickList businessEntity)
        {
            UIPickList UIEntity = base.BusinessToUI(businessEntity);

            UIEntity.BusCompName         = businessEntity.BusCompName;
            UIEntity.Bounded             = businessEntity.Bounded;
            UIEntity.SearchSpecification = businessEntity.SearchSpecification;
            return(UIEntity);
        }
        public override BUSPickList UIToBusiness(UIPickList UIEntity, TContext context, IViewInfo viewInfo, bool isNewRecord)
        {
            BUSPickList       businessEntity = base.UIToBusiness(UIEntity, context, viewInfo, isNewRecord);
            BusinessComponent busComp        = context.BusinessComponents.AsNoTracking().FirstOrDefault(n => n.Name == UIEntity.BusCompName);

            if (busComp != null)
            {
                businessEntity.BusComp     = busComp;
                businessEntity.BusCompId   = busComp.Id;
                businessEntity.BusCompName = busComp.Name;
            }
            businessEntity.SearchSpecification = UIEntity.SearchSpecification;
            businessEntity.Bounded             = UIEntity.Bounded;
            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 IEnumerable <ValidationResult> BUSUIValidate(TContext context, BUSPickList businessComponent, UIPickList UIEntity)
        {
            List <ValidationResult> result = base.BUSUIValidate(context, businessComponent, UIEntity).ToList();

            if (string.IsNullOrWhiteSpace(businessComponent.ErrorMessage))
            {
                if (businessComponent.BusComp == null)
                {
                    result.Add(new ValidationResult(
                                   "Business component with this name not found",
                                   new List <string>()
                    {
                        "BusCompName"
                    }));
                }
            }
            return(result);
        }
        public override IEnumerable <ValidationResult> UIValidate(TContext context, IViewInfo viewInfo, UIPickList UIEntity, bool isNewRecord)
        {
            List <ValidationResult> result = base.UIValidate(context, viewInfo, UIEntity, isNewRecord).ToList();
            PickList pickList = context.PickLists.AsNoTracking().FirstOrDefault(n => n.Name == UIEntity.Name);

            if (pickList != null && pickList.Id != UIEntity.Id)
            {
                result.Add(new ValidationResult("Picklist with this name is already exists.", new List <string>()
                {
                    "Name"
                }));
            }
            if (string.IsNullOrWhiteSpace(UIEntity.BusCompName))
            {
                result.Add(new ValidationResult(
                               "Business component name is a required field",
                               new List <string>()
                {
                    "BusCompName"
                }));
            }
            return(result);
        }