private async Task StorePrefillParts(Instance instance, Application appInfo, List <RequestPart> parts) { Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); int instanceOwnerIdAsInt = int.Parse(instance.InstanceOwner.PartyId); string org = instance.Org; string app = instance.AppId.Split("/")[1]; foreach (RequestPart part in parts) { DataType dataType = appInfo.DataTypes.Find(d => d.Id == part.Name); DataElement dataElement; if (dataType.AppLogic != null) { _logger.LogInformation($"Storing part {part.Name}"); Type type; try { type = _altinnApp.GetAppModelType(dataType.AppLogic.ClassRef); } catch (Exception altinnAppException) { throw new ServiceException(HttpStatusCode.InternalServerError, $"App.GetAppModelType failed: {altinnAppException.Message}", altinnAppException); } ModelDeserializer deserializer = new ModelDeserializer(_logger, type); object data = await deserializer.DeserializeAsync(part.Stream, part.ContentType); if (!string.IsNullOrEmpty(deserializer.Error)) { throw new InvalidOperationException(deserializer.Error); } await _prefillService.PrefillDataModel(instance.InstanceOwner.PartyId, part.Name, data); await _altinnApp.RunDataCreation(instance, data); dataElement = await _dataService.InsertFormData( data, instanceGuid, type, org, app, instanceOwnerIdAsInt, part.Name); } else { dataElement = await _dataService.InsertBinaryData(instance.Id, part.Name, part.ContentType, part.FileName, part.Stream); } if (dataElement == null) { throw new ServiceException(HttpStatusCode.InternalServerError, $"Data service did not return a valid instance metadata when attempt to store data element {part.Name}"); } } }
private async Task <ActionResult> CreateAppModelData( string org, string app, Instance instance, string dataType) { Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); object appModel; string classRef = _appResourcesService.GetClassRefForLogicDataType(dataType); if (Request.ContentType == null) { appModel = _altinnApp.CreateNewAppModel(classRef); } else { ModelDeserializer deserializer = new ModelDeserializer(_logger, _altinnApp.GetAppModelType(classRef)); appModel = await deserializer.DeserializeAsync(Request.Body, Request.ContentType); if (!string.IsNullOrEmpty(deserializer.Error)) { return(BadRequest(deserializer.Error)); } } // runs prefill from repo configuration if config exists await _prefillService.PrefillDataModel(instance.InstanceOwner.PartyId, dataType, appModel); // send events to trigger application business logic await _altinnApp.RunDataCreation(instance, appModel); Dictionary <string, string> updatedValues = DataHelper.GetUpdatedDataValues( _appResourcesService.GetApplication().PresentationFields, instance.PresentationTexts, dataType, appModel); if (updatedValues.Count > 0) { await _instanceService.UpdatePresentationTexts( int.Parse(instance.InstanceOwner.PartyId), Guid.Parse(instance.Id.Split("/")[1]), new PresentationTexts { Texts = updatedValues }); } int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId); DataElement dataElement = await _dataService.InsertFormData(appModel, instanceGuid, _altinnApp.GetAppModelType(classRef), org, app, instanceOwnerPartyId, dataType); SelfLinkHelper.SetDataAppSelfLinks(instanceOwnerPartyId, instanceGuid, dataElement, Request); return(Created(dataElement.SelfLinks.Apps, dataElement)); }
private async Task <ActionResult> CreateAppModelData( string org, string app, Instance instance, string dataType) { Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); object appModel; string classRef = _appResourcesService.GetClassRefForLogicDataType(dataType); if (Request.ContentType == null) { appModel = _altinnApp.CreateNewAppModel(classRef); } else { appModel = ParseContentAndDeserializeServiceModel(_altinnApp.GetAppModelType(classRef), out ActionResult contentError); if (contentError != null) { return(contentError); } } // send events to trigger application business logic await _altinnApp.RunDataCreation(instance, appModel); int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId); DataElement dataElement = await _dataService.InsertFormData(appModel, instanceGuid, _altinnApp.GetAppModelType(classRef), org, app, instanceOwnerPartyId, dataType); SelfLinkHelper.SetDataAppSelfLinks(instanceOwnerPartyId, instanceGuid, dataElement, Request); return(Created(dataElement.SelfLinks.Apps, dataElement)); }