internal PageDataModel(BaseErpPageModel erpPageModel) { this.erpPageModel = erpPageModel ?? throw new NullReferenceException(nameof(erpPageModel)); InitContextRelatedData(erpPageModel); if (erpPageModel.ErpRequestContext != null && erpPageModel.ErpRequestContext.Page != null) { InitDataSources(erpPageModel.ErpRequestContext.Page); } }
public static BaseErpPageModel CreatePageModelSimulation( ErpRequestContext erpRequestContext, ErpUser currentUser ) { var pageModel = new BaseErpPageModel(); pageModel.ErpRequestContext = erpRequestContext; pageModel.currentUser = currentUser; pageModel.AppName = erpRequestContext.App != null ? erpRequestContext.App.Name : ""; pageModel.AreaName = erpRequestContext.SitemapArea != null ? erpRequestContext.SitemapArea.Name : ""; pageModel.NodeName = erpRequestContext.SitemapNode != null ? erpRequestContext.SitemapNode.Name : ""; pageModel.PageName = erpRequestContext.Page != null ? erpRequestContext.Page.Name : ""; pageModel.RecordId = erpRequestContext.RecordId; pageModel.DataModel = new PageDataModel(pageModel); return(pageModel); }
private void InitContextRelatedData(BaseErpPageModel erpPageModel) { if (erpPageModel != null) { //if page request context is set, it is used, otherwise, the requestContext is used ErpRequestContext reqCtx = erpPageModel.ErpRequestContext; var currentUserMPW = new MPW(MPT.Object, erpPageModel.CurrentUser); Properties.Add("CurrentUser", currentUserMPW); if (erpPageModel.CurrentUser != null) { currentUserMPW.Properties.Add("CreatedOn", new MPW(MPT.Object, erpPageModel.CurrentUser.CreatedOn)); currentUserMPW.Properties.Add("Email", new MPW(MPT.Object, erpPageModel.CurrentUser.Email)); currentUserMPW.Properties.Add("FirstName", new MPW(MPT.Object, erpPageModel.CurrentUser.FirstName)); currentUserMPW.Properties.Add("Id", new MPW(MPT.Object, erpPageModel.CurrentUser.Id)); currentUserMPW.Properties.Add("Image", new MPW(MPT.Object, erpPageModel.CurrentUser.Image)); currentUserMPW.Properties.Add("LastName", new MPW(MPT.Object, erpPageModel.CurrentUser.LastName)); currentUserMPW.Properties.Add("Username", new MPW(MPT.Object, erpPageModel.CurrentUser.Username)); var rolesMPW = new MPW(MPT.Object, erpPageModel.CurrentUser.Roles); currentUserMPW.Properties.Add("Roles", rolesMPW); if (erpPageModel.CurrentUser.Roles != null) { for (int i = 0; i < erpPageModel.CurrentUser.Roles.Count; i++) { var roleMPW = new MPW(MPT.Object, erpPageModel.CurrentUser.Roles[i]); rolesMPW.Properties.Add($"[{i}]", roleMPW); roleMPW.Properties.Add($"Id", new MPW(MPT.Object, erpPageModel.CurrentUser.Roles[i].Id)); roleMPW.Properties.Add($"Name", new MPW(MPT.Object, erpPageModel.CurrentUser.Roles[i].Name)); } } } Properties.Add("ReturnUrl", new MPW(MPT.Object, erpPageModel.ReturnUrl)); //this is the case of with related/parent entity and related/parent record id set if (erpPageModel.RecordId != null && erpPageModel.ParentRecordId != null && erpPageModel.RelationId != null && reqCtx != null && reqCtx.Entity != null && reqCtx.ParentEntity != null) { EntityQuery eq = new EntityQuery(reqCtx.ParentEntity.Name, "*", EntityQuery.QueryEQ("id", erpPageModel.ParentRecordId.Value)); var response = recMan.Find(eq); if (!response.Success) { throw new Exception(response.Message); } else if (response.Object.Data.Count > 0) { Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, response.Object.Data[0])); } else { Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, null)); } eq = new EntityQuery(reqCtx.Entity.Name, "*", EntityQuery.QueryEQ("id", erpPageModel.RecordId.Value)); response = recMan.Find(eq); if (!response.Success) { throw new Exception(response.Message); } else if (response.Object.Data.Count > 0) { Properties.Add("Record", new MPW(MPT.EntityRecord, response.Object.Data[0])); } else { Properties.Add("Record", new MPW(MPT.EntityRecord, null)); } } //this is the case of Create and List page with related/parent entity amd with no related/parent record set else if (erpPageModel.RecordId == null && erpPageModel.ParentRecordId != null && erpPageModel.RelationId != null && reqCtx != null && reqCtx.Entity != null && reqCtx.ParentEntity != null) { EntityQuery eq = new EntityQuery(reqCtx.ParentEntity.Name, "*", EntityQuery.QueryEQ("id", erpPageModel.ParentRecordId)); var response = recMan.Find(eq); if (!response.Success) { throw new Exception(response.Message); } else if (response.Object.Data.Count > 0) { Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, response.Object.Data[0])); } else { Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, null)); } Properties.Add("Record", new MPW(MPT.EntityRecord, null)); } //this is the case with no parent (relations/parents cases are checked above) else if (erpPageModel.RecordId != null && reqCtx != null && reqCtx.Entity != null) { EntityQuery eq = new EntityQuery(reqCtx.Entity.Name, "*", EntityQuery.QueryEQ("id", reqCtx.RecordId)); var response = recMan.Find(eq); if (!response.Success) { throw new Exception(response.Message); } else if (response.Object.Data.Count > 0) { Properties.Add("Record", new MPW(MPT.EntityRecord, response.Object.Data[0])); } else { Properties.Add("Record", new MPW(MPT.EntityRecord, null)); } Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, null)); } //this is the case with no entity page else { Properties.Add("Record", new MPW(MPT.EntityRecord, null)); Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, null)); } var validationMPW = new MPW(MPT.Object, erpPageModel.Validation); if (erpPageModel.Validation != null) { validationMPW.Properties.Add("Message", new MPW(MPT.Object, erpPageModel.Validation.Message)); var errorsMPW = new MPW(MPT.Object, erpPageModel.Validation.Errors); if (erpPageModel.Validation.Errors != null) { for (int i = 0; i < erpPageModel.Validation.Errors.Count; i++) { var errorMPW = new MPW(MPT.Object, erpPageModel.Validation.Errors[i]); errorsMPW.Properties.Add($"[{i}]", errorMPW); errorMPW.Properties.Add($"Index", new MPW(MPT.Object, erpPageModel.Validation.Errors[i].Index)); errorMPW.Properties.Add($"IsSystem", new MPW(MPT.Object, erpPageModel.Validation.Errors[i].IsSystem)); errorMPW.Properties.Add($"Message", new MPW(MPT.Object, erpPageModel.Validation.Errors[i].Message)); errorMPW.Properties.Add($"PropertyName", new MPW(MPT.Object, erpPageModel.Validation.Errors[i].PropertyName)); } } validationMPW.Properties.Add("Errors", errorsMPW); } Properties.Add("Validation", validationMPW); if (reqCtx != null) { Properties.Add("RecordId", new MPW(MPT.Object, reqCtx.RecordId)); Properties.Add("ParentRecordId", new MPW(MPT.Object, reqCtx.ParentRecordId)); Properties.Add("RelationId", new MPW(MPT.Object, reqCtx.RelationId)); Properties.Add("PageContext", new MPW(MPT.Object, reqCtx.PageContext)); var pageMPW = new MPW(MPT.Object, reqCtx.Page); if (reqCtx.Page != null) { pageMPW.Properties.Add("AppId", new MPW(MPT.Object, reqCtx.Page.AppId)); pageMPW.Properties.Add("AreaId", new MPW(MPT.Object, reqCtx.Page.AreaId)); pageMPW.Properties.Add("EntityId", new MPW(MPT.Object, reqCtx.Page.EntityId)); pageMPW.Properties.Add("IconClass", new MPW(MPT.Object, reqCtx.Page.IconClass)); pageMPW.Properties.Add("Id", new MPW(MPT.Object, reqCtx.Page.Id)); pageMPW.Properties.Add("IsRazorBody", new MPW(MPT.Object, reqCtx.Page.IsRazorBody)); pageMPW.Properties.Add("Label", new MPW(MPT.Object, reqCtx.Page.Label)); pageMPW.Properties.Add("Name", new MPW(MPT.Object, reqCtx.Page.Name)); pageMPW.Properties.Add("NodeId", new MPW(MPT.Object, reqCtx.Page.NodeId)); pageMPW.Properties.Add("System", new MPW(MPT.Object, reqCtx.Page.System)); pageMPW.Properties.Add("Type", new MPW(MPT.Object, reqCtx.Page.Type)); pageMPW.Properties.Add("Weight", new MPW(MPT.Object, reqCtx.Page.Weight)); } Properties.Add("Page", pageMPW); var parentPageMPW = new MPW(MPT.Object, reqCtx.ParentPage); if (reqCtx.ParentPage != null) { parentPageMPW.Properties.Add("AppId", new MPW(MPT.Object, reqCtx.ParentPage.AppId)); parentPageMPW.Properties.Add("AreaId", new MPW(MPT.Object, reqCtx.ParentPage.AreaId)); parentPageMPW.Properties.Add("EntityId", new MPW(MPT.Object, reqCtx.ParentPage.EntityId)); parentPageMPW.Properties.Add("IconClass", new MPW(MPT.Object, reqCtx.ParentPage.IconClass)); parentPageMPW.Properties.Add("Id", new MPW(MPT.Object, reqCtx.ParentPage.Id)); parentPageMPW.Properties.Add("IsRazorBody", new MPW(MPT.Object, reqCtx.ParentPage.IsRazorBody)); parentPageMPW.Properties.Add("Label", new MPW(MPT.Object, reqCtx.ParentPage.Label)); parentPageMPW.Properties.Add("Name", new MPW(MPT.Object, reqCtx.ParentPage.Name)); parentPageMPW.Properties.Add("NodeId", new MPW(MPT.Object, reqCtx.ParentPage.NodeId)); parentPageMPW.Properties.Add("System", new MPW(MPT.Object, reqCtx.ParentPage.System)); parentPageMPW.Properties.Add("Type", new MPW(MPT.Object, reqCtx.ParentPage.Type)); parentPageMPW.Properties.Add("Weight", new MPW(MPT.Object, reqCtx.ParentPage.Weight)); } Properties.Add("ParentPage", parentPageMPW); var entityMPW = new MPW(MPT.Object, reqCtx.Entity); if (reqCtx.Entity != null) { entityMPW.Properties.Add("Color", new MPW(MPT.Object, reqCtx.Entity.Color)); entityMPW.Properties.Add("Fields", new MPW(MPT.Object, reqCtx.Entity.Fields)); entityMPW.Properties.Add("IconName", new MPW(MPT.Object, reqCtx.Entity.IconName)); entityMPW.Properties.Add("Id", new MPW(MPT.Object, reqCtx.Entity.Id)); entityMPW.Properties.Add("Label", new MPW(MPT.Object, reqCtx.Entity.Label)); entityMPW.Properties.Add("LabelPlural", new MPW(MPT.Object, reqCtx.Entity.LabelPlural)); entityMPW.Properties.Add("Name", new MPW(MPT.Object, reqCtx.Entity.Name)); entityMPW.Properties.Add("RecordScreenIdField", new MPW(MPT.Object, reqCtx.Entity.RecordScreenIdField)); entityMPW.Properties.Add("System", new MPW(MPT.Object, reqCtx.Entity.System)); } Properties.Add("Entity", entityMPW); var parentEntityMPW = new MPW(MPT.Object, reqCtx.ParentEntity); if (reqCtx.ParentEntity != null) { parentEntityMPW.Properties.Add("Color", new MPW(MPT.Object, reqCtx.ParentEntity.Color)); parentEntityMPW.Properties.Add("Fields", new MPW(MPT.Object, reqCtx.ParentEntity.Fields)); parentEntityMPW.Properties.Add("IconName", new MPW(MPT.Object, reqCtx.ParentEntity.IconName)); parentEntityMPW.Properties.Add("Id", new MPW(MPT.Object, reqCtx.ParentEntity.Id)); parentEntityMPW.Properties.Add("Label", new MPW(MPT.Object, reqCtx.ParentEntity.Label)); parentEntityMPW.Properties.Add("LabelPlural", new MPW(MPT.Object, reqCtx.ParentEntity.LabelPlural)); parentEntityMPW.Properties.Add("Name", new MPW(MPT.Object, reqCtx.ParentEntity.Name)); parentEntityMPW.Properties.Add("RecordScreenIdField", new MPW(MPT.Object, reqCtx.ParentEntity.RecordScreenIdField)); parentEntityMPW.Properties.Add("System", new MPW(MPT.Object, reqCtx.ParentEntity.System)); } Properties.Add("ParentEntity", parentEntityMPW); var detectionMPW = new MPW(MPT.Object, reqCtx.Detection); if (reqCtx.Detection != null) { var deviceMPW = new MPW(MPT.Object, reqCtx.Detection.Device); if (reqCtx.Detection.Device != null) { deviceMPW.Properties.Add("Crawler", new MPW(MPT.Object, reqCtx.Detection.Device.Crawler)); deviceMPW.Properties.Add("Type", new MPW(MPT.Object, reqCtx.Detection.Device.Type)); } detectionMPW.Properties.Add("Device", deviceMPW); } Properties.Add("Detection", detectionMPW); var appMPW = new MPW(MPT.Object, reqCtx.App); if (reqCtx.App != null) { appMPW.Properties.Add("Author", new MPW(MPT.Object, reqCtx.App.Author)); appMPW.Properties.Add("Color", new MPW(MPT.Object, reqCtx.App.Color)); appMPW.Properties.Add("Description", new MPW(MPT.Object, reqCtx.App.Description)); appMPW.Properties.Add("IconClass", new MPW(MPT.Object, reqCtx.App.IconClass)); appMPW.Properties.Add("Id", new MPW(MPT.Object, reqCtx.App.Id)); appMPW.Properties.Add("Label", new MPW(MPT.Object, reqCtx.App.Label)); appMPW.Properties.Add("Name", new MPW(MPT.Object, reqCtx.App.Name)); var sitemapMPW = new MPW(MPT.Object, reqCtx.App.Sitemap); if (reqCtx.App.Sitemap != null) { var areasMPW = new MPW(MPT.Object, reqCtx.App.Sitemap.Areas); sitemapMPW.Properties.Add("Areas", sitemapMPW); for (int i = 0; i < reqCtx.App.Sitemap.Areas.Count; i++) { var area = reqCtx.App.Sitemap.Areas[i]; var areaMPW = new MPW(MPT.Object, area); areaMPW.Properties.Add($"Access", new MPW(MPT.Object, area.Access)); areaMPW.Properties.Add($"Color", new MPW(MPT.Object, area.Color)); areaMPW.Properties.Add($"Description", new MPW(MPT.Object, area.Description)); areaMPW.Properties.Add($"IconClass", new MPW(MPT.Object, area.IconClass)); areaMPW.Properties.Add($"Id", new MPW(MPT.Object, area.Id)); areaMPW.Properties.Add($"Label", new MPW(MPT.Object, area.Label)); areaMPW.Properties.Add($"Name", new MPW(MPT.Object, area.Name)); var areaNodesMPW = new MPW(MPT.Object, area.Nodes); for (int j = 0; j < area.Nodes.Count; j++) { var node = area.Nodes[j]; var nodeMPW = new MPW(MPT.Object, area); nodeMPW.Properties.Add($"Access", new MPW(MPT.Object, node.Access)); nodeMPW.Properties.Add($"EntityId", new MPW(MPT.Object, node.EntityId)); nodeMPW.Properties.Add($"GroupName", new MPW(MPT.Object, node.GroupName)); nodeMPW.Properties.Add($"IconClass", new MPW(MPT.Object, node.IconClass)); nodeMPW.Properties.Add($"Id", new MPW(MPT.Object, node.Id)); nodeMPW.Properties.Add($"Label", new MPW(MPT.Object, node.Label)); nodeMPW.Properties.Add($"Name", new MPW(MPT.Object, node.Name)); nodeMPW.Properties.Add($"Type", new MPW(MPT.Object, node.Type)); nodeMPW.Properties.Add($"Url", new MPW(MPT.Object, node.Url)); nodeMPW.Properties.Add($"Weight", new MPW(MPT.Object, node.Weight)); areaNodesMPW.Properties.Add($"[{j}]", nodeMPW); } areaMPW.Properties.Add($"Nodes", areaNodesMPW); areasMPW.Properties.Add($"[{i}]", areaMPW); } } appMPW.Properties.Add("Sitemap", sitemapMPW); } Properties.Add("App", appMPW); var sitemapNodeMPW = new MPW(MPT.Object, reqCtx.SitemapNode); if (reqCtx.SitemapNode != null) { sitemapNodeMPW.Properties.Add($"Access", new MPW(MPT.Object, reqCtx.SitemapNode.Access)); sitemapNodeMPW.Properties.Add($"EntityId", new MPW(MPT.Object, reqCtx.SitemapNode.EntityId)); sitemapNodeMPW.Properties.Add($"GroupName", new MPW(MPT.Object, reqCtx.SitemapNode.GroupName)); sitemapNodeMPW.Properties.Add($"IconClass", new MPW(MPT.Object, reqCtx.SitemapNode.IconClass)); sitemapNodeMPW.Properties.Add($"Id", new MPW(MPT.Object, reqCtx.SitemapNode.Id)); sitemapNodeMPW.Properties.Add($"Label", new MPW(MPT.Object, reqCtx.SitemapNode.Label)); sitemapNodeMPW.Properties.Add($"Name", new MPW(MPT.Object, reqCtx.SitemapNode.Name)); sitemapNodeMPW.Properties.Add($"Type", new MPW(MPT.Object, reqCtx.SitemapNode.Type)); sitemapNodeMPW.Properties.Add($"Url", new MPW(MPT.Object, reqCtx.SitemapNode.Url)); sitemapNodeMPW.Properties.Add($"Weight", new MPW(MPT.Object, reqCtx.SitemapNode.Weight)); } Properties.Add("SitemapNode", sitemapNodeMPW); var sitemapAreaMPW = new MPW(MPT.Object, reqCtx.SitemapArea); if (reqCtx.SitemapArea != null) { var area = reqCtx.SitemapArea; sitemapAreaMPW.Properties.Add($"Access", new MPW(MPT.Object, area.Access)); sitemapAreaMPW.Properties.Add($"Color", new MPW(MPT.Object, area.Color)); sitemapAreaMPW.Properties.Add($"Description", new MPW(MPT.Object, area.Description)); sitemapAreaMPW.Properties.Add($"IconClass", new MPW(MPT.Object, area.IconClass)); sitemapAreaMPW.Properties.Add($"Id", new MPW(MPT.Object, area.Id)); sitemapAreaMPW.Properties.Add($"Label", new MPW(MPT.Object, area.Label)); sitemapAreaMPW.Properties.Add($"Name", new MPW(MPT.Object, area.Name)); var areaNodesMPW = new MPW(MPT.Object, area.Nodes); for (int j = 0; j < area.Nodes.Count; j++) { var node = area.Nodes[j]; var nodeMPW = new MPW(MPT.Object, area); nodeMPW.Properties.Add($"Access", new MPW(MPT.Object, node.Access)); nodeMPW.Properties.Add($"EntityId", new MPW(MPT.Object, node.EntityId)); nodeMPW.Properties.Add($"GroupName", new MPW(MPT.Object, node.GroupName)); nodeMPW.Properties.Add($"IconClass", new MPW(MPT.Object, node.IconClass)); nodeMPW.Properties.Add($"Id", new MPW(MPT.Object, node.Id)); nodeMPW.Properties.Add($"Label", new MPW(MPT.Object, node.Label)); nodeMPW.Properties.Add($"Name", new MPW(MPT.Object, node.Name)); nodeMPW.Properties.Add($"Type", new MPW(MPT.Object, node.Type)); nodeMPW.Properties.Add($"Url", new MPW(MPT.Object, node.Url)); nodeMPW.Properties.Add($"Weight", new MPW(MPT.Object, node.Weight)); areaNodesMPW.Properties.Add($"[{j}]", nodeMPW); } sitemapAreaMPW.Properties.Add($"Nodes", areaNodesMPW); } Properties.Add("SitemapArea", sitemapAreaMPW); } EntityRecord queryRecord = new EntityRecord(); if (reqCtx != null && reqCtx.PageContext != null && reqCtx.PageContext.HttpContext != null) { var httpCtx = reqCtx.PageContext.HttpContext; if (httpCtx.Request != null && httpCtx.Request.Query != null) { foreach (var key in httpCtx.Request.Query.Keys) { queryRecord[key] = ((string)httpCtx.Request.Query[key]) ?? string.Empty; } } } Properties.Add("RequestQuery", new MPW(MPT.EntityRecord, queryRecord)); } else { Properties.Add("CurrentUser", new MPW(MPT.Object, null)); Properties.Add("RecordId", new MPW(MPT.Object, null)); Properties.Add("ParentRecordId", new MPW(MPT.Object, null)); Properties.Add("Record", new MPW(MPT.EntityRecord, null)); Properties.Add("ParentRecord", new MPW(MPT.EntityRecord, null)); Properties.Add("ReturnUrl", new MPW(MPT.Object, null)); } Properties.Add("RowRecord", new MPW(MPT.EntityRecord, null)); }