Beispiel #1
0
        private string GetContentAs(string content, FhirMimeType mimeType)
        {
            var serializer = new SerializationWrapper(_arguments.FhirVersion);
            var resource   = serializer.Parse(content);

            return(serializer.Serialize(resource, mimeType));
        }
        private async IAsyncEnumerable <ResourceWrapper> ExpandInputFiles()
        {
            var serializer = new SerializationWrapper(_arguments.FhirVersion);
            var content    = await File.ReadAllTextAsync(_arguments.QuestionnairePath);

            var resource = serializer.Parse(content, _arguments.MimeType);

            if (resource == null)
            {
                _issues.Add(new Issue
                {
                    Details  = $"Failed to deserialize Questionnaire from file\nLocation: '{_arguments.QuestionnairePath}'.",
                    Severity = IssueSeverityEnum.Error,
                });

                yield break;
            }

            if (resource.ResourceType == ResourceTypeWrapper.Bundle)
            {
                var bundle = resource.CastToBundle();
                foreach (var r in bundle.GetResources())
                {
                    if (r.ResourceType != ResourceTypeWrapper.Questionnaire)
                    {
                        continue;
                    }
                    yield return(r);
                }
            }
            else
            {
                yield return(resource);
            }
        }
        private async Tasks.Task <BundleWrapper> CreateBundle(IEnumerable <string> batch, FhirVersion fhirVersion)
        {
            var serializer = new SerializationWrapper(fhirVersion);
            var bundle     = new BundleWrapper(fhirVersion);

            foreach (var file in batch)
            {
                ResourceWrapper resource = null;
                try
                {
                    var content = await File.ReadAllTextAsync(file);

                    resource = serializer.Parse(content, _arguments.MimeType);
                }
                catch (Exception e)
                {
                    _logger.LogError($"Could not read and parse {file}. Skipping: {e.Message}");
                }
                var entry   = bundle.AddResourceEntry(resource, null);
                var request = new RequestComponentWrapper(fhirVersion);
                request.Method = HTTPVerbWrapper.PUT;
                request.Url    = $"/{resource.ResourceType}/{resource.Id}";
                entry.Request  = request;
                _logger.LogInformation($"  Added {request.Url} to bundle");
            }

            return(bundle);
        }
Beispiel #4
0
        public string Convert(string fromString)
        {
            var parser     = new SerializationWrapper(FromVersion);
            var serializer = new SerializationWrapper(ToVersion);

            var fromObject = parser.Parse(fromString);
            var converted  = Convert <Base, Base>(fromObject.Resource);

            return(serializer.Serialize(converted));
        }
Beispiel #5
0
        //[InlineData(@"TestData\account.profile-r4.json")]
        //[InlineData(@"TestData\activitydefinition.profile-r4.json")]
        //[InlineData(@"TestData\adverseevent.profile-r4.json")]
        //[InlineData(@"TestData\allergyintolerance.profile-r4.json")]
        //[InlineData(@"TestData\appointment.profile-r4.json")]
        //[InlineData(@"TestData\appointmentresponse.profile-r4.json")]
        //[InlineData(@"TestData\consent.profile-r4.json")]
        public async Task CanConvert_Questionnaire_FromR4ToR3_RoundTrip(string path)
        {
            var converterFromR4ToR3 = new FhirConverter(to: FhirVersion.R3, from: FhirVersion.R4);
            var converterFromR3ToR4 = new FhirConverter(to: FhirVersion.R4, from: FhirVersion.R3);

            var r4Serializer = new SerializationWrapper(FhirVersion.R4);
            var r4Resource   = r4Serializer.Parse(await File.ReadAllTextAsync(path));

            var r3Resource          = converterFromR4ToR3.Convert <Resource, Resource>(r4Resource.Resource);
            var r4ResourceRoundTrip = converterFromR3ToR4.Convert <Resource, Resource>(r3Resource);

            var r4ResourceContent          = r4Serializer.Serialize(r4Resource);
            var r4ResourceRoundTripContent = r4Serializer.Serialize(r4ResourceRoundTrip);

            Assert.Equal(r4ResourceContent, r4ResourceRoundTripContent);
        }
        public override async Task <OperationResultEnum> Execute()
        {
            _logger.LogInformation($"Starting conversion of source file: '{_arguments.Source}' from format: '{_arguments.FromMimeType}' to format '{_arguments.ToMimeType}'.");

            var serializer = new SerializationWrapper(_arguments.FhirVersion);
            var resource   = serializer.Parse(_arguments.SourceContent, _arguments.FromMimeType, true);
            var outContent = serializer.Serialize(resource, _arguments.ToMimeType.GetValueOrDefault());
            var fileName   = Path.GetFileName(_arguments.Source);
            var outPath    = GetOutPath(_arguments.OutPath, fileName, _arguments.ToMimeType.GetValueOrDefault());

            if (!string.IsNullOrWhiteSpace(outContent))
            {
                using var stream = File.Open(outPath, FileMode.OpenOrCreate, FileAccess.Write);
                await stream.WriteAsync(Encoding.UTF8.GetBytes(outContent));

                _logger.LogInformation($"Converted file stored at: '{outPath}'.");
            }

            return(await Task.FromResult(OperationResultEnum.Succeeded));
        }
        public override async Tasks.Task <OperationResultEnum> Execute()
        {
            var serializer = new SerializationWrapper(_arguments.FhirVersion);
            var lines      = await File.ReadAllLinesAsync(_arguments.DumpFile.Path);

            var linenr = 0;

            var versionMap = new Dictionary <string, Data>();

            foreach (var entry in lines)
            {
                linenr++;
                try
                {
                    var json = JObject.Parse(entry);

                    var versionPath = json["_id"].ToString();
                    json.Property("_id").Remove();
                    var id = $"{json["resourceType"]}/{json["id"]}";

                    if (_arguments.SkipIds.Contains(id))
                    {
                        _logger.LogInformation($"Skipping {id} as requested");
                        continue;
                    }

                    RemoveUnwantedMongoDbElements(json);

                    var dataEntry = new Data
                    {
                        Id          = id,
                        JObject     = json,
                        Line        = linenr,
                        RawContent  = entry,
                        VersionPath = versionPath
                    };

                    if (versionMap.TryGetValue(id, out Data other))
                    {
                        var version      = GetVersion(json);
                        var otherVersion = GetVersion(other.JObject);

                        if (version > otherVersion)
                        {
                            versionMap.Remove(id);
                            versionMap.Add(id, dataEntry);
                        }
                    }
                    else
                    {
                        versionMap.Add(id, dataEntry);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError($"line {linenr}: Not valid json{Environment.NewLine}{e.Message}");
                    if (_arguments.ShowOriginalJson)
                    {
                        _logger.LogInformation(entry);
                    }
                    continue;
                }
            }

            foreach (var key in versionMap.Keys)
            {
                var data = versionMap[key];
                try
                {
                    var content  = data.JObject.ToString();
                    var resource = serializer.Parse(content, permissiveParsing: _arguments.PermissiveParsing);
                }
                catch (Exception e)
                {
                    _logger.LogError($"line {data.Line}: resource {key} is not a valid FHIR object (was {data.VersionPath}){Environment.NewLine}{e.Message}");
                    if (_arguments.ShowOriginalJson)
                    {
                        _logger.LogInformation(data.RawContent);
                    }
                    continue;
                }
            }

            return(await Tasks.Task.FromResult(OperationResultEnum.Succeeded));
        }