/// <inheritdoc/>
        public async Task ProcessAsync(ChangeFeedEntry changeFeedEntry, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(changeFeedEntry, nameof(changeFeedEntry));

            // Create a context used throughout this process.
            var context = new FhirTransactionContext(changeFeedEntry);

            // Prepare all required objects for the transaction.
            foreach (IFhirTransactionPipelineStep pipeline in _fhirTransactionPipelines)
            {
                await pipeline.PrepareRequestAsync(context, cancellationToken);
            }

            // Check to see if any resource needs to be created/updated.
            var bundle = new Bundle()
            {
                Type = Bundle.BundleType.Transaction,
            };

            var usedPropertyAccessors = new List <FhirTransactionRequestResponsePropertyAccessor>(_requestResponsePropertyAccessors.Count);

            foreach (FhirTransactionRequestResponsePropertyAccessor propertyAccessor in _requestResponsePropertyAccessors)
            {
                FhirTransactionRequestEntry requestEntry = propertyAccessor.RequestEntryGetter(context.Request);

                if (requestEntry == null || requestEntry.RequestMode == FhirTransactionRequestMode.None)
                {
                    // No associated request, skip it.
                    continue;
                }

                // There is a associated request, add to the list so it gets processed.
                usedPropertyAccessors.Add(propertyAccessor);
                bundle.Entry.Add(CreateRequestBundleEntryComponent(requestEntry));
            }

            if (!bundle.Entry.Any())
            {
                // Nothing to update.
                return;
            }

            // Execute the transaction.
            Bundle responseBundle = await _fhirTransactionExecutor.ExecuteTransactionAsync(bundle, cancellationToken);

            // Process the response.
            for (int i = 0; i < usedPropertyAccessors.Count; i++)
            {
                FhirTransactionResponseEntry responseEntry = CreateResponseEntry(responseBundle.Entry[i]);

                usedPropertyAccessors[i].ResponseEntrySetter(context.Response, responseEntry);
            }

            // Execute any additional checks of the response.
            foreach (IFhirTransactionPipelineStep pipeline in _fhirTransactionPipelines)
            {
                pipeline.ProcessResponse(context);
            }
        /// <inheritdoc/>
        public void ProcessResponse(FhirTransactionContext context)
        {
            // If the Patient does not exist, we will use conditional create to create the resource
            // to avoid duplicated resource being created. However, if the resource with the identifier
            // was created externally between the retrieve and create, conditional create will return 200
            // and might not contain the changes so we will need to try again.
            if (context.Request.Patient?.RequestMode == FhirTransactionRequestMode.Create)
            {
                FhirTransactionResponseEntry patient = context.Response.Patient;

                HttpStatusCode statusCode = patient.Response.Annotation <HttpStatusCode>();

                if (statusCode == HttpStatusCode.OK)
                {
                    throw new ResourceConflictException();
                }
            }
        }
示例#3
0
        /// <inheritdoc/>
        public void ProcessResponse(FhirTransactionContext context)
        {
            EnsureArg.IsNotNull(context, nameof(context));

            // If the ImagingStudy does not exist, we will use conditional create to create the resource
            // to avoid duplicated resource being created. However, if the resource with the identifier
            // was created externally between the retrieve and create, conditional create will return 200
            // and might not contain the changes so we will need to try again.
            if (context.Request.ImagingStudy?.RequestMode == FhirTransactionRequestMode.Create)
            {
                FhirTransactionResponseEntry imagingStudy = context.Response.ImagingStudy;

                HttpStatusCode statusCode = imagingStudy.Response.Annotation <HttpStatusCode>();

                if (statusCode == HttpStatusCode.OK)
                {
                    throw new ResourceConflictException();
                }
            }
        }
        /// <inheritdoc/>
        public async Task ProcessAsync(ChangeFeedEntry changeFeedEntry, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(changeFeedEntry, nameof(changeFeedEntry));

            // Create a context used throughout this process.
            var context = new FhirTransactionContext(changeFeedEntry);

            // Prepare all required objects for the transaction.
            foreach (IFhirTransactionPipelineStep pipeline in _fhirTransactionPipelines)
            {
                await pipeline.PrepareRequestAsync(context, cancellationToken);
            }

            // Check to see if any resource needs to be created/updated.
            var bundle = new Bundle()
            {
                Type = Bundle.BundleType.Transaction,
            };

            var usedPropertyAccessors = new List <(FhirTransactionRequestResponsePropertyAccessor Accessor, int Count)>(_requestResponsePropertyAccessors.Count);

            foreach (FhirTransactionRequestResponsePropertyAccessor propertyAccessor in _requestResponsePropertyAccessors)
            {
                List <FhirTransactionRequestEntry> requestEntries = propertyAccessor.RequestEntryGetter(context.Request)?.ToList();

                if (requestEntries == null || requestEntries.Count == 0)
                {
                    continue;
                }

                int useCount = 0;
                foreach (FhirTransactionRequestEntry requestEntry in requestEntries)
                {
                    if (requestEntry == null || requestEntry.RequestMode == FhirTransactionRequestMode.None)
                    {
                        // No associated request, skip it.
                        continue;
                    }

                    // There is a associated request, add to the list so it gets processed.
                    bundle.Entry.Add(CreateRequestBundleEntryComponent(requestEntry));
                    useCount++;
                }

                usedPropertyAccessors.Add((propertyAccessor, useCount));
            }

            if (bundle.Entry.Count == 0)
            {
                // Nothing to update.
                return;
            }

            // Execute the transaction.
            Bundle responseBundle = await _fhirTransactionExecutor.ExecuteTransactionAsync(bundle, cancellationToken);

            // Process the response.
            int processedResponseItems = 0;

            foreach ((FhirTransactionRequestResponsePropertyAccessor accessor, int count) in
                     usedPropertyAccessors.Where(x => x.Count > 0))
            {
                var responseEntries = new List <FhirTransactionResponseEntry>();
                for (int j = 0; j < count; j++)
                {
                    FhirTransactionResponseEntry responseEntry = CreateResponseEntry(responseBundle.Entry[processedResponseItems + j]);
                    responseEntries.Add(responseEntry);
                }

                processedResponseItems += count;
                accessor.ResponseEntrySetter(context.Response, responseEntries);
            }

            // Execute any additional checks of the response.
            foreach (IFhirTransactionPipelineStep pipeline in _fhirTransactionPipelines)
            {
                pipeline.ProcessResponse(context);
            }