public async Task <ActionResult <Application> > Post(string appId, [FromBody] Application application) { if (!IsValidAppId(appId)) { return(BadRequest("AppId is not valid.")); } string org = appId.Split("/")[0]; var existingApp = await repository.FindOne(appId, org); if (existingApp != null) { return(BadRequest("Application already exists in repository! Try update application instead. ")); } DateTime creationTime = DateTime.UtcNow; // make sure minimum application values are set application.Id = appId; application.Org = org; application.CreatedBy = GetUserId(); application.Created = creationTime; application.LastChangedBy = GetUserId(); application.LastChanged = creationTime; if (application.ValidFrom == null) { application.ValidFrom = creationTime; } if (application.DataTypes == null || application.DataTypes.Count == 0) { application.DataTypes = new List <DataType>(); DataType form = new DataType() { Id = "default", AllowedContentTypes = new List <string>(), }; form.AllowedContentTypes.Add("text/xml"); form.AllowedContentTypes.Add("application/xml"); application.DataTypes.Add(form); } Application result = await repository.Create(application); logger.LogInformation($"Application {appId} sucessfully stored", result); return(Created(appId, result)); }
public async Task <ActionResult> Post(string appId, [FromBody] Application application) { if (!IsValidAppId(appId)) { return(BadRequest("AppId is not valid.")); } string org = appId.Split("/")[0]; try { await repository.FindOne(appId, org); return(BadRequest("Application already exists in repository! Try update application instead. ")); } catch (DocumentClientException e) { // repository throws exception if not found if (e.StatusCode != HttpStatusCode.NotFound) { return(StatusCode(500, $"Unable to access application collection: {e}")); } } catch (Exception e) { logger.LogError($"Unable to perform request: {e}"); return(StatusCode(500, $"Unable to perform request: {e}")); } DateTime creationTime = DateTime.UtcNow; // make sure minimum application values are set application.Id = appId; application.Org = org; application.CreatedBy = GetUserId(); application.Created = creationTime; application.LastChangedBy = GetUserId(); application.LastChanged = creationTime; if (application.ValidFrom == null) { application.ValidFrom = creationTime; } if (application.DataTypes == null || application.DataTypes.Count == 0) { application.DataTypes = new List <DataType>(); DataType form = new DataType() { Id = "default", AllowedContentTypes = new List <string>(), }; form.AllowedContentTypes.Add("text/xml"); form.AllowedContentTypes.Add("application/xml"); application.DataTypes.Add(form); } try { Application result = await repository.Create(application); logger.LogInformation($"Application {appId} sucessfully stored", result); return(Created(appId, result)); } catch (Exception e) { logger.LogError($"Unable to store application data in database. {e}"); return(StatusCode(500, $"Unable to store application data in database. {e}")); } }
public async Task <List <ValidationIssue> > ValidateDataElement(Instance instance, DataType dataType, DataElement dataElement) { _logger.LogInformation($"Validation of data element {dataElement.Id} of instance {instance.Id}"); // Todo. Figure out where to get this from Dictionary <string, Dictionary <string, string> > serviceText = new Dictionary <string, Dictionary <string, string> >(); List <ValidationIssue> messages = new List <ValidationIssue>(); if (dataElement.ContentType == null) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, Code = ValidationIssueCodes.DataElementCodes.MissingContentType, DataElementId = dataElement.Id, Severity = ValidationIssueSeverity.Error, Description = AppTextHelper.GetAppText( ValidationIssueCodes.DataElementCodes.MissingContentType, serviceText, null, "nb") }; messages.Add(message); } else { string contentTypeWithoutEncoding = dataElement.ContentType.Split(";")[0]; if (dataType.AllowedContentTypes != null && dataType.AllowedContentTypes.Count > 0 && dataType.AllowedContentTypes.All(ct => !ct.Equals(contentTypeWithoutEncoding, StringComparison.OrdinalIgnoreCase))) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, DataElementId = dataElement.Id, Code = ValidationIssueCodes.DataElementCodes.ContentTypeNotAllowed, Severity = ValidationIssueSeverity.Error, Description = AppTextHelper.GetAppText( ValidationIssueCodes.DataElementCodes.ContentTypeNotAllowed, serviceText, null, "nb"), Field = dataType.Id }; messages.Add(message); } } if (dataType.MaxSize.HasValue && dataType.MaxSize > 0 && (long)dataType.MaxSize * 1024 * 1024 < dataElement.Size) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, DataElementId = dataElement.Id, Code = ValidationIssueCodes.DataElementCodes.DataElementTooLarge, Severity = ValidationIssueSeverity.Error, Description = AppTextHelper.GetAppText( ValidationIssueCodes.DataElementCodes.DataElementTooLarge, serviceText, null, "nb"), Field = dataType.Id }; messages.Add(message); } if (dataType.AppLogic != null) { Type modelType = _altinnApp.GetAppModelType(dataType.AppLogic.ClassRef); Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); string app = instance.AppId.Split("/")[1]; int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId); dynamic data = await _dataService.GetFormData(instanceGuid, modelType, instance.Org, app, instanceOwnerPartyId, Guid.Parse(dataElement.Id)); ModelStateDictionary validationResults = new ModelStateDictionary(); var actionContext = new ActionContext( _httpContextAccessor.HttpContext, new Microsoft.AspNetCore.Routing.RouteData(), new ActionDescriptor(), validationResults); ValidationStateDictionary validationState = new ValidationStateDictionary(); _objectModelValidator.Validate(actionContext, validationState, null, data); await _altinnApp.RunDataValidation(data, validationResults); if (!validationResults.IsValid) { messages.AddRange(MapModelStateToIssueList(actionContext.ModelState, instance, dataElement.Id, serviceText)); } } return(messages); }
public async Task <List <ValidationIssue> > ValidateDataElement(Instance instance, DataType dataType, DataElement dataElement) { _logger.LogInformation($"Validation of data element {dataElement.Id} of instance {instance.Id}"); // Todo. Figure out where to get this from Dictionary <string, Dictionary <string, string> > serviceText = new Dictionary <string, Dictionary <string, string> >(); List <ValidationIssue> messages = new List <ValidationIssue>(); if (dataElement.ContentType == null) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, Code = ValidationIssueCodes.DataElementCodes.MissingContentType, DataElementId = dataElement.Id, Severity = ValidationIssueSeverity.Error, Description = ServiceTextHelper.GetServiceText( ValidationIssueCodes.DataElementCodes.MissingContentType, serviceText, null, "nb") }; messages.Add(message); } else { string contentTypeWithoutEncoding = dataElement.ContentType.Split(";")[0]; if (dataType.AllowedContentTypes.All(ct => !ct.Equals(contentTypeWithoutEncoding, StringComparison.OrdinalIgnoreCase))) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, DataElementId = dataElement.Id, Code = ValidationIssueCodes.DataElementCodes.ContentTypeNotAllowed, Severity = ValidationIssueSeverity.Error, Description = ServiceTextHelper.GetServiceText( ValidationIssueCodes.DataElementCodes.ContentTypeNotAllowed, serviceText, null, "nb") }; messages.Add(message); } } if (dataType.MaxSize.HasValue && dataType.MaxSize > 0 && (long)dataType.MaxSize * 1024 * 1024 < dataElement.Size) { ValidationIssue message = new ValidationIssue { InstanceId = instance.Id, DataElementId = dataElement.Id, Code = ValidationIssueCodes.DataElementCodes.DataElementTooLarge, Severity = ValidationIssueSeverity.Error, Description = ServiceTextHelper.GetServiceText( ValidationIssueCodes.DataElementCodes.DataElementTooLarge, serviceText, null, "nb") }; messages.Add(message); } if (dataType.AppLogic != null) { Type modelType = _altinnApp.GetAppModelType(dataType.AppLogic.ClassRef); Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); string app = instance.AppId.Split("/")[1]; int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId); dynamic data = _dataService.GetFormData(instanceGuid, modelType, instance.Org, app, instanceOwnerPartyId, Guid.Parse(dataElement.Id)); var context = new ValidationContext(data); List <System.ComponentModel.DataAnnotations.ValidationResult> validationResults = new List <System.ComponentModel.DataAnnotations.ValidationResult>(); bool isValid = await _altinnApp.RunValidation(data.Result, modelType, validationResults); isValid = Validator.TryValidateObject(data, context, validationResults, true) && isValid; if (!isValid) { messages.AddRange(MapModelStateToIssueList(instance, validationResults, dataElement.Id, dataElement.DataType, serviceText)); } } return(messages); }