Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        private object ParseApiBody(Type modelType, out ApiResult apiResult, AltinnCoreApiModel model)
        {
            apiResult = new ApiResult();

            object serviceModel = null;

            if (Request.ContentType.Contains("application/json"))
            {
                try
                {
                    serviceModel = JsonConvert.DeserializeObject(model.BodyContent, modelType);
                }
                catch (Exception ex)
                {
                    apiResult.Message = ex.Message;
                    apiResult.Status  = ApiStatusType.Rejected;
                    if (ex.InnerException != null)
                    {
                        apiResult.Message += " " + ex.InnerException.Message;
                    }
                }
            }
            else if (Request.ContentType.Contains("application/xml"))
            {
                try
                {
                    using (TextReader sr = new StringReader(model.BodyContent))
                    {
                        XmlSerializer serializer = new XmlSerializer(modelType);
                        serviceModel = serializer.Deserialize(sr);
                    }
                }
                catch (Exception ex)
                {
                    apiResult.Message = ex.Message;
                    apiResult.Status  = ApiStatusType.Rejected;
                    if (ex.InnerException != null)
                    {
                        apiResult.Message += " " + ex.InnerException.Message;
                    }
                }
            }

            return(serviceModel);
        }
Ejemplo n.º 3
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));
            }
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
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));
        }