public IActionResult GetEntityLibrary(string entityName) { var result = new EntityLibraryItemsResponse() { Success = true, Timestamp = DateTime.UtcNow }; var relMan = new EntityRelationManager(); var relations = relMan.Read().Object; if (string.IsNullOrWhiteSpace(entityName)) { result.Errors.Add(new ErrorModel { Message = "Invalid entity name." }); result.Success = false; return DoResponse(result); } var entity = entMan.ReadEntity(entityName).Object; if (entity == null) { result.Errors.Add(new ErrorModel { Message = "Entity not found." }); result.Success = false; return DoResponse(result); } entity = Helpers.DeepClone(entMan.ReadEntity(entityName).Object); List<object> itemList = new List<object>(); //itemList.Add(new { type = "html", tag = "HTML string", content = "" }); foreach (var field in entity.Fields) { if (!(field is TreeSelectField)) { itemList.Add(new RecordViewFieldItem { FieldId = field.Id, FieldName = field.Name, Meta = CleanupFieldForLibrary(field), EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = field.Name }); } else { TreeSelectField treeField = field as TreeSelectField; var treeRelation = relations.SingleOrDefault(x => x.Id == treeField.RelationId); if (treeRelation == null) //skip if missing relation is used // simple protection continue; Entity relatedEntity = Helpers.DeepClone(entMan.ReadEntity(treeField.RelatedEntityId).Object); if (relatedEntity == null) //skip if missing related entity // simple protection continue; var tree = relatedEntity.RecordTrees.SingleOrDefault(t => t.Id == treeField.SelectedTreeId); if (tree == null) //skip if missing selected tree // simple protection continue; itemList.Add(new RelationTreeItem { RelationId = treeRelation.Id, RelationName = treeRelation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, TreeId = tree.Id, TreeName = tree.Name, Meta = tree, DataName = string.Format("$tree${0}${1}", treeRelation.Name, tree.Name), FieldLabel = "", FieldPlaceholder = "", FieldHelpText = "", FieldRequired = false, FieldLookupList = "", FieldManageView = "" }); } } foreach (var view in entity.RecordViews) { itemList.Add(new RecordViewViewItem { ViewId = view.Id, ViewName = view.Name, Meta = view, EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = string.Format("view{0}", view.Name) }); } foreach (var list in entity.RecordLists) { itemList.Add(new RecordViewListItem { ListId = list.Id, ListName = list.Name, Meta = list, EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = string.Format("$list${0}", list.Name) }); } var entityRelations = relations.Where(x => x.OriginEntityId == entity.Id || x.TargetEntityId == entity.Id).ToList(); foreach (var relation in entityRelations) { Guid relatedEntityId = relation.OriginEntityId == entity.Id ? relation.TargetEntityId : relation.OriginEntityId; Entity relatedEntity = Helpers.DeepClone(entMan.ReadEntity(relatedEntityId).Object); itemList.Add(new EntityRelationOptionsItem { RelationId = relation.Id, RelationName = relation.Name, Direction = "origin-target" }); //TODO validation if (relatedEntity == null) throw new Exception(string.Format("Invalid relation '{0}'. Related entity '{1}' do not exist.", relation.Name, relatedEntityId)); foreach (var field in relatedEntity.Fields) { itemList.Add(new RecordViewRelationFieldItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, FieldId = field.Id, FieldName = field.Name, Meta = CleanupFieldForLibrary(field), DataName = string.Format("$field${0}${1}", relation.Name, field.Name), FieldLabel = "", FieldPlaceholder = "", FieldHelpText = "", FieldRequired = false, FieldLookupList = "" }); } foreach (var view in relatedEntity.RecordViews) { itemList.Add(new RecordViewRelationViewItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, ViewId = view.Id, ViewName = view.Name, Meta = view, DataName = string.Format("$view${0}${1}", relation.Name, view.Name), FieldLabel = "", FieldPlaceholder = "", FieldHelpText = "", FieldRequired = false, FieldLookupList = "", FieldManageView = "" }); } foreach (var list in relatedEntity.RecordLists) { itemList.Add(new RecordViewRelationListItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, ListId = list.Id, ListName = list.Name, Meta = list, DataName = string.Format("$list${0}${1}", relation.Name, list.Name), FieldLabel = "", FieldPlaceholder = "", FieldHelpText = "", FieldRequired = false, FieldLookupList = "", FieldManageView = "" }); } } result.Object = itemList; return DoResponse(result); }
public IActionResult GetEntityLibrary(string entityName) { var result = new EntityLibraryItemsResponse() { Success = true, Timestamp = DateTime.UtcNow }; var relMan = new EntityRelationManager(service.StorageService); if (string.IsNullOrWhiteSpace(entityName)) { result.Errors.Add(new ErrorModel { Message = "Invalid entity name." }); result.Success = false; return DoResponse(result); } var entity = entityManager.ReadEntity(entityName).Object; if (entity == null) { result.Errors.Add(new ErrorModel { Message = "Entity not found." }); result.Success = false; return DoResponse(result); } List<object> itemList = new List<object>(); itemList.Add(new { type = "html", tag = "", content = "" }); foreach (var field in entity.Fields) { itemList.Add(new RecordViewFieldItem { FieldId = field.Id, FieldName = field.Name, Meta = field, EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = field.Name }); } foreach (var view in entity.RecordViews) { itemList.Add(new RecordViewViewItem { ViewId = view.Id, ViewName = view.Name, Meta = view, EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = string.Format("view{0}", view.Name) }); } foreach (var list in entity.RecordLists) { itemList.Add(new RecordViewListItem { ListId = list.Id, ListName = list.Name, Meta = list, EntityId = entity.Id, EntityName = entity.Name, EntityLabel = entity.Label, EntityLabelPlural = entity.LabelPlural, DataName = string.Format("$list${0}", list.Name) }); } var relations = relMan.Read().Object; var entityRelations = relations.Where(x => x.OriginEntityId == entity.Id || x.TargetEntityId == entity.Id).ToList(); foreach (var relation in entityRelations) { Guid relatedEntityId = relation.OriginEntityId == entity.Id ? relation.TargetEntityId : relation.OriginEntityId; Entity relatedEntity = entityManager.ReadEntity(relatedEntityId).Object; //TODO validation if (relatedEntity == null) throw new Exception(string.Format("Invalid relation '{0}'. Related entity '{1}' do not exist.", relation.Name, relatedEntityId)); foreach (var field in relatedEntity.Fields) { itemList.Add(new RecordViewRelationFieldItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, FieldId = field.Id, FieldName = field.Name, Meta = field, DataName = string.Format("$field${0}${1}", relation.Name, field.Name) }); } foreach (var view in relatedEntity.RecordViews) { itemList.Add(new RecordViewRelationViewItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, ViewId = view.Id, ViewName = view.Name, Meta = view, DataName = string.Format("$view${0}${1}", relation.Name, view.Name) }); } foreach (var list in relatedEntity.RecordLists) { itemList.Add(new RecordViewRelationListItem { RelationId = relation.Id, RelationName = relation.Name, EntityId = relatedEntity.Id, EntityName = relatedEntity.Name, EntityLabel = relatedEntity.Label, EntityLabelPlural = relatedEntity.LabelPlural, ListId = list.Id, ListName = list.Name, Meta = list, DataName = string.Format("$list${0}${1}", relation.Name, list.Name) }); } } result.Object = itemList; return DoResponse(result); }