public void ShouldDeserializeResourceInJson()
        {
            var json     = "{ \"Name\":\"John Doe\", \"Amount\":123.45 }";
            var resource = deserializer.Deserialize(json, typeof(SomeResource)) as SomeResource;

            Assert.AreEqual("John Doe", resource.Name);
            Assert.AreEqual(123.45, resource.Amount);
        }
        private async Task ProcessSearchResultsAsync(IEnumerable <SearchResultEntry> searchResults, string partId, IAnonymizer anonymizer, CancellationToken cancellationToken)
        {
            foreach (SearchResultEntry result in searchResults)
            {
                ResourceWrapper resourceWrapper = result.Resource;
                ResourceElement element         = _resourceDeserializer.Deserialize(resourceWrapper);

                if (anonymizer != null)
                {
                    try
                    {
                        element = anonymizer.Anonymize(element);
                    }
                    catch (Exception ex)
                    {
                        throw new FailedToAnonymizeResourceException(ex.Message, ex);
                    }
                }

                // Serialize into NDJson and write to the file.
                byte[] bytesToWrite = _resourceToByteArraySerializer.Serialize(element);

                await _fileManager.WriteToFile(resourceWrapper.ResourceTypeName, partId, bytesToWrite, cancellationToken);
            }
        }
Beispiel #3
0
        private ResourceElement CreatePatientWithIdentity(ResourceElement patient, SearchResult results)
        {
            var searchMatchOnly = results.Results.Where(x => x.SearchEntryMode == ValueSets.SearchEntryMode.Match).ToList();

            if (searchMatchOnly.Count > 1)
            {
                throw new MemberMatchMatchingException(Core.Resources.MemberMatchMultipleMatchesFound);
            }

            if (searchMatchOnly.Count == 0)
            {
                throw new MemberMatchMatchingException(Core.Resources.MemberMatchNoMatchFound);
            }

            var match        = searchMatchOnly[0];
            var element      = _resourceDeserializer.Deserialize(match.Resource);
            var foundPatient = element.ToPoco <Patient>();
            var id           = foundPatient.Identifier.Where(x => x.Type != null && x.Type.Coding != null && x.Type.Coding.Exists(x => x.Code == "MB")).FirstOrDefault();

            if (id == null)
            {
                throw new MemberMatchMatchingException(Core.Resources.MemberMatchNoMatchFound);
            }

            var resultPatient = patient.ToPoco <Patient>();
            var resultId      = new Identifier(id.System, id.Value);

            resultId.Type = new CodeableConcept("http://terminology.hl7.org/CodeSystem/v2-0203", "UMB", "Member Match");
            resultPatient.Identifier.Add(resultId);
            var result = resultPatient.ToResourceElement();

            return(result);
        }
Beispiel #4
0
        private async Task ProcessSearchResultsAsync(IEnumerable <SearchResultEntry> searchResults, string partId, IAnonymizer anonymizer, CancellationToken cancellationToken)
        {
            foreach (SearchResultEntry result in searchResults)
            {
                ResourceWrapper resourceWrapper = result.Resource;

                string resourceType = resourceWrapper.ResourceTypeName;

                // Check whether we already have an existing file for the current resource type.
                if (!_resourceTypeToFileInfoMapping.TryGetValue(resourceType, out ExportFileInfo exportFileInfo))
                {
                    // Check whether we have seen this file previously (in situations where we are resuming an export)
                    if (_exportJobRecord.Output.TryGetValue(resourceType, out exportFileInfo))
                    {
                        // A file already exists for this resource type. Let us open the file on the client.
                        await _exportDestinationClient.OpenFileAsync(exportFileInfo.FileUri, cancellationToken);
                    }
                    else
                    {
                        // File does not exist. Create it.
                        string fileName;
                        if (_exportJobRecord.StorageAccountContainerName.Equals(_exportJobRecord.Id, StringComparison.OrdinalIgnoreCase))
                        {
                            fileName = $"{resourceType}.ndjson";
                        }
                        else
                        {
                            string dateTime = _exportJobRecord.QueuedTime.UtcDateTime.ToString("s")
                                              .Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase)
                                              .Replace(":", string.Empty, StringComparison.OrdinalIgnoreCase);
                            fileName = $"{dateTime}-{_exportJobRecord.Id}/{resourceType}.ndjson";
                        }

                        Uri fileUri = await _exportDestinationClient.CreateFileAsync(fileName, cancellationToken);

                        exportFileInfo = new ExportFileInfo(resourceType, fileUri, sequence: 0);

                        // Since we created a new file the JobRecord Output also needs to know about it.
                        _exportJobRecord.Output.TryAdd(resourceType, exportFileInfo);
                    }

                    _resourceTypeToFileInfoMapping.Add(resourceType, exportFileInfo);
                }

                ResourceElement element = _resourceDeserializer.Deserialize(resourceWrapper);

                if (anonymizer != null)
                {
                    element = anonymizer.Anonymize(element);
                }

                // Serialize into NDJson and write to the file.
                byte[] bytesToWrite = _resourceToByteArraySerializer.Serialize(element);

                await _exportDestinationClient.WriteFilePartAsync(exportFileInfo.FileUri, partId, bytesToWrite, cancellationToken);

                // Increment the file information.
                exportFileInfo.IncrementCount(bytesToWrite.Length);
            }
        }
        /// <inheritdoc />
        public void Update(ResourceWrapper resourceWrapper)
        {
            var resourceElement = _resourceDeserializer.Deserialize(resourceWrapper);
            var newIndices      = _searchIndexer.Extract(resourceElement);

            ExtractMinAndMaxValues(newIndices);
            resourceWrapper.UpdateSearchIndices(newIndices);
        }
Beispiel #6
0
 public object Build(string xml, Type objectType)
 {
     try
     {
         return(string.IsNullOrEmpty(xml) ? null : deserializer.Deserialize(xml, objectType));
     } catch (Exception e)
     {
         throw new UnmarshallingException(e.Message);
     }
 }
Beispiel #7
0
        public async Task <ReindexSingleResourceResponse> Handle(ReindexSingleResourceRequest request, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(request, nameof(request));

            if (await _authorizationService.CheckAccess(DataActions.Reindex) != DataActions.Reindex)
            {
                throw new UnauthorizedFhirActionException();
            }

            var             key            = new ResourceKey(request.ResourceType, request.ResourceId);
            ResourceWrapper storedResource = await _fhirDataStore.GetAsync(key, cancellationToken);

            if (storedResource == null)
            {
                throw new ResourceNotFoundException(string.Format(Core.Resources.ResourceNotFoundById, request.ResourceType, request.ResourceId));
            }

            // We need to extract the "new" search indices since the assumption is that
            // a new search parameter has been added to the fhir server.
            ResourceElement resourceElement = _resourceDeserializer.Deserialize(storedResource);
            IReadOnlyCollection <SearchIndexEntry> newIndices = _searchIndexer.Extract(resourceElement);

            // Create a new parameter resource and include the new search indices and the corresponding values.
            var parametersResource = new Parameters
            {
                Id        = Guid.NewGuid().ToString(),
                VersionId = "1",
                Parameter = new List <Parameters.ParameterComponent>(),
            };

            parametersResource.Parameter.Add(new Parameters.ParameterComponent()
            {
                Name = "originalResourceId", Value = new FhirString(request.ResourceId)
            });
            parametersResource.Parameter.Add(new Parameters.ParameterComponent()
            {
                Name = "originalResourceType", Value = new FhirString(request.ResourceType)
            });

            foreach (SearchIndexEntry searchIndex in newIndices)
            {
                parametersResource.Parameter.Add(new Parameters.ParameterComponent()
                {
                    Name = searchIndex.SearchParameter.Name.ToString(), Value = new FhirString(searchIndex.Value.ToString())
                });
            }

            return(new ReindexSingleResourceResponse(parametersResource.ToResourceElement()));
        }