public override BUSApplet DataToBusiness(Applet dataEntity, TContext context) { BUSApplet businessEntity = base.DataToBusiness(dataEntity, context); PhysicalRender physicalRender = context.PhysicalRenders.AsNoTracking().FirstOrDefault(i => i.Id == dataEntity.PhysicalRenderId); if (physicalRender != null) { businessEntity.PhysicalRender = physicalRender; businessEntity.PhysicalRenderId = physicalRender.Id; businessEntity.PhysicalRenderName = physicalRender.Name; } BusinessComponent busComp = context.BusinessComponents.AsNoTracking().FirstOrDefault(i => i.Id == dataEntity.BusCompId); if (busComp != null) { businessEntity.BusComp = busComp; businessEntity.BusCompId = busComp.Id; businessEntity.BusCompName = busComp.Name; } businessEntity.Virtual = dataEntity.Virtual; businessEntity.Header = dataEntity.Header; businessEntity.Type = dataEntity.Type; businessEntity.EmptyState = dataEntity.EmptyState; businessEntity.DisplayLines = dataEntity.DisplayLines; businessEntity.Initflag = dataEntity.Initflag; return(businessEntity); }
public override BusinessComponent BusinessToData(BusinessComponent busComp, BUSBusinessComponent businessEntity, TContext context, bool NewRecord) { BusinessComponent dataEntity = base.BusinessToData(busComp, businessEntity, context, NewRecord); dataEntity.Table = businessEntity.Table; dataEntity.TableId = businessEntity.TableId; dataEntity.Routing = "/api/" + GetPermissibleName(businessEntity.Name) + "/"; dataEntity.ShadowCopy = businessEntity.ShadowCopy; return(dataEntity); }
public override void OnRecordDelete(BusinessComponent recordToDelete, DbSet <BusinessComponent> entities, TContext context) { string permissibleName = GetPermissibleName(recordToDelete.Name); if (EntityFileExists(permissibleName, "BusinessComponent")) { DeleteEntityFile(permissibleName, "BusinessComponent"); DeleteEntityFile(EntitiesConfig.DataFR + permissibleName + "FR", "DataBUSFactory"); DeleteEntityFile(permissibleName + "Controller", "BusinessComponentController"); } base.OnRecordDelete(recordToDelete, entities, context); }
public override BUSBusinessComponent DataToBusiness(BusinessComponent dataEntity, TContext context) { BUSBusinessComponent businessEntity = base.DataToBusiness(dataEntity, context); Table table = context.Tables.AsNoTracking().FirstOrDefault(i => i.Id == dataEntity.TableId); if (table != null) { businessEntity.Table = table; businessEntity.TableId = table.Id; businessEntity.TableName = table.Name; } businessEntity.ShadowCopy = dataEntity.ShadowCopy; return(businessEntity); }
public static void GenerateModel(MainContext context, Type entityType, Guid recordId) { SyntaxTree syntaxTree; string entityName = string.Empty; IEnumerable <MemberDeclarationSyntax> members = new List <MemberDeclarationSyntax>(); switch (entityType.Name) { case "Table": Table table = context.Tables .AsNoTracking() .Include(tc => tc.TableColumns) .FirstOrDefault(i => i.Id == recordId); entityName = table.Name; members = CreateTableColumns(table.TableColumns, context); break; case "BusinessComponent": BusinessComponent busComp = context.BusinessComponents .AsNoTracking() .Include(f => f.Fields) .ThenInclude(tc => tc.TableColumn) .Include(f => f.Fields) .ThenInclude(tc => tc.JoinColumn) .FirstOrDefault(i => i.Id == recordId); entityName = busComp.Name; members = CreateFields(busComp.Fields); break; case "Applet": Applet applet = context.Applets .AsNoTracking() .Include(c => c.Controls) .ThenInclude(f => f.Field) .Include(c => c.Columns) .ThenInclude(f => f.Field) .FirstOrDefault(i => i.Id == recordId); entityName = applet.Name; members = CreateColumns(applet.Columns, context).Concat(CreateControls(applet.Controls, context)); break; } string permissibleName = GetPermissibleName(entityName); if (TryGetModel(GSCrmApplication, permissibleName, entityType.Name, out syntaxTree)) { SyntaxNode rootNode = syntaxTree.GetRoot(); ClassDeclarationSyntax classNode = rootNode .DescendantNodes() .OfType <ClassDeclarationSyntax>() .SingleOrDefault(n => n.Identifier.ValueText == permissibleName); List <PropertyDeclarationSyntax> interProps = new List <PropertyDeclarationSyntax>(); classNode.Members.OfType <PropertyDeclarationSyntax>().ToList().ForEach(property => { if (!members.Contains(property) && !property.Identifier.ValueText.StartsWith(SystemPrefix)) { interProps.Add(property); } }); rootNode = rootNode.ReplaceNode(classNode, classNode.WithMembers(List(members.Except(interProps)))); syntaxTree = SyntaxTree(rootNode).WithFilePath(syntaxTree.FilePath); } else { syntaxTree = CreateModel(permissibleName, entityType, members); if (entityType == typeof(Table)) { Compile("GSCrmApplicationContext", syntaxTree); } } WriteTree(GSCrmApplication, permissibleName, entityType.Name, syntaxTree); Compile("GSCrm" + entityType.Name, syntaxTree); }
public ActionResult <object> Pick(RequestPickListModel model) { string newPickedValue = string.Empty; Control control = viewInfo.CurrentPopupControl ?? viewInfo.CurrentControl; Dictionary <string, object> result = new Dictionary <string, object>() { { "ErrorMessages", new object() { } }, { "NewPickedValue", newPickedValue }, { "Status", string.Empty } }; if (control?.Field != null) { // Получение необходимых сущностей TBUSFactory busFactory = new TBUSFactory(); 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); Field field = busComp.Fields.FirstOrDefault(i => i.Id == control.FieldId); PickList pickList = field.PickList; BusinessComponent pickListBusComp = context.BusinessComponents .AsNoTracking() .Include(f => f.Fields) .FirstOrDefault(i => i.Id == pickList.BusCompId); List <PickMap> pickMaps = field.PickMaps; PickMap mapForCurrentField = pickMaps.FirstOrDefault(f => f.BusCompFieldId == field.Id); List <TBusinessComponent> pickListRecords = ComponentsContext <TBusinessComponent> .GetPickListContext(); // Пикнутая и текущая запись dynamic pickedRecord = model.Value ?? busFactory.GetRecord(null, context, viewInfo, pickListBusComp, "Id", model.PickedRecord); dynamic currentRecord = busFactory.GetRecord(null, context, viewInfo, busComp); // Если произошел выбор записи из пиклсита if (model.IsPicked) { // Пик на основе пикмапы pickMaps.Where(c => c.Constrain == false).OrderBy(s => s.Sequence).ToList().ForEach(map => { string componentField = busComp.Fields.FirstOrDefault(i => i.Id == map.BusCompFieldId).Name; string PLComponentField = pickListBusComp.Fields.FirstOrDefault(i => i.Id == map.PickListFieldId).Name; dynamic PLComponentFieldValue = pickedRecord.GetType().GetProperty(PLComponentField).GetValue(pickedRecord); var PLComponentFieldType = PLComponentFieldValue.GetType(); Type componentFieldType = currentRecord.GetType().GetProperty(componentField).GetValue(currentRecord)?.GetType(); if (componentFieldType?.BaseType == typeof(Enum)) { switch (componentFieldType.Name) { case "ActionType": PLComponentFieldValue = (ActionType)Enum.Parse(typeof(ActionType), PLComponentFieldValue); break; } } try { currentRecord.GetType().GetProperty(componentField).SetValue(currentRecord, PLComponentFieldValue); } catch (RuntimeBinderException ex) { result["ErrorMessages"] = Utils.GetErrorsInfo(ex); result["Status"] = "Fail"; } if (map.Id == mapForCurrentField.Id) { newPickedValue = PLComponentFieldValue.ToString(); } }); } // Если пользователь ввел значение сам или стер его else { newPickedValue = string.Empty; string componentField = busComp.Fields.FirstOrDefault(i => i.Id == mapForCurrentField.BusCompFieldId).Name; // Если пиклист не позволяет принять введенное пользователем значение if (pickList.Bounded) { // Если значение не пустое, то выполняется поиск такого значения в пиклисте if (!string.IsNullOrWhiteSpace(model.Value)) { // Если значение найдено, то оно пикается if (pickListRecords.AsQueryable().Any($"{pickListBusComp.Fields.FirstOrDefault(i => i.Id == mapForCurrentField.PickListFieldId).Name} = \"{model.Value}\"")) { dynamic newValue = model.Value; PropertyInfo property = currentRecord.GetType().GetProperty(componentField); if (property?.Name == "ActionType") { newValue = ActionType.None; newPickedValue = "None"; } currentRecord.GetType().GetProperty(componentField).SetValue(currentRecord, newValue); newPickedValue = model.Value; } // Иначе текущая запись никак не меняется и на фронт возвращаетя старое значение else { newPickedValue = currentRecord.GetType().GetProperty(componentField).GetValue(currentRecord).ToString(); result["ErrorMessages"] = new object[] { "This pick list does not allow you to enter your values. Choose a value from the suggested." }; result["Status"] = "Fail"; } } // Иначе в текущюю запись проставляется пустая строка, она же и возвращается на фронт else { // Пик на основе пикмапы pickMaps.Where(c => c.Constrain == false).OrderBy(s => s.Sequence).ToList().ForEach(map => { // Для каждого поля из пикмапы установка дефолтового значения на основании его типа dynamic newValue = null; componentField = busComp.Fields.FirstOrDefault(i => i.Id == map.BusCompFieldId).Name; PropertyInfo property = currentRecord.GetType().GetProperty(componentField); Type propertyType = property.GetType(); if (propertyType.GetTypeInfo().IsValueType) { newValue = Activator.CreateInstance(propertyType); } if (property?.Name == "ActionType") { newValue = ActionType.None; newPickedValue = "None"; } else { newPickedValue = string.Empty; } currentRecord.GetType().GetProperty(componentField).SetValue(currentRecord, newValue); }); } } // Иначе else { currentRecord.GetType().GetProperty(componentField).SetValue(currentRecord, model.Value); newPickedValue = model.Value; } } // Установка текущей записи busFactory.SetRecord(null, context, viewInfo, busComp, currentRecord); } if (result["Status"].ToString() == string.Empty) { result["Status"] = "Done"; } result["NewPickedValue"] = newPickedValue; return(result); }
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 void OnRecordCreate(BusinessComponent recordToCreate, DbSet <BusinessComponent> entities, TContext context) { if (recordToCreate.Table != null) { // Table Table table = context.Tables .AsNoTracking() .Select(t => new { id = t.Id, tableColumns = t.TableColumns.Select(tableColumn => new { id = tableColumn.Id, name = tableColumn.Name }) }) .Select(t => new Table { Id = t.id, TableColumns = t.tableColumns.Select(tableColumn => new TableColumn { Id = tableColumn.id, Name = tableColumn.name }).ToList() }) .FirstOrDefault(i => i.Id == recordToCreate.TableId); // Table columns IEnumerable <TableColumn> tableColumns = table.TableColumns; // Add fields context.Fields.AddRange(new List <Field>() { new Field() { Name = "Id", BusComp = recordToCreate, BusCompId = recordToCreate.Id, TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "Id").Id }, new Field() { Name = "Created", BusComp = recordToCreate, BusCompId = recordToCreate.Id, TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "Created").Id }, new Field() { Name = "CreatedBy", BusComp = recordToCreate, BusCompId = recordToCreate.Id, TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "CreatedBy").Id }, new Field() { Name = "LastUpdated", BusComp = recordToCreate, BusCompId = recordToCreate.Id, TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "LastUpdated").Id }, new Field() { Name = "UpdatedBy", BusComp = recordToCreate, BusCompId = recordToCreate.Id, TableColumnId = tableColumns.FirstOrDefault(n => n.Name == "UpdatedBy").Id } }); context.Entry(recordToCreate.Table).State = EntityState.Unchanged; } base.OnRecordCreate(recordToCreate, entities, context); }