Exemple #1
0
        private bool ProcessCompartmentDefinition(string ResourceId)
        {
            //First Get the CompartmentDefinition resource
            //Request: GET [base]/CompartmentDefinition/pyro-patient
            var GetRequestMeta         = IRequestMetaFactory.CreateRequestMeta().Set(ResourceType.CompartmentDefinition, $"{ResourceId}");
            var ResourceServiceOutcome = IResourceServices.GetRead(ResourceId, GetRequestMeta);

            if (ResourceServiceOutcome.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                if (ResourceServiceOutcome.ResourceResult is CompartmentDefinition Def)
                {
                    //Check if it is already an Active Compartment
                    var ActiveCoding = IPyroFhirServerCodeSystem.GetCoding(Common.PyroHealthFhirResource.CodeSystems.PyroFhirServer.Codes.ActiveCompartment);
                    if (Def.Meta.Tag.Any(x => x.System == ActiveCoding.System && x.Code == ActiveCoding.Code))
                    {
                        //If it is then set to Active (Note: On server start we will have bypassed the Protected Resource check so need to InActivate and then Activate it to make sure the
                        //Compartment is set correctly against the loaded Resource in the database table [_ServiceCompartment])
                        //It is highly likley that this never occurs unless this task run on server that already has Compartments set, first time installs this would never happen.
                        //Request: GET [base]/CompartmentDefinition/pyro-patient/x-set-compartment-inactive
                        var GetInActivateRequestMeta            = IRequestMetaFactory.CreateRequestMeta().Set(ResourceType.CompartmentDefinition, $"{ResourceId}/${FhirOperationEnum.OperationType.xSetCompartmentInActive.GetPyroLiteral()}");
                        var ResourceServiceOutcomeGetOpInActive = IFhirResourceInstanceOperationService.ProcessGet(ResourceType.CompartmentDefinition.GetLiteral(), ResourceId, FhirOperationEnum.OperationType.xSetCompartmentInActive.GetPyroLiteral(), GetInActivateRequestMeta);
                        //Note: When a Compartment is made InActive it returns 204 NoContent to indicate it has been made InActive
                        if (ResourceServiceOutcomeGetOpInActive.HttpStatusCode == System.Net.HttpStatusCode.NoContent)
                        {
                            return(SetCompatmentAsActive(ResourceId));
                        }
                        else
                        {
                            ILog.Warn($"The resource CompartmentDefinition/{ResourceId} was marked as an Active compartment yet the server was not able to set the compartment to InActive inorder to update it. The HTTP status returned was: {ResourceServiceOutcome.HttpStatusCode.ToString()}");
                            return(false);
                        }
                    }
                    else
                    {
                        //The Compartment is not set as Active so we can just Activate it
                        //Note: ToDo: Do we actualy check that a Compartment code i.e 'Patient' is not set twice by two seperate resources?
                        return(SetCompatmentAsActive(ResourceId));
                    }
                }
                else
                {
                    ILog.Warn($"Unable to cast FHIR resource returned by get CompartmentDefinition/{ResourceId} to a CompartmentDefinition resource.");
                    return(false);
                }
            }
            else
            {
                ILog.Warn($"Unable to get the CompartmentDefinition resource inorder to set the Compartment on server startup. The resource was CompartmentDefinition/{ResourceId}. The HTTP status returned was: {ResourceServiceOutcome.HttpStatusCode.ToString()} ");
                return(false);
            }
        }
        public IResourceServiceOutcome OperationGetResourceInstanceWithParameters(string BaseRequestUri, HttpRequestMessage Request, string ResourceName, string operation, string FhirId)
        {
            using (DbContextTransaction Transaction = IUnitOfWork.BeginTransaction())
            {
                try
                {
                    IRequestServiceRootValidate.Validate(BaseRequestUri);
                    IRequestMeta RequestMeta = IRequestMetaFactory.CreateRequestMeta().Set(Request);
                    IFhirResourceInstanceOperationService FhirResourceInstanceOperationService = IFhirResourceInstanceOperationServiceFactory.CreateFhirResourceInstanceOperationService();
                    IResourceServiceOutcome ResourceServiceOutcome = FhirResourceInstanceOperationService.ProcessGet(ResourceName, FhirId, operation, RequestMeta);
                    ResourceServiceOutcome.SummaryType = RequestMeta.SearchParameterGeneric.SummaryType;
                    if (ResourceServiceOutcome.SuccessfulTransaction)
                    {
                        Transaction.Commit();
                    }
                    else
                    {
                        Transaction.Rollback();
                    }

                    return(ResourceServiceOutcome);
                }
                catch (Exception Exec)
                {
                    Transaction.Rollback();
                    ILog.Error(Exec, $"PyroService.ResourceInstanceOperationWithParameters, Request: {Request.RequestUri.OriginalString}");
                    throw new PyroException(System.Net.HttpStatusCode.InternalServerError,
                                            Common.Tools.FhirOperationOutcomeSupport.Create(OperationOutcome.IssueSeverity.Error, OperationOutcome.IssueType.Exception, Exec.Message), Exec.Message);
                }
            }
        }