Exemplo n.º 1
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));
        }
Exemplo n.º 2
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));
        }
        public async Task <IActionResult> Lookup(string reportee, string org, string service, string edition)
        {
            // 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 = serviceImplementation.CreateNewServiceModel();

            serviceImplementation.SetServiceModel(serviceModel);

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

            // 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));
        }
Exemplo n.º 4
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));
        }
        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));
        }