public async Task <IActionResult> CompleteAndSendIn(string org, string service, int instanceId, string view)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = PopulateRequestContext(instanceId);

            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service);

            serviceImplementation.SetPlatformServices(platformServices);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            PopulateViewBag(org, service, instanceId, 0, requestContext, serviceContext, platformServices);

            // Getting the Form Data from database
            object serviceModel = _form.GetFormModel(instanceId, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.ReporteeId, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            serviceImplementation.SetServiceModel(serviceModel);

            ViewBag.FormID         = instanceId;
            ViewBag.ServiceContext = serviceContext;

            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);
            await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);

            ApiResult apiResult = new ApiResult();

            if (ModelState.IsValid)
            {
                ServiceState currentState = _workflowSI.MoveServiceForwardInWorkflow(instanceId, org, service, requestContext.UserContext.ReporteeId);
                if (currentState.State == WorkflowStep.Archived)
                {
                    _archive.ArchiveServiceModel(serviceModel, instanceId, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.ReporteeId);
                    apiResult.NextState = currentState.State;
                }
            }

            ModelHelper.MapModelStateToApiResult(ModelState, apiResult, serviceContext);

            if (apiResult.Status.Equals(ApiStatusType.ContainsError))
            {
                Response.StatusCode = 202;
            }
            else
            {
                Response.StatusCode = 200;
            }

            return(new ObjectResult(apiResult));
        }
Esempio n. 2
0
        private async Task <ActionResult> PutFormData(string org, string app, Instance instance, Guid dataGuid, string elementType)
        {
            Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]);
            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType);

            object serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError);

            if (contentError != null)
            {
                return(contentError);
            }

            if (serviceModel == null)
            {
                return(BadRequest("No data found in content"));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

            try
            {
                // Run the model Validation that handles validation defined on the model
                TryValidateModel(serviceModel);

                // send events to trigger application business logic
                await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);
            }
            catch (Exception ex)
            {
                logger.LogError($"Validation errors are currently ignored: {ex.Message}");
            }

            // Save Formdata to database
            Instance instanceAfter = await this.dataService.UpdateData(
                serviceModel,
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                int.Parse(instance.InstanceOwnerId),
                dataGuid);

            InstancesController.SetAppSelfLinks(instanceAfter, Request);
            DataElement updatedElement = instanceAfter.Data.First(d => d.Id == dataGuid.ToString());
            string      dataUrl        = updatedElement.DataLinks.Apps;

            return(Created(dataUrl, updatedElement));
        }
Esempio n. 3
0
        /// <summary>
        /// Validate the model.
        /// </summary>
        /// <param name="org">the organisation.</param>
        /// <param name="service">the service.</param>
        /// <param name="instanceId">the instance id.</param>
        /// <returns>The api response.</returns>
        public async Task <IActionResult> ModelValidation(string org, string service, Guid instanceId)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Reportee = requestContext.UserContext.Reportee;
            requestContext.Form     = Request.Form;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Assign the Requestcontext and ViewBag to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            serviceImplementation.SetPlatformServices(_platformSI);

            // Getting the populated form data from disk
            dynamic serviceModel = _form.GetFormModel(
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                service,
                requestContext.UserContext.ReporteeId,
                AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            serviceImplementation.SetServiceModel(serviceModel);

            // Do Model Binding and update form data
            await TryUpdateModelAsync(serviceModel);

            // ServiceEvent : HandleValidationEvent
            // Perform Validation defined by the service developer.
            await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);

            ApiResult apiResult = new ApiResult();

            ModelHelper.MapModelStateToApiResult(ModelState, apiResult, serviceContext);

            if (apiResult.Status.Equals(ApiStatusType.ContainsError))
            {
                Response.StatusCode = 202;
            }
            else
            {
                Response.StatusCode = 200;
            }

            return(new ObjectResult(apiResult));
        }
Esempio n. 4
0
        /// <summary>
        /// Action method to present.
        /// </summary>
        /// <param name="org">The Organization code for the service owner.</param>
        /// <param name="service">The service code for the current service.</param>
        /// <param name="partyId">The partyId.</param>
        /// <param name="instanceGuid">The instanceGuid.</param>
        /// <returns>The receipt view.</returns>
        public async Task <IActionResult> Receipt(string org, string service, int partyId, Guid instanceGuid)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceGuid);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Party = requestContext.UserContext.Party;

            serviceImplementation.SetPlatformServices(_platformSI);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            PopulateViewBag(org, service, instanceGuid, 0, requestContext, serviceContext, _platformSI);

            object serviceModel = _archive.GetArchivedServiceModel(instanceGuid, serviceImplementation.GetServiceModelType(), org, service, requestContext.Party.PartyId);
            List <ServiceInstance> formInstances = _testdata.GetFormInstances(requestContext.Party.PartyId, org, service);
            string properInstanceId = $"{requestContext.Party}/{instanceGuid}";

            ViewBag.ServiceInstance = formInstances.Find(i => i.ServiceInstanceID == properInstanceId);

            return(View());
        }
Esempio n. 5
0
        /// <summary>
        ///  Gets a data element (form data) from storage and performs business logic on it (e.g. to calculate certain fields) before it is returned.
        ///  If more there are more data elements of the same elementType only the first one is returned. In that case use the more spesific
        ///  GET method to fetch a particular data element.
        /// </summary>
        /// <returns>data element is returned in response body</returns>
        private async Task <ActionResult> GetFormData(
            string org,
            string app,
            int instanceOwnerId,
            Guid instanceGuid,
            Guid dataGuid,
            string elementType)
        {
            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType);

            // Get Form Data from data service. Assumes that the data element is form data.
            object serviceModel = dataService.GetFormData(
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                instanceOwnerId,
                dataGuid);

            if (serviceModel == null)
            {
                return(BadRequest($"Did not find form data for data element {dataGuid}"));
            }

            // Assing the populated service model to the service implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

            return(Ok(serviceModel));
        }
Esempio n. 6
0
        private async Task <Instance> StorePrefillParts(Instance instance, List <RequestPart> parts)
        {
            Guid     instanceGuid         = Guid.Parse(instance.Id.Split("/")[1]);
            int      instanceOwnerIdAsInt = int.Parse(instance.InstanceOwnerId);
            Instance instanceWithData     = null;
            string   org = instance.Org;
            string   app = instance.AppId.Split("/")[1];

            foreach (RequestPart part in parts)
            {
                logger.LogInformation($"Storing part {part.Name}");
                object data = new StreamReader(part.Stream).ReadToEnd();

                IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, part.Name, true);

                instanceWithData = await dataService.InsertFormData(
                    data,
                    instanceGuid,
                    serviceImplementation.GetServiceModelType(),
                    org,
                    app,
                    instanceOwnerIdAsInt);

                if (instanceWithData == null)
                {
                    throw new InvalidOperationException($"Dataservice did not return a valid instance metadata when attempt to store data element {part.Name}");
                }
            }

            return(instanceWithData);
        }
Esempio n. 7
0
        public async Task <IActionResult> CompleteAndSendIn(string org, string service, Guid instanceId)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Reportee = requestContext.UserContext.Reportee;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            serviceImplementation.SetPlatformServices(_platformSI);

            // Identify the correct view
            // Getting the Form Data from database
            object serviceModel = _form.GetFormModel(instanceId, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.ReporteeId, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            serviceImplementation.SetServiceModel(serviceModel);
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            ViewBag.FormID         = instanceId;
            ViewBag.ServiceContext = serviceContext;

            await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);

            return(View());
        }
        /// <summary>
        /// Action method to present.
        /// </summary>
        /// <param name="org">The Organization code for the service owner.</param>
        /// <param name="service">The service code for the current service.</param>
        /// <param name="instanceId">The instanceId.</param>
        /// <returns>The receipt view.</returns>
        public IActionResult Receipt(string org, string service, int instanceId)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId);

            requestContext.UserContext = _userHelper.GetUserContext(HttpContext);
            requestContext.Reportee    = requestContext.UserContext.Reportee;

            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service);

            serviceImplementation.SetPlatformServices(platformServices);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            PopulateViewBag(org, service, instanceId, 0, requestContext, serviceContext, platformServices);

            object serviceModel = _archive.GetArchivedServiceModel(instanceId, serviceImplementation.GetServiceModelType(), org, service, requestContext.Reportee.PartyId);
            List <ServiceInstance> formInstances = _testdata.GetFormInstances(requestContext.Reportee.PartyId, org, service, AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext));

            ViewBag.ServiceInstance = formInstances.Find(i => i.ServiceInstanceID == instanceId);

            return(View());
        }
        public async Task <IActionResult> Index(string org, string service, string edition, int instanceId)
        {
            // Getting the Service Specific Implementation contained in external DLL migrated from TUL
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, edition);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId);

            requestContext.UserContext = _userHelper.GetUserContext(HttpContext);
            requestContext.Reportee    = requestContext.UserContext.Reportee;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, edition);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            ViewBag.ServiceContext = serviceContext;
            ViewBag.RequestContext = requestContext;
            ViewBag.Org            = org;
            ViewBag.Service        = service;
            ViewBag.Edition        = edition;
            ViewBag.FormID         = instanceId;

            // Assign the RequestContext and ViewBag to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service, edition);

            serviceImplementation.SetPlatformServices(platformServices);

            ViewBag.PlatformServices = platformServices;

            // Getting the Form Data from datastore
            object serviceModel = this._form.GetFormModel(
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                service,
                edition,
                requestContext.UserContext.ReporteeId);

            // Assing the populated service model to the service implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // ServiceEvent 1: HandleGetDataEvent
            // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources
            // based on the data in the service model
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval);

            // ServiceEvent 2: HandleCalculationEvent
            // Perform Calculation defined by the service developer
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Calculation);

            return(Ok(serviceModel));
        }
Esempio n. 10
0
        public async Task <ActionResult> CreateDataElement(
            [FromRoute] string org,
            [FromRoute] string app,
            [FromRoute] int instanceOwnerId,
            [FromRoute] Guid instanceGuid,
            [FromQuery] string elementType = "default")
        {
            bool startService = true;

            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType, startService);

            Application application = repositoryService.GetApplication(org, app);

            if (application != null)
            {
                return(NotFound($"AppId {org}/{app} was not found"));
            }

            Instance instanceBefore = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid);

            if (instanceBefore == null)
            {
                return(BadRequest("Unknown instance"));
            }

            object serviceModel = null;

            if (Request.ContentType == null)
            {
                serviceModel = serviceImplementation.CreateNewServiceModel();
            }
            else
            {
                serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError);
                if (contentError != null)
                {
                    return(contentError);
                }
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation);

            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            InstancesController.SetAppSelfLinks(instanceBefore, Request);

            Instance instanceAfter = await dataService.InsertData(serviceModel, instanceGuid, serviceImplementation.GetServiceModelType(), org, app, instanceOwnerId);

            InstancesController.SetAppSelfLinks(instanceAfter, Request);
            List <DataElement> createdElements = CompareAndReturnCreatedElements(instanceBefore, instanceAfter);
            string             dataUrl         = createdElements.First().DataLinks.Apps;

            return(Created(dataUrl, instanceAfter));
        }
Esempio n. 11
0
        public async Task <IActionResult> CompleteAndSendIn(string org, string service, string edition, int instanceId, string view)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, edition);

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, edition);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = PopulateRequestContext(instanceId);

            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service, edition);

            serviceImplementation.SetPlatformServices(platformServices);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            PopulateViewBag(org, service, edition, instanceId, 0, requestContext, serviceContext, platformServices);

            //Getting the Form Data from database
            object serviceModel = _form.GetFormModel(instanceId, serviceImplementation.GetServiceModelType(), org, service, edition, requestContext.UserContext.ReporteeId);

            serviceImplementation.SetServiceModel(serviceModel);

            ViewBag.FormID         = instanceId;
            ViewBag.ServiceContext = serviceContext;

            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);
            await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);

            if (ModelState.IsValid)
            {
                _archive.ArchiveServiceModel(serviceModel, instanceId, serviceImplementation.GetServiceModelType(), org, service, edition, requestContext.UserContext.ReporteeId);

                return(RedirectToAction("Receipt", new { org, service, edition, instanceId }));
            }

            return(View());
        }
Esempio n. 12
0
        /// <inheritdoc/>
        public async Task <Instance> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid   instanceGuid    = Guid.NewGuid();
            string appName         = startServiceModel.Service;
            string org             = startServiceModel.Org;
            int    instanceOwnerId = startServiceModel.PartyId;
            int    userId          = startServiceModel.UserId;

            ServiceState currentTask = _workflow.GetInitialServiceState(org, appName);

            Instance instance = new Instance
            {
                Id = $"{instanceOwnerId}/{instanceGuid}",
                InstanceOwnerId = instanceOwnerId.ToString(),
                AppId           = $"{org}/{appName}",
                Org             = org,
                CreatedBy       = userId.ToString(),
                CreatedDateTime = DateTime.UtcNow,
                Process         = new Storage.Interface.Models.ProcessState()
                {
                    CurrentTask = currentTask.State.ToString(),
                    IsComplete  = false,
                },
                InstanceState = new Storage.Interface.Models.InstanceState()
                {
                    IsArchived            = false,
                    IsDeleted             = false,
                    IsMarkedForHardDelete = false,
                },
                LastChangedDateTime = DateTime.UtcNow,
                LastChangedBy       = userId.ToString(),
            };

            string developer         = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string testDataForParty  = $"{_settings.GetTestdataForPartyPath(org, appName, developer)}{instanceOwnerId}";
            string folderForInstance = Path.Combine(testDataForParty, instanceGuid.ToString());

            Directory.CreateDirectory(folderForInstance);
            string instanceFilePath = $"{testDataForParty}/{instanceGuid}/{instanceGuid}.json";

            File.WriteAllText(instanceFilePath, JsonConvert.SerializeObject(instance).ToString(), Encoding.UTF8);

            // Save instantiated form model
            instance = await _data.InsertData(
                serviceModel,
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                appName,
                instanceOwnerId);

            return(instance);
        }
Esempio n. 13
0
        public async Task <ActionResult> GetDataElement(
            [FromRoute] string org,
            [FromRoute] string app,
            [FromRoute] int instanceOwnerId,
            [FromRoute] Guid instanceGuid,
            [FromRoute] string elementType = "default")
        {
            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType);

            Instance instance = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid);

            if (instance == null)
            {
                return(NotFound("Did not find instance"));
            }

            // Assume that there is only one data element of a given type !!
            DataElement dataElement = instance.Data.Find(m => m.ElementType.Equals(elementType));

            if (dataElement == null)
            {
                return(NotFound("Did not find data element"));
            }

            Guid dataId = Guid.Parse(dataElement.Id);

            // Get Form Data from data service. Assumes that the data element is form data.
            object serviceModel = dataService.GetFormData(
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                instanceOwnerId,
                dataId);

            if (serviceModel == null)
            {
                return(BadRequest($"Did not find form data for data element {dataId}"));
            }

            // Assing the populated service model to the service implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval);

            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Calculation);

            return(Ok(serviceModel));
        }
Esempio n. 14
0
        /// <inheritdoc />
        public async Task <Instance> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid     instanceId;
            Instance instance        = null;
            string   org             = startServiceModel.Org;
            string   appId           = ApplicationHelper.GetFormattedApplicationId(org, startServiceModel.Service);
            string   appName         = startServiceModel.Service;
            int      instanceOwnerId = startServiceModel.PartyId;

            string apiUrl = $"instances/?appId={appId}&instanceOwnerId={instanceOwnerId}";
            string token  = JwtTokenUtil.GetTokenFromContext(_httpContextAccessor.HttpContext, _cookieOptions.Cookie.Name);

            JwtTokenUtil.AddTokenToRequestHeader(_client, token);

            try
            {
                HttpResponseMessage response = await _client.PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json"));

                Instance createdInstance = await response.Content.ReadAsAsync <Instance>();

                instanceId = Guid.Parse(createdInstance.Id.Split("/")[1]);
            }
            catch
            {
                return(instance);
            }

            // Save instantiated form model
            instance = await _data.InsertData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                appName,
                instanceOwnerId);

            ServiceState currentState = _workflow.GetInitialServiceState(org, appName);

            // set initial workflow state
            instance.Process = new Storage.Interface.Models.ProcessState()
            {
                CurrentTask = currentState.State.ToString(),
                IsComplete  = false,
            };

            instance = await UpdateInstance(instance, appName, org, instanceOwnerId, instanceId);

            return(instance);
        }
        public async Task <IActionResult> Lookup([FromBody] AltinnCoreApiModel model, string reportee, string org, string service, string edition)
        {
            ApiResult apiResult = new ApiResult();

            // Load the service implementation for the requested service
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, edition);

            // Get the service context containing metadata about the service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, edition);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, 0);

            requestContext.UserContext = _userHelper.GetUserContext(HttpContext);
            requestContext.Reportee    = requestContext.UserContext.Reportee;

            // Create platform service and assign to service implementation making it possible for the service implementation
            // to use plattform services. Also make it avaiable in ViewBag so it can be used from Views
            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service, edition);

            serviceImplementation.SetPlatformServices(platformServices);
            ViewBag.PlatformServices = platformServices;

            // Create a new instance of the service model (a Get to lookup will always create a new service model)
            dynamic serviceModel = null;

            // Assign the different context information to the service implementation making it possible for
            // the service developer to take use of this information
            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);

            // Do Model Binding and update form data
            serviceModel = ParseApiBody(serviceImplementation.GetServiceModelType(), out apiResult, model);
            if (serviceModel == null)
            {
                Response.StatusCode = 403;
                return(new ObjectResult(apiResult));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // Run the Data Retriavel event where service developer can potensial load any data without any user input
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval);

            return(Ok(serviceModel));
        }
Esempio n. 16
0
        /// <inheritdoc />
        public async Task <Instance> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid     instanceId;
            Instance instance           = null;
            string   applicationOwnerId = startServiceModel.Org;
            string   applicationId      = ApplicationHelper.GetFormattedApplicationId(applicationOwnerId, startServiceModel.Service);
            int      instanceOwnerId    = startServiceModel.ReporteeID;

            using (HttpClient client = new HttpClient())
            {
                string apiUrl = $"{_platformSettings.GetApiStorageEndpoint}instances/?applicationId={applicationId}&instanceOwnerId={instanceOwnerId}";
                client.BaseAddress = new Uri(apiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                try
                {
                    HttpResponseMessage response = await client.PostAsync(apiUrl, null);

                    string id = await response.Content.ReadAsAsync <string>();

                    instanceId = Guid.Parse(id);
                }
                catch
                {
                    return(instance);
                }
            }

            // Save instantiated form model
            instance = await _data.InsertData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                applicationOwnerId,
                applicationId,
                instanceOwnerId);

            ServiceState currentState = _workflow.GetInitialServiceState(applicationOwnerId, applicationId);

            // set initial workflow state
            instance.CurrentWorkflowStep = currentState.State.ToString();

            instance = await UpdateInstance(instance, applicationId, applicationOwnerId, instanceOwnerId, instanceId);

            return(instance);
        }
Esempio n. 17
0
        public async Task <Instance> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid     instanceId;
            Instance instance        = null;
            string   org             = startServiceModel.Org;
            string   app             = startServiceModel.Service;
            int      instanceOwnerId = startServiceModel.PartyId;

            Instance instanceTemplate = new Instance()
            {
                InstanceOwnerId = instanceOwnerId.ToString(),
                Process         = new ProcessState()
                {
                    Started     = DateTime.UtcNow,
                    CurrentTask = new ProcessElementInfo
                    {
                        Started = DateTime.UtcNow,

                        ElementId = _workflow.GetInitialServiceState(org, app).State.ToString(),
                    }
                },
            };

            Instance createdInstance = await CreateInstance(org, app, instanceTemplate);

            if (createdInstance == null)
            {
                return(null);
            }

            instanceId = Guid.Parse(createdInstance.Id.Split("/")[1]);

            // Save instantiated form model
            instance = await _data.InsertFormData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                instanceOwnerId);

            instance = await UpdateInstance(instance, app, org, instanceOwnerId, instanceId);

            return(instance);
        }
Esempio n. 18
0
        private async Task <ActionResult> CreateFormData(
            string org,
            string app,
            Instance instanceBefore,
            string elementType)
        {
            bool startService = true;
            Guid instanceGuid = Guid.Parse(instanceBefore.Id.Split("/")[1]);
            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType, startService);

            object serviceModel;

            if (Request.ContentType == null)
            {
                serviceModel = serviceImplementation.CreateNewServiceModel();
            }
            else
            {
                serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError);
                if (contentError != null)
                {
                    return(contentError);
                }
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation);

            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            InstancesController.SetAppSelfLinks(instanceBefore, Request);

            Instance instanceAfter = await dataService.InsertFormData(serviceModel, instanceGuid, serviceImplementation.GetServiceModelType(), org, app, int.Parse(instanceBefore.InstanceOwnerId));

            InstancesController.SetAppSelfLinks(instanceAfter, Request);
            List <DataElement> createdElements = CompareAndReturnCreatedElements(instanceBefore, instanceAfter);
            string             dataUrl         = createdElements.First().DataLinks.Apps;

            return(Created(dataUrl, createdElements));
        }
Esempio n. 19
0
        /// <summary>
        /// This method creates new instance in database
        /// </summary>
        public async Task <Guid> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid   instanceId;
            string applicationId      = startServiceModel.Service;
            string applicationOwnerId = startServiceModel.Org;
            int    instanceOwnerId    = startServiceModel.ReporteeID;

            using (HttpClient client = new HttpClient())
            {
                string apiUrl = $"{_platformStorageSettings.ApiUrl}/instances/?applicationId={applicationId}&instanceOwnerId={instanceOwnerId}";
                client.BaseAddress = new Uri(apiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                try
                {
                    HttpResponseMessage response = await client.PostAsync(apiUrl, null);

                    string id = await response.Content.ReadAsAsync <string>();

                    instanceId = Guid.Parse(id);
                }
                catch
                {
                    return(Guid.Parse(string.Empty));
                }
            }

            // Save instantiated form model
            Instance instance = await _data.InsertData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                applicationOwnerId,
                applicationId,
                instanceOwnerId);

            return(instanceId);
        }
Esempio n. 20
0
        /// <summary>
        /// Generates a new service instanceID for a service.
        /// </summary>
        /// <returns>A new instanceId.</returns>
        public async Task <Guid> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid   instanceId         = Guid.NewGuid();
            string applicationId      = startServiceModel.Service;
            string applicationOwnerId = startServiceModel.Org;
            int    instanceOwnerId    = startServiceModel.ReporteeID;

            Instance instance = new Instance
            {
                Id = instanceId.ToString(),
                InstanceOwnerId = instanceOwnerId.ToString(),
                ApplicationId   = applicationId,
                CreatedBy       = instanceOwnerId,
                CreatedDateTime = DateTime.UtcNow,
            };

            string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string apiUrl    = $"{_settings.GetRuntimeAPIPath(SaveInstanceMethod, applicationOwnerId, applicationId, developer, instanceOwnerId)}&instanceId={instanceId}";

            using (HttpClient client = AuthenticationHelper.GetDesignerHttpClient(_httpContextAccessor.HttpContext, _testdataRepositorySettings.GetDesignerHost()))
            {
                client.BaseAddress = new Uri(apiUrl);

                using (MemoryStream stream = new MemoryStream())
                {
                    var          jsonData = JsonConvert.SerializeObject(instance);
                    StreamWriter writer   = new StreamWriter(stream);
                    writer.Write(jsonData);
                    writer.Flush();
                    stream.Position = 0;

                    Task <HttpResponseMessage> response = client.PostAsync(apiUrl, new StreamContent(stream));
                    if (!response.Result.IsSuccessStatusCode)
                    {
                        throw new Exception("Unable to save instance");
                    }
                }
            }

            // Save instantiated form model
            Guid dataId = await _form.SaveFormModel(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                applicationOwnerId,
                applicationId,
                instanceOwnerId,
                Guid.Empty);

            // Update instance with dataId
            instance = await GetInstance(applicationId, applicationOwnerId, instanceOwnerId, instanceId);

            Data data = new Data
            {
                Id          = dataId.ToString(),
                ContentType = "application/Xml",
                StorageUrl  = "data/boatdata/",
                CreatedBy   = instanceOwnerId.ToString(),
            };
            Dictionary <string, Data> formData = new Dictionary <string, Data>();

            formData.Add(dataId.ToString(), data);
            instance.Data = new Dictionary <string, Dictionary <string, Data> >();
            instance.Data.Add("boatData", formData);
            await UpdateInstance(instance, applicationId, applicationOwnerId, instanceOwnerId, instanceId);

            return(instanceId);
        }
        public async Task <IActionResult> Index([FromBody] AltinnCoreApiModel model, string org, string service, string edition, int instanceId, ApiMode apiMode)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            ApiResult apiResult = new ApiResult();

            // Getting the Service Specific Implementation contained in external DLL migrated from TUL
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, edition);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, 0);

            requestContext.UserContext = _userHelper.GetUserContext(HttpContext);
            requestContext.Reportee    = requestContext.UserContext.Reportee;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, edition);

            // Assign the Requestcontext and ViewBag to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, org, service, edition);

            serviceImplementation.SetPlatformServices(platformServices);

            ViewBag.PlatformServices = platformServices;

            dynamic serviceModel = ParseApiBody(serviceImplementation.GetServiceModelType(), out apiResult, model);

            if (serviceModel == null)
            {
                // The parsing did not create any result
                Response.StatusCode = 403;
                return(new ObjectResult(apiResult));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // ServiceEvent 2: HandleGetDataEvent
            // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources
            // based on the data in the service model
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            // RunService 3: Calcuation
            await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

            // ServiceEvent 3: HandleCalculationEvent
            // Perform Calculation defined by the service developer
            // Only perform when the mode is to create a new instance or to specific calculate
            if (apiMode.Equals(ApiMode.Calculate) || apiMode.Equals(ApiMode.Create))
            {
                if (apiMode.Equals(ApiMode.Calculate))
                {
                    // Returns a updated Service model with new calculated data.
                    return(Ok(serviceModel));
                }
            }

            // ServiceEvent 4: HandleValidationEvent
            // Perform additional Validation defined by the service developer.
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Validation);

            // Run the model Validation that handles validation defined on the model
            TryValidateModel(serviceModel);

            // If ApiMode is only validate the instance should not be created and only return any validation errors
            if (apiMode.Equals(ApiMode.Validate) || (!ModelState.IsValid && !apiMode.Equals(ApiMode.Create)))
            {
                MapModelStateToApiResult(ModelState, apiResult, serviceContext);

                if (apiResult.Status.Equals(ApiStatusType.ContainsError))
                {
                    if (apiMode.Equals(ApiMode.Validate))
                    {
                        Response.StatusCode = 202;
                    }
                    else
                    {
                        Response.StatusCode = 400;
                    }

                    return(new ObjectResult(apiResult));
                }

                return(Ok(apiResult));
            }


            // Save Formdata to database
            this._form.SaveFormModel(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                service,
                edition,
                requestContext.UserContext.ReporteeId);

            apiResult.InstanceId = instanceId;
            apiResult.Status     = ApiStatusType.Ok;
            if (!requestContext.RequiresClientSideReleoad)
            {
                return(Ok(apiResult));
            }
            {
                Response.StatusCode = 303;
                return(new ObjectResult(apiResult));
            }
        }
Esempio n. 22
0
        public async Task <ActionResult> PutDataElement(
            [FromRoute] string org,
            [FromRoute] string app,
            [FromRoute] int instanceOwnerId,
            [FromRoute] Guid instanceGuid,
            [FromRoute] Guid dataGuid)
        {
            Instance instance = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid);

            if (instance == null)
            {
                return(NotFound("Instance not found"));
            }

            DataElement dataElement = instance.Data.Find(m => m.Id == dataGuid.ToString());

            if (dataElement == null)
            {
                return(NotFound("Data element not found"));
            }

            string elementType = dataElement.ElementType;
            IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType);

            object serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError);

            if (contentError != null)
            {
                return(contentError);
            }

            if (serviceModel == null)
            {
                return(BadRequest("No data found in content"));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // send events to trigger application business logic
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

            try
            {
                // Run the model Validation that handles validation defined on the model
                TryValidateModel(serviceModel);

                // send events to trigger application business logic
                await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Validation);
            }
            catch (Exception ex)
            {
                logger.LogError($"Validation errors are currently ignored: {ex.Message}");
            }

            // Save Formdata to database
            this.dataService.UpdateData(
                serviceModel,
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                instanceOwnerId,
                dataGuid);

            // Create and store instance saved event
            await DispatchEvent(InstanceEventType.Saved.ToString(), instance, dataGuid);

            return(Ok(serviceModel));
        }
        public async Task <IActionResult> StartService(StartServiceModel startServiceModel)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            bool startService = true;
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(startServiceModel.Org, startServiceModel.Service, startService);

            // Get the service context containing metadata about the service
            ServiceContext serviceContext = _execution.GetServiceContext(startServiceModel.Org, startServiceModel.Service, startService);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, 0);

            requestContext.UserContext = _userHelper.GetUserContext(HttpContext);

            // Populate the reportee information
            requestContext.UserContext.Reportee = _register.GetParty(startServiceModel.ReporteeID);
            requestContext.Reportee             = requestContext.UserContext.Reportee;

            // Create platform service and assign to service implementation making it possible for the service implementation
            // to use plattform services. Also make it available in ViewBag so it can be used from Views
            PlatformServices platformServices = new PlatformServices(_authorization, _repository, _execution, startServiceModel.Org, startServiceModel.Service);

            serviceImplementation.SetPlatformServices(platformServices);
            ViewBag.PlatformServices = platformServices;

            // Assign the different context information to the service implementation making it possible for
            // the service developer to take use of this information
            serviceImplementation.SetContext(requestContext, ViewBag, serviceContext, null, ModelState);

            object serviceModel = null;

            if (!string.IsNullOrEmpty(startServiceModel.PrefillKey))
            {
                _form.GetPrefill(
                    startServiceModel.Org,
                    startServiceModel.Service,
                    serviceImplementation.GetServiceModelType(),
                    startServiceModel.ReporteeID,
                    startServiceModel.PrefillKey);
            }

            if (serviceModel == null)
            {
                // If the service model was not loaded from prefill.
                serviceModel = serviceImplementation.CreateNewServiceModel();
            }

            // Assign service model to the implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // Run Instansiation event
            await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation);

            // Run validate Instansiation event where
            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            // If ValidateInstansiation event has not added any errors the new form is saved and user is redirercted to the correct
            if (ModelState.IsValid)
            {
                if (serviceContext.WorkFlow.Any() && serviceContext.WorkFlow[0].StepType.Equals(StepType.Lookup))
                {
                    return(RedirectToAction("Lookup", new { org = startServiceModel.Org, service = startServiceModel.Service }));
                }

                // Create a new instance Id
                int formID = _execution.GetNewServiceInstanceID(startServiceModel.Org, startServiceModel.Service);

                _form.SaveFormModel(
                    serviceModel,
                    formID,
                    serviceImplementation.GetServiceModelType(),
                    startServiceModel.Org,
                    startServiceModel.Service,
                    requestContext.UserContext.ReporteeId);

                ServiceState currentState = _workflowSI.InitializeService(formID, startServiceModel.Org, startServiceModel.Service, requestContext.UserContext.ReporteeId);
                string       redirectUrl  = _workflowSI.GetUrlForCurrentState(formID, startServiceModel.Org, startServiceModel.Service, currentState.State);
                return(Redirect(redirectUrl));
            }

            startServiceModel.ReporteeList = _authorization.GetReporteeList(requestContext.UserContext.UserId)
                                             .Select(x => new SelectListItem
            {
                Text  = x.ReporteeNumber + " " + x.ReporteeName,
                Value = x.PartyID.ToString(),
            }).ToList();

            HttpContext.Response.Cookies.Append("altinncorereportee", startServiceModel.ReporteeID.ToString());
            return(View(startServiceModel));
        }
Esempio n. 24
0
        public async Task <IActionResult> CompleteAndSendIn(string org, string service, int partyId, Guid instanceGuid, string view)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = await PopulateRequestContext(instanceGuid);

            serviceImplementation.SetPlatformServices(_platformSI);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            PopulateViewBag(org, service, instanceGuid, 0, requestContext, serviceContext, _platformSI);

            // Getting the Form Data
            Instance instance = await _instance.GetInstance(service, org, requestContext.UserContext.PartyId, instanceGuid);

            Guid.TryParse(instance.Data.Find(m => m.ElementType == FORM_ID).Id, out Guid dataId);
            object serviceModel = _data.GetFormData(instanceGuid, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.PartyId, dataId);

            serviceImplementation.SetServiceModel(serviceModel);

            ViewBag.FormID         = instanceGuid;
            ViewBag.ServiceContext = serviceContext;

            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);
            await serviceImplementation.RunServiceEvent(ServiceEventType.Validation);

            ApiResult apiResult = new ApiResult();

            if (ModelState.IsValid)
            {
                ServiceState currentState = _workflowSI.MoveServiceForwardInWorkflow(instanceGuid, org, service, requestContext.UserContext.PartyId);

                if (currentState.State == WorkflowStep.Archived)
                {
                    await _instance.ArchiveInstance(serviceModel, serviceImplementation.GetServiceModelType(), service, org, requestContext.UserContext.PartyId, instanceGuid);

                    apiResult.NextState = currentState.State;
                }

                // Create and store the instance submitted event
                InstanceEvent instanceEvent = new InstanceEvent
                {
                    AuthenticationLevel = requestContext.UserContext.AuthenticationLevel,
                    EventType           = InstanceEventType.Submited.ToString(),
                    InstanceId          = instance.Id,
                    InstanceOwnerId     = instance.InstanceOwnerId.ToString(),
                    UserId       = requestContext.UserContext.UserId,
                    WorkflowStep = instance.Process.CurrentTask,
                };

                await _event.SaveInstanceEvent(instanceEvent, org, service);
            }

            ModelHelper.MapModelStateToApiResult(ModelState, apiResult, serviceContext);

            if (apiResult.Status.Equals(ApiStatusType.ContainsError))
            {
                Response.StatusCode = 202;
            }
            else
            {
                Response.StatusCode = 200;
            }

            return(new ObjectResult(apiResult));
        }
Esempio n. 25
0
        public async Task <IActionResult> Index([FromBody] AltinnCoreApiModel model, string org, string service, Guid instanceId, ApiMode apiMode)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            ApiResult apiResult = new ApiResult();

            // Getting the Service Specific Implementation contained in external DLL migrated from TUL
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, Guid.Empty);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Party = requestContext.UserContext.Party;
            if (Request.Headers.Keys.Contains(VALIDATION_TRIGGER_FIELD))
            {
                requestContext.ValidationTriggerField = Request.Headers[VALIDATION_TRIGGER_FIELD];
            }

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Assign the Requestcontext to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            serviceImplementation.SetPlatformServices(_platformSI);

            ViewBag.PlatformServices = _platformSI;

            dynamic serviceModel = ParseApiBody(serviceImplementation.GetServiceModelType(), out apiResult, model);

            if (serviceModel == null)
            {
                // The parsing did not create any result
                Response.StatusCode = 403;
                return(new ObjectResult(apiResult));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // ServiceEvent 2: HandleGetDataEvent
            // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources
            // based on the data in the service model
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            // RunService 3: Calcuation
            await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

            // ServiceEvent 3: HandleCalculationEvent
            // Perform Calculation defined by the service developer
            // Only perform when the mode is to create a new instance or to specific calculate
            if (apiMode.Equals(ApiMode.Calculate) || apiMode.Equals(ApiMode.Create))
            {
                if (apiMode.Equals(ApiMode.Calculate))
                {
                    // Returns a updated Service model with new calculated data.
                    return(Ok(serviceModel));
                }
            }

            // Run the model Validation that handles validation defined on the model
            TryValidateModel(serviceModel);

            // ServiceEvent 4: HandleValidationEvent
            // Perform additional Validation defined by the service developer. Runs when the ApiMode is set to Validate or Complete.
            if (apiMode.Equals(ApiMode.Validate) || apiMode.Equals(ApiMode.Complete))
            {
                await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Validation);
            }

            // If ApiMode is only validate the instance should not be created and only return any validation errors
            if (apiMode.Equals(ApiMode.Validate) || (!ModelState.IsValid && !apiMode.Equals(ApiMode.Create)))
            {
                MapModelStateToApiResultForClient(ModelState, apiResult, serviceContext);

                if (apiResult.Status.Equals(ApiStatusType.ContainsError))
                {
                    if (apiMode.Equals(ApiMode.Validate))
                    {
                        Response.StatusCode = 202;
                    }
                    else
                    {
                        Response.StatusCode = 400;
                    }

                    return(new ObjectResult(apiResult));
                }

                return(Ok(apiResult));
            }

            Instance instance = await _instance.GetInstance(service, org, requestContext.UserContext.PartyId, instanceId);

            Guid dataId = Guid.Parse(instance.Data.Find(m => m.ElementType.Equals(FORM_ID)).Id);

            // Save Formdata to database
            this._data.UpdateData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                service,
                requestContext.UserContext.PartyId,
                dataId);

            // Create and store instance saved event
            if (apiMode.Equals(ApiMode.Update))
            {
                InstanceEvent instanceEvent = new InstanceEvent
                {
                    AuthenticationLevel = requestContext.UserContext.AuthenticationLevel,
                    EventType           = InstanceEventType.Saved.ToString(),
                    InstanceId          = instance.Id,
                    InstanceOwnerId     = instance.InstanceOwnerId.ToString(),
                    UserId       = requestContext.UserContext.UserId,
                    WorkflowStep = instance.Process.CurrentTask
                };

                await _event.SaveInstanceEvent(instanceEvent, org, service);
            }

            if (apiMode.Equals(ApiMode.Complete))
            {
                ServiceState currentState = _workflowSI.MoveServiceForwardInWorkflow(instanceId, org, service, requestContext.UserContext.PartyId);
                instance.Process = new Storage.Interface.Models.ProcessState()
                {
                    CurrentTask = currentState.State.ToString(),
                    IsComplete  = false,
                };

                Instance updatedInstance = await _instance.UpdateInstance(instance, service, org, requestContext.UserContext.PartyId, instanceId);

                Response.StatusCode   = 200;
                apiResult.InstanceId  = instanceId;
                apiResult.Instance    = updatedInstance;
                apiResult.Status      = ApiStatusType.Ok;
                apiResult.NextStepUrl = _workflowSI.GetUrlForCurrentState(instanceId, org, service, currentState.State);
                apiResult.NextState   = currentState.State;
                return(new ObjectResult(apiResult));
            }

            apiResult.InstanceId = instanceId;
            apiResult.Status     = ApiStatusType.Ok;
            if (!requestContext.RequiresClientSideReleoad)
            {
                return(Ok(apiResult));
            }

            {
                Response.StatusCode = 303;
                return(new ObjectResult(apiResult));
            }
        }
Esempio n. 26
0
        public async Task <IActionResult> Gindex(string org, string service, int partyId, Guid instanceId)
        {
            // Getting the Service Specific Implementation contained in external DLL migrated from TUL
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Party = requestContext.UserContext.Party;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, service, false);

            // Assign data to the ViewBag so it is available to the service views or service implementation
            ViewBag.ServiceContext = serviceContext;
            ViewBag.RequestContext = requestContext;
            ViewBag.Org            = org;
            ViewBag.Service        = service;
            ViewBag.FormID         = instanceId;

            // Assign the RequestContext to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            serviceImplementation.SetPlatformServices(_platformSI);

            ViewBag.PlatformServices = _platformSI;

            Instance instance = await _instance.GetInstance(service, org, requestContext.UserContext.PartyId, instanceId);

            Guid dataId = Guid.Parse(instance.Data.Find(m => m.ElementType.Equals(FORM_ID)).Id);

            // Getting the Form Data from datastore
            object serviceModel = this._data.GetFormData(
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                service,
                requestContext.UserContext.PartyId,
                dataId);

            // Assing the populated service model to the service implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // ServiceEvent 1: HandleGetDataEvent
            // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources
            // based on the data in the service model
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval);

            // ServiceEvent 2: HandleCalculationEvent
            // Perform Calculation defined by the service developer
            await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Calculation);

            return(Ok(serviceModel));
        }
Esempio n. 27
0
        public async Task <IActionResult> StartService(StartServiceModel startServiceModel)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            bool startService = true;
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(startServiceModel.Org, startServiceModel.Service, startService);

            // Get the service context containing metadata about the service
            ServiceContext serviceContext = _execution.GetServiceContext(startServiceModel.Org, startServiceModel.Service, startService);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, Guid.Empty);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            // Populate the reportee information
            requestContext.UserContext.Party = await _register.GetParty(startServiceModel.PartyId);

            requestContext.Party = requestContext.UserContext.Party;

            // Create platform service and assign to service implementation making it possible for the service implementation
            // to use plattform services. Also make it available in ViewBag so it can be used from Views
            serviceImplementation.SetPlatformServices(_platformSI);

            // Assign the different context information to the service implementation making it possible for
            // the service developer to take use of this information
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            object serviceModel = null;

            if (!string.IsNullOrEmpty(startServiceModel.PrefillKey))
            {
                _form.GetPrefill(
                    startServiceModel.Org,
                    startServiceModel.Service,
                    serviceImplementation.GetServiceModelType(),
                    startServiceModel.PartyId,
                    startServiceModel.PrefillKey);
            }

            if (serviceModel == null)
            {
                // If the service model was not loaded from prefill.
                serviceModel = serviceImplementation.CreateNewServiceModel();
            }

            // Assign service model to the implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // Run Instansiation event
            await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation);

            // Run validate Instansiation event where
            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            // If ValidateInstansiation event has not added any errors the new form is saved and user is redirercted to the correct
            if (ModelState.IsValid)
            {
                if (serviceContext.WorkFlow.Any() && serviceContext.WorkFlow[0].StepType.Equals(StepType.Lookup))
                {
                    return(RedirectToAction("Lookup", new { org = startServiceModel.Org, service = startServiceModel.Service }));
                }

                int instanceOwnerId = requestContext.UserContext.PartyId;

                // Create a new instance document
                Instance instance = await _instance.InstantiateInstance(startServiceModel, serviceModel, serviceImplementation);

                // Create and store the instance created event
                InstanceEvent instanceEvent = new InstanceEvent
                {
                    AuthenticationLevel = requestContext.UserContext.AuthenticationLevel,
                    EventType           = InstanceEventType.Created.ToString(),
                    InstanceId          = instance.Id,
                    InstanceOwnerId     = instanceOwnerId.ToString(),
                    UserId       = requestContext.UserContext.UserId,
                    WorkflowStep = instance.Process.CurrentTask
                };

                await _event.SaveInstanceEvent(instanceEvent, startServiceModel.Org, startServiceModel.Service);

                Enum.TryParse <WorkflowStep>(instance.Process.CurrentTask, out WorkflowStep currentStep);

                string redirectUrl = _workflowSI.GetUrlForCurrentState(Guid.Parse(instance.Id), startServiceModel.Org, startServiceModel.Service, currentStep);
                return(Redirect(redirectUrl));
            }

            startServiceModel.PartyList = _authorization.GetPartyList(requestContext.UserContext.UserId)
                                          .Select(x => new SelectListItem
            {
                Text  = (x.PartyTypeName == PartyType.Person) ? x.SSN + " " + x.Name : x.OrgNumber + " " + x.Name,
                Value = x.PartyId.ToString(),
            }).ToList();

            HttpContext.Response.Cookies.Append(_generalSettings.GetAltinnPartyCookieName, startServiceModel.PartyId.ToString());
            return(View(startServiceModel));
        }
Esempio n. 28
0
        public async Task <IActionResult> Index([FromBody] AltinnCoreApiModel model, string org, string app, ApiMode apiMode)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            ApiResult apiResult = new ApiResult();

            // Getting the Service Specific Implementation contained in external DLL migrated from TUL
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, app, false);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // service developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, Guid.Empty);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            requestContext.Party = requestContext.UserContext.Party;

            // Get the serviceContext containing all metadata about current service
            ServiceContext serviceContext = _execution.GetServiceContext(org, app, false);

            // Assign the Requestcontext and ViewBag to the serviceImplementation so
            // service developer can use the information in any of the service events that is called
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            // Set the platform services to the ServiceImplementation so the AltinnCore service can take
            // use of the plattform services
            serviceImplementation.SetPlatformServices(_platformSI);

            dynamic serviceModel = ParseApiBody(serviceImplementation.GetServiceModelType(), out apiResult, model);

            if (serviceModel == null)
            {
                // The parsing did not create any result
                Response.StatusCode = 403;
                return(new ObjectResult(apiResult));
            }

            serviceImplementation.SetServiceModel(serviceModel);

            // ServiceEvent 1: Validate Instansiation. In the service the service developer can add verification to be run at this point
            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            if (!ModelState.IsValid)
            {
                // The validatate instansiation failed
                MapModelStateToApiResult(ModelState, apiResult, serviceContext);
                apiResult.Status    = ApiStatusType.Rejected;
                Response.StatusCode = 403;

                return(new ObjectResult(apiResult));
            }

            // ServiceEvent 2: HandleGetDataEvent
            // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources
            // based on the data in the service model
            await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval);

            // ServiceEvent 3: HandleCalculationEvent
            // Perform Calculation defined by the service developer
            // Only perform when the mode is to create a new instance or to specific calculate
            if (apiMode.Equals(ApiMode.Calculate) || apiMode.Equals(ApiMode.Create))
            {
                await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation);

                if (apiMode.Equals(ApiMode.Calculate))
                {
                    // Returns a updated Service model with new calculated data.
                    return(Ok(serviceModel));
                }
            }

            // Run the model Validation that handles validation defined on the model
            TryValidateModel(serviceModel);

            // ServiceEvent 4: HandleValidationEvent
            // Perform additional Validation defined by the service developer. Runs when the ApiMode is set to Validate or Complete.
            if (apiMode.Equals(ApiMode.Validate) || apiMode.Equals(ApiMode.Complete))
            {
                await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Validation);
            }

            // If ApiMode is only validate the instance should not be created and only return any validation errors
            if (apiMode.Equals(ApiMode.Validate) || (!ModelState.IsValid && !apiMode.Equals(ApiMode.Create)))
            {
                MapModelStateToApiResult(ModelState, apiResult, serviceContext);

                if (apiResult.Status.Equals(ApiStatusType.ContainsError))
                {
                    if (apiMode.Equals(ApiMode.Validate))
                    {
                        Response.StatusCode = 202;
                    }
                    else
                    {
                        Response.StatusCode = 400;
                    }

                    return(new ObjectResult(apiResult));
                }

                return(Ok(apiResult));
            }

            Guid instanceId = _execution.GetNewServiceInstanceID();

            // Save Formdata to database
            Instance instance = this._data.InsertFormData(
                serviceModel,
                instanceId,
                serviceImplementation.GetServiceModelType(),
                org,
                app,
                requestContext.UserContext.PartyId);

            apiResult.InstanceId = instanceId;
            apiResult.Instance   = instance;
            apiResult.Status     = ApiStatusType.Ok;
            return(Ok(apiResult));
        }