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));
        }
Exemple #2
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 = _userHelper.GetUserContext(HttpContext);
            requestContext.Reportee    = requestContext.UserContext.Reportee;
            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 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);

            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));
                }
            }

            // 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));
            }

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

            if (apiMode.Equals(ApiMode.Complete))
            {
                ServiceState currentState = _workflowSI.MoveServiceForwardInWorkflow(instanceId, org, service, requestContext.UserContext.ReporteeId);
                Response.StatusCode   = 200;
                apiResult.InstanceId  = instanceId;
                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));
            }
        }