public IActionResult UpdateRecordTreeByName(string entityName, string treeName, [FromBody]JObject submitObj) { RecordListResponse response = new RecordListResponse(); InputRecordTree tree = new InputRecordTree(); Type inputViewType = tree.GetType(); foreach (var prop in submitObj.Properties()) { int count = inputViewType.GetProperties().Where(n => n.Name.ToLower() == prop.Name.ToLower()).Count(); if (count < 1) response.Errors.Add(new ErrorModel(prop.Name, prop.Value.ToString(), "Input object contains property that is not part of the object model.")); } if (response.Errors.Count > 0) return DoBadRequestResponse(response); try { tree = InputRecordTree.Convert(submitObj); } catch (Exception e) { return DoBadRequestResponse(response, "Input object is not in valid format! It cannot be converted.", e); } return DoResponse(entMan.UpdateRecordTree(entityName, tree)); }
public IActionResult PatchRecordTreeByName(string entityName, string treeName, [FromBody]JObject submitObj) { RecordTreeResponse response = new RecordTreeResponse(); Entity entity = new Entity(); InputRecordTree tree = new InputRecordTree(); try { DbEntity storageEntity = DbContext.Current.EntityRepository.Read(entityName); if (storageEntity == null) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "Entity with such Name does not exist!"; return DoBadRequestResponse(response); } entity = storageEntity.MapTo<Entity>(); RecordTree treeToUpdate = entity.RecordTrees.FirstOrDefault(l => l.Name == treeName); if (treeToUpdate == null) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "REcord tree with such Name does not exist!"; return DoBadRequestResponse(response); } tree = treeToUpdate.MapTo<InputRecordTree>(); Type inputListType = tree.GetType(); foreach (var prop in submitObj.Properties()) { int count = inputListType.GetProperties().Where(n => n.Name.ToLower() == prop.Name.ToLower()).Count(); if (count < 1) response.Errors.Add(new ErrorModel(prop.Name, prop.Value.ToString(), "Input object contains property that is not part of the object model.")); } if (response.Errors.Count > 0) return DoBadRequestResponse(response); InputRecordTree inputTree = InputRecordTree.Convert(submitObj); foreach (var prop in submitObj.Properties()) { if (prop.Name.ToLower() == "label") tree.Label = inputTree.Label; if (prop.Name.ToLower() == "default") tree.Default = inputTree.Default; if (prop.Name.ToLower() == "system") tree.System = inputTree.System; if (prop.Name.ToLower() == "depthlimit") tree.DepthLimit = inputTree.DepthLimit; if (prop.Name.ToLower() == "cssclass") tree.CssClass = inputTree.CssClass; if (prop.Name.ToLower() == "iconname") tree.IconName = inputTree.IconName; if (prop.Name.ToLower() == "nodenamefieldid") tree.NodeNameFieldId = inputTree.NodeNameFieldId; if (prop.Name.ToLower() == "nodelabelfieldid") tree.NodeLabelFieldId = inputTree.NodeLabelFieldId; if (prop.Name.ToLower() == "nodeweightfieldid") tree.NodeWeightFieldId = inputTree.NodeWeightFieldId; if (prop.Name.ToLower() == "rootnodes") tree.RootNodes = inputTree.RootNodes; if (prop.Name.ToLower() == "nodeobjectproperties") tree.NodeObjectProperties = inputTree.NodeObjectProperties; } } catch (Exception e) { return DoBadRequestResponse(response, "Input object is not in valid format! It cannot be converted.", e); } var updateResponse = entMan.UpdateRecordTree(entity, tree); return DoResponse(updateResponse); }
public IActionResult CreateRecordTreeByName(string entityName, [FromBody]JObject submitObj) { RecordListResponse response = new RecordListResponse(); InputRecordTree tree = new InputRecordTree(); try { tree = InputRecordTree.Convert(submitObj); } catch (Exception e) { return DoBadRequestResponse(response, "Input object is not in valid format! It cannot be converted.", e); } return DoResponse(entMan.CreateRecordTree(entityName, tree)); }
public RecordTreeResponse CreateRecordTree(string entityName, InputRecordTree inputRecordTree) { RecordTreeResponse response = new RecordTreeResponse(); var entityResponse = ReadEntity(entityName); if (!entityResponse.Success) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = entityResponse.Message; return response; } else if (entityResponse.Object == null) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "Entity with such Id does not exist!"; return response; } Entity entity = entityResponse.Object; return CreateRecordTree(entity, inputRecordTree); }
private List<ErrorModel> ValidateRecordTree(Entity entity, InputRecordTree recordTree) { List<ErrorModel> errorList = new List<ErrorModel>(); if (!recordTree.Id.HasValue || recordTree.Id == Guid.Empty) errorList.Add(new ErrorModel("id", null, "Id is required!")); if (string.IsNullOrWhiteSpace(recordTree.Name)) errorList.Add(new ErrorModel("name", null, "Name is required!")); if (string.IsNullOrWhiteSpace(recordTree.Name)) errorList.Add(new ErrorModel("label", null, "Label is required!")); if (errorList.Any()) return errorList; if (entity.RecordTrees.Any(f => f.Id != recordTree.Id && f.Name.ToLowerInvariant() == recordTree.Name.ToLowerInvariant())) errorList.Add(new ErrorModel("name", null, "There is already a tree with same name!")); if (entity.RecordTrees.Any(f => f.Id != recordTree.Id && f.Label.ToLowerInvariant() == recordTree.Label.ToLowerInvariant())) errorList.Add(new ErrorModel("label", null, "There is already a tree with same label!")); DbEntityRelation relation = null; if (recordTree.RelationId == null || recordTree.RelationId == Guid.Empty) { errorList.Add(new ErrorModel("relationId", null, "Relation is required!")); return errorList; } else { relation = DbContext.Current.RelationRepository.Read(recordTree.RelationId); } if (relation == null) { errorList.Add(new ErrorModel("relationId", null, "Cannot find relation for specified id!")); return errorList; } if (!(relation.OriginEntityId == entity.Id && relation.TargetEntityId == relation.OriginEntityId) || relation.RelationType == EntityRelationType.OneToOne) { errorList.Add(new ErrorModel("relationId", null, "Specified relation cannot be used as tree relation!")); return errorList; } if (recordTree.NodeIdFieldId == null) recordTree.NodeIdFieldId = relation.OriginFieldId; else if (recordTree.NodeIdFieldId != relation.OriginFieldId) errorList.Add(new ErrorModel("nodeIdFieldId", null, "Node field does not correspond to specified relation target field!")); if (recordTree.NodeParentIdFieldId == null) recordTree.NodeParentIdFieldId = relation.TargetFieldId; else if (recordTree.NodeParentIdFieldId != relation.TargetFieldId) errorList.Add(new ErrorModel("nodeParentIdFieldId", null, "Node parent field does not correspond to specified relation origin field!")); var idField = entity.Fields.Single(f => f.Name == "id"); if (recordTree.NodeNameFieldId == null) recordTree.NodeNameFieldId = idField.Id; if (recordTree.NodeLabelFieldId == null) recordTree.NodeLabelFieldId = idField.Id; if (!entity.Fields.Any(f => f.Id == recordTree.NodeIdFieldId)) errorList.Add(new ErrorModel("nodeIdFieldId", null, "Node field is not found in entity fields collection!")); if (!entity.Fields.Any(f => f.Id == recordTree.NodeParentIdFieldId)) errorList.Add(new ErrorModel("nodeParentIdFieldId", null, "Node parent field is not found in entity fields collection!")); if (!entity.Fields.Any(f => f.Id == recordTree.NodeNameFieldId)) errorList.Add(new ErrorModel("nodeNameFieldId", null, "Node name field is not found in entity fields collection!")); if (!entity.Fields.Any(f => f.Id == recordTree.NodeLabelFieldId)) errorList.Add(new ErrorModel("nodeLabelFieldId", null, "Node label field is not found in entity fields collection!")); if (recordTree.DepthLimit == null) errorList.Add(new ErrorModel("depthLimit", null, "Depth limit is required!")); else if (recordTree.DepthLimit <= 0) errorList.Add(new ErrorModel("depthLimit", null, "Depth limit should be a positive number!")); else if (recordTree.DepthLimit > 50) errorList.Add(new ErrorModel("depthLimit", null, "Depth limit cannot be more than 20!")); if (errorList.Any()) return errorList; if (recordTree.RootNodes != null) { List<RecordTreeNode> expiredNodes = new List<RecordTreeNode>(); foreach (var node in recordTree.RootNodes) { var recData = DbContext.Current.RecordRepository.Find(entity.Name, node.RecordId); if (recData != null) { var parentIdField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeParentIdFieldId); var nameField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeNameFieldId); var labelField = entity.Fields.SingleOrDefault(x => x.Id == recordTree.NodeLabelFieldId); var value = recData[idField.Name]; node.Id = (value as Guid?) ?? Guid.Empty; value = recData[parentIdField.Name]; node.ParentId = (value as Guid?) ?? Guid.Empty; value = recData[nameField.Name]; node.Name = (value ?? string.Empty).ToString(); value = recData[labelField.Name]; node.Label = (value ?? string.Empty).ToString(); } else expiredNodes.Add(node); } foreach (var node in expiredNodes) errorList.Add(new ErrorModel("rootNodes", null, $"Root node Id '{node.Id}' not found")); } else recordTree.RootNodes = new List<RecordTreeNode>(); //verify and init node properties bellow if (recordTree.NodeObjectProperties == null) recordTree.NodeObjectProperties = new List<Guid>(); List<Guid> expiredFields = new List<Guid>(); foreach (var fieldId in recordTree.NodeObjectProperties) { if (!entity.Fields.Any(f => f.Id == fieldId)) expiredFields.Add(fieldId); } foreach (var expiredId in expiredFields) recordTree.NodeObjectProperties.Remove(expiredId); if (!recordTree.NodeObjectProperties.Contains(idField.Id)) recordTree.NodeObjectProperties.Add(idField.Id); if (!recordTree.NodeObjectProperties.Contains(recordTree.NodeIdFieldId.Value)) recordTree.NodeObjectProperties.Add(recordTree.NodeIdFieldId.Value); if (!recordTree.NodeObjectProperties.Contains(recordTree.NodeIdFieldId.Value)) recordTree.NodeObjectProperties.Add(recordTree.NodeIdFieldId.Value); if (!recordTree.NodeObjectProperties.Contains(recordTree.NodeParentIdFieldId.Value)) recordTree.NodeObjectProperties.Add(recordTree.NodeParentIdFieldId.Value); if (!recordTree.NodeObjectProperties.Contains(recordTree.NodeNameFieldId.Value)) recordTree.NodeObjectProperties.Add(recordTree.NodeNameFieldId.Value); if (!recordTree.NodeObjectProperties.Contains(recordTree.NodeLabelFieldId.Value)) recordTree.NodeObjectProperties.Add(recordTree.NodeLabelFieldId.Value); return errorList; }
private RecordTreeResponse CreateRecordTree(Entity entity, InputRecordTree inputRecordTree) { RecordTreeResponse response = new RecordTreeResponse { Success = true, Message = "The tree was successfully created!", }; if (!inputRecordTree.Id.HasValue) inputRecordTree.Id = Guid.NewGuid(); RecordTree recordTree = inputRecordTree.MapTo<RecordTree>(); try { response.Object = recordTree; response.Errors = ValidateRecordTree(entity, inputRecordTree); recordTree = inputRecordTree.MapTo<RecordTree>(); if (response.Errors.Count > 0) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "The tree was not created. Validation error occurred!"; return response; } entity.RecordTrees.Add(recordTree); DbEntity updatedEntity = entity.MapTo<DbEntity>(); bool result = DbContext.Current.EntityRepository.Update(updatedEntity); if (!result) { Cache.ClearEntities(); response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "The tree was not created! An internal error occurred!"; return response; } } catch (Exception e) { Cache.ClearEntities(); response.Success = false; response.Object = recordTree; response.Timestamp = DateTime.UtcNow; #if DEBUG response.Message = e.Message + e.StackTrace; #else response.Message = "The tree was not created. An internal error occurred!"; #endif return response; } Cache.ClearEntities(); return ReadRecordTree(entity.Id, recordTree.Id); }
public RecordTreeResponse CreateRecordTree(string entityName, InputRecordTree inputRecordTree) { RecordTreeResponse response = new RecordTreeResponse(); IStorageEntity storageEntity = EntityRepository.Read(entityName); if (storageEntity == null) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "Entity with such name does not exist!"; return response; } return CreateRecordTree(storageEntity.MapTo<Entity>(), inputRecordTree); }
public RecordTreeResponse UpdateRecordTree(Entity entity, InputRecordTree inputRecordTree) { RecordTreeResponse response = new RecordTreeResponse { Success = true, Message = "The tree was successfully updated!", }; RecordTree recordTree = inputRecordTree.MapTo<RecordTree>(); try { response.Object = recordTree; response.Errors = ValidateRecordTree(entity, inputRecordTree); recordTree = inputRecordTree.MapTo<RecordTree>(); if (response.Errors.Count > 0) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "The tree was not updated. Validation error occurred!"; return response; } RecordTree treeToUpdate = entity.RecordTrees.FirstOrDefault(r => r.Id == recordTree.Id); if (treeToUpdate.Id == recordTree.Id) entity.RecordTrees.Remove(treeToUpdate); entity.RecordTrees.Add(recordTree); IStorageEntity updatedEntity = entity.MapTo<IStorageEntity>(); bool result = EntityRepository.Update(updatedEntity); if (!result) { response.Timestamp = DateTime.UtcNow; response.Success = false; response.Message = "The tree was not updated! An internal error occurred!"; return response; } } catch (Exception e) { response.Success = false; response.Object = recordTree; response.Timestamp = DateTime.UtcNow; #if DEBUG response.Message = e.Message + e.StackTrace; #else response.Message = "The tree was not updated. An internal error occurred!"; #endif return response; } return ReadRecordTree(entity.Id, recordTree.Id); }