private RecordTreeResponse ReadRecordTree(Entity entity, RecordTree recordTree) { RecordTreeResponse response = new RecordTreeResponse { Success = true, Message = "The list was successfully returned!", }; try { response.Object = recordTree; } catch (Exception e) { response.Timestamp = DateTime.UtcNow; response.Success = false; #if DEBUG response.Message = e.Message + e.StackTrace; #else response.Message = "An internal error occurred!"; #endif return response; } response.Timestamp = DateTime.Now; return response; }
private List<ResponseTreeNode> GetTreeRecords(List<Entity> entities, List<EntityRelation> relationList, RecordTree tree) { EntityRelation relation = relationList.FirstOrDefault(r => r.Id == tree.RelationId); Guid treeEntityId = relation.OriginEntityId; Guid treeRelFieldId = relation.OriginFieldId; Entity treeEntity = entities.FirstOrDefault(e => e.Id == treeEntityId); Field treeIdField = treeEntity.Fields.FirstOrDefault(f => f.Id == treeRelFieldId); Field treeParrentField = treeEntity.Fields.FirstOrDefault(f => f.Id == tree.NodeParentIdFieldId); Field nameField = treeEntity.Fields.FirstOrDefault(f => f.Id == tree.NodeNameFieldId); Field labelField = treeEntity.Fields.FirstOrDefault(f => f.Id == tree.NodeLabelFieldId); Field weightField = treeEntity.Fields.FirstOrDefault(f => f.Id == tree.NodeWeightFieldId); var relIdField = treeEntity.Fields.Single(x => x.Name == "id"); List<Guid> fieldIdsToInclude = new List<Guid>(); if (!fieldIdsToInclude.Contains(treeIdField.Id)) fieldIdsToInclude.Add(treeIdField.Id); if (!fieldIdsToInclude.Contains(treeParrentField.Id)) fieldIdsToInclude.Add(treeParrentField.Id); if (!fieldIdsToInclude.Contains(tree.NodeNameFieldId)) fieldIdsToInclude.Add(tree.NodeNameFieldId); if (!fieldIdsToInclude.Contains(tree.NodeLabelFieldId)) fieldIdsToInclude.Add(tree.NodeLabelFieldId); var weightFieldNonNullable = Guid.Empty; if(tree.NodeWeightFieldId.HasValue) { weightFieldNonNullable = tree.NodeWeightFieldId.Value; } if (weightField != null && !fieldIdsToInclude.Contains(weightFieldNonNullable)) fieldIdsToInclude.Add(weightFieldNonNullable); string queryFields = string.Empty; foreach (var fieldId in fieldIdsToInclude) { var f = treeEntity.Fields.SingleOrDefault(x => x.Id == fieldId); if (f != null) { if (!queryFields.Contains(f.Name)) queryFields += (f.Name + ","); } } queryFields += "id"; EntityQuery eq = new EntityQuery(treeEntity.Name, queryFields); RecordManager recMan = new RecordManager(service); var allRecords = recMan.Find(eq).Object.Data; List<ResponseTreeNode> rootNodes = new List<ResponseTreeNode>(); foreach (var rootNode in tree.RootNodes.OrderBy(x=>x.Name)) { List<ResponseTreeNode> children = new List<ResponseTreeNode>(); int? rootNodeWeight = null; if(weightField != null) { rootNodeWeight = rootNode.Weight; children = GetTreeNodeChildren(allRecords, treeIdField.Name, treeParrentField.Name, nameField.Name, labelField.Name, rootNode.Id, weightField.Name, 1, tree.DepthLimit); } else { children = GetTreeNodeChildren(allRecords, treeIdField.Name, treeParrentField.Name, nameField.Name, labelField.Name, rootNode.Id, "no-weight", 1, tree.DepthLimit); } rootNodes.Add(new ResponseTreeNode { RecordId = rootNode.RecordId, Id = rootNode.Id.Value, ParentId = rootNode.ParentId, Name = rootNode.Name, Label = rootNode.Label, Weight = rootNodeWeight, Nodes = children }); } return rootNodes; }