public HttpResponseMessage UpdateValueSet(
            [FromBody] FhirValueSet fhirValueSet,
            [FromUri] int valueSetId,
            [FromUri(Name = "_format")] string format = null)
        {
            ValueSetExporter exporter         = new ValueSetExporter(this.tdb);
            ValueSetImporter importer         = new ValueSetImporter(this.tdb);
            ValueSet         originalValueSet = this.tdb.ValueSets.Single(y => y.Id == valueSetId);
            ValueSet         newValueSet      = importer.Convert(fhirValueSet, valueSet: originalValueSet);

            if (originalValueSet == null)
            {
                this.tdb.ValueSets.Add(newValueSet);
            }

            this.tdb.SaveChanges();

            string location = string.Format("{0}://{1}/api/FHIR3/ValueSet/{2}",
                                            this.Request.RequestUri.Scheme,
                                            this.Request.RequestUri.Authority,
                                            fhirValueSet.Id);

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add("Location", location);

            FhirValueSet updatedFhirValueSet = exporter.Convert(newValueSet);

            return(Shared.GetResponseMessage(this.Request, format, updatedFhirValueSet, originalValueSet != null ? 200 : 201, headers));
        }
        public HttpResponseMessage CreateValueSet(
            [FromBody] FhirValueSet fhirValueSet,
            [FromUri(Name = "_format")] string format = null)
        {
            var foundValueSets = (from vs in this.tdb.ValueSets
                                  join vsi in this.tdb.ValueSetIdentifiers on vs.Id equals vsi.ValueSetId
                                  join fvsi in fhirValueSet.Identifier on vsi.Identifier.ToLower().Trim() equals fvsi.Value.ToLower().Trim()
                                  select vs)
                                 .Distinct();

            if (foundValueSets.Count() > 0)
            {
                throw new Exception("ValueSet already exists with this identifier. Use a PUT instead");
            }

            ValueSetImporter importer = new ValueSetImporter(this.tdb);
            ValueSetExporter exporter = new ValueSetExporter(this.tdb);
            ValueSet         valueSet = importer.Convert(fhirValueSet);

            this.tdb.ValueSets.Add(valueSet);
            this.tdb.SaveChanges();

            string location = string.Format("{0}://{1}/api/FHIR3/ValueSet/{2}",
                                            this.Request.RequestUri.Scheme,
                                            this.Request.RequestUri.Authority,
                                            fhirValueSet.Id);

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add("Location", location);

            FhirValueSet createdFhirValueSet = exporter.Convert(valueSet);

            return(Shared.GetResponseMessage(this.Request, format, createdFhirValueSet, statusCode: 201, headers: headers));
        }
Exemple #3
0
        private void PopulateIdentifier(ValueSet valueSet, FhirValueSet fhirValueSet)
        {
            var existingIdentifiers = valueSet.Identifiers.Where(y => y.Type == ValueSetIdentifierTypes.HTTP).ToList();

            // Remove HTTP identifiers that are not found in the FHIR ValueSet's identifiers
            for (var i = existingIdentifiers.Count - 1; i >= 0; i--)
            {
                var existingIdentifier = existingIdentifiers[i];

                if (!fhirValueSet.Identifier.Any(y => y.Value.ToLower().Trim() == existingIdentifier.Identifier.ToLower().Trim()))
                {
                    this.tdb.ValueSetIdentifiers.Remove(existingIdentifier);
                }
            }

            // Add identifiers from the FHIR ValueSet's identifiers that don't already exist
            foreach (var fhirIdentifer in fhirValueSet.Identifier)
            {
                if (!existingIdentifiers.Any(y => y.Identifier.ToLower().Trim() == fhirIdentifer.Value.ToLower().Trim()))
                {
                    valueSet.Identifiers.Add(new ValueSetIdentifier()
                    {
                        Type       = ValueSetIdentifierTypes.HTTP,
                        Identifier = fhirIdentifer.Value
                    });
                }
            }
        }
        public HttpResponseMessage GetValueSetExpansion(
            [FromUri] int valueSetId,
            [FromUri(Name = "_format")] string format        = null,
            [FromUri(Name = "_summary")] SummaryType?summary = null)
        {
            ValueSet         valueSet     = this.tdb.ValueSets.Single(y => y.Id == valueSetId);
            ValueSetExporter exporter     = new ValueSetExporter(this.tdb);
            FhirValueSet     fhirValueSet = exporter.Convert(valueSet, summary);

            return(Shared.GetResponseMessage(this.Request, format, fhirValueSet));
        }
        /// <summary>
        /// Converts a Trifolia ValueSet model to a FHIR ValueSet model.
        /// </summary>
        /// <param name="valueSet">The Trifolia ValueSet model to convert to a FHIR model</param>
        /// <param name="summaryType">Does not populate certain fields when a summaryType is specified.</param>
        /// <param name="publishedValueSets">Optional list of ValueSets that are used by a published implementation guide. If not specified, queries the database for implementation guides that this value set may be published under.</param>
        /// <returns>A FHIR ValueSet model</returns>
        public FhirValueSet Convert(ValueSet valueSet, SummaryType?summaryType = null, IEnumerable <ValueSet> publishedValueSets = null, string baseUrl = null)
        {
            bool usedByPublishedIgs = false;

            if (publishedValueSets == null)
            {
                var implementationGuides = (from tc in valueSet.Constraints
                                            join t in this.tdb.Templates on tc.TemplateId equals t.Id
                                            select t.OwningImplementationGuide);
                usedByPublishedIgs = implementationGuides.Count(y => y.PublishStatus != null && y.PublishStatus.IsPublished) > 0;
            }
            else
            {
                usedByPublishedIgs = publishedValueSets.Contains(valueSet);
            }

            FhirValueSet fhirValueSet = new FhirValueSet()
            {
                Meta = new Meta()
                {
                    ElementId = valueSet.Id.ToString()
                },
                Id          = valueSet.GetFhirId(),
                Name        = valueSet.Name,
                Status      = usedByPublishedIgs ? PublicationStatus.Active : PublicationStatus.Draft,
                Description = new Markdown(valueSet.Description),
                Url         = valueSet.GetIdentifier(ValueSetIdentifierTypes.HTTP)
            };

            // Handle urn:oid: and urn:hl7ii: identifiers differently if a base url is provided
            // baseUrl is most likely provided when within the context of an implementation guide
            if (fhirValueSet.Url != null)
            {
                if (fhirValueSet.Url.StartsWith("urn:oid:") && !string.IsNullOrEmpty(baseUrl))
                {
                    fhirValueSet.Url = baseUrl.TrimEnd('/') + "/ValueSet/" + fhirValueSet.Url.Substring(8);
                }
                else if (fhirValueSet.Url.StartsWith("urn:hl7ii:") && !string.IsNullOrEmpty(baseUrl))
                {
                    fhirValueSet.Url = baseUrl.TrimEnd('/') + "/ValueSet/" + fhirValueSet.Url.Substring(10);
                }
            }

            List <ValueSetMember> activeMembers = valueSet.GetActiveMembers(DateTime.Now);

            if (activeMembers.Count > 0)
            {
                // If the value set was created in Trifolia, then Trifolia contains the definition
                // and it should be represented by <compose>
                if (valueSet.ImportSource == null)
                {
                    // Compose
                    var compose = new FhirValueSet.ComposeComponent();
                    fhirValueSet.Compose = compose;

                    foreach (var groupedMember in activeMembers.GroupBy(y => y.CodeSystem, y => y))
                    {
                        var include = new FhirValueSet.ConceptSetComponent();
                        compose.Include.Add(include);

                        include.System = groupedMember.Key.Oid;

                        foreach (var member in groupedMember)
                        {
                            include.Concept.Add(new FhirValueSet.ConceptReferenceComponent()
                            {
                                Code    = member.Code,
                                Display = member.DisplayName
                            });
                        }
                    }
                }
                else
                {
                    // If the value set was imported, then we have the expansion,
                    // but we don't have the defintion (the <compose>).
                    var expansion = new FhirValueSet.ExpansionComponent();
                    fhirValueSet.Expansion = expansion;
                    expansion.Identifier   = fhirValueSet.Url;
                    expansion.Total        = activeMembers.Count;

                    if (valueSet.LastUpdate != null)
                    {
                        expansion.TimestampElement = new FhirDateTime(valueSet.LastUpdate.Value);
                    }
                    else
                    {
                        expansion.TimestampElement = new FhirDateTime(DateTime.Now);
                    }

                    expansion.Contains = (from m in activeMembers
                                          select new FhirValueSet.ContainsComponent()
                    {
                        System = m.CodeSystem.Oid,
                        Code = m.Code,
                        Display = m.DisplayName
                    }).ToList();
                }
            }

            return(fhirValueSet);
        }
Exemple #6
0
        public ValueSet Convert(FhirValueSet fhirValueSet, ValueSet valueSet = null)
        {
            string fhirDescription = fhirValueSet.Description != null ? fhirValueSet.Description.Value : null;

            if (valueSet == null)
            {
                valueSet = new ValueSet();
            }

            if (valueSet.Name != fhirValueSet.Name)
            {
                valueSet.Name = fhirValueSet.Name;
            }

            if (valueSet.Description != fhirDescription)
            {
                valueSet.Description = fhirDescription;
            }

            if (fhirValueSet.Identifier == null)
            {
                throw new Exception("ValueSet.identifier.value is required");
            }

            this.PopulateIdentifier(valueSet, fhirValueSet);

            if (fhirValueSet.Expansion != null)
            {
                foreach (var expContains in fhirValueSet.Expansion.Contains)
                {
                    // Skip members that don't have a code or a code system
                    if (string.IsNullOrEmpty(expContains.Code) || string.IsNullOrEmpty(expContains.System))
                    {
                        continue;
                    }

                    CodeSystem codeSystem = this.tdb.CodeSystems.SingleOrDefault(y => y.Oid == expContains.System);

                    if (codeSystem == null)
                    {
                        codeSystem = new CodeSystem()
                        {
                            Oid  = expContains.System,
                            Name = expContains.System
                        };
                        this.tdb.CodeSystems.Add(codeSystem);
                    }

                    ValueSetMember newMember = valueSet.Members.SingleOrDefault(y => y.CodeSystem == codeSystem && y.Code == expContains.Code);

                    if (newMember == null)
                    {
                        newMember = new ValueSetMember()
                        {
                            CodeSystem = codeSystem,
                            Code       = expContains.Code
                        }
                    }
                    ;

                    if (newMember.DisplayName != expContains.Display)
                    {
                        newMember.DisplayName = expContains.Display;
                    }

                    DateTime versionDateVal = DateTime.MinValue;
                    if (!DateTime.TryParse(fhirValueSet.Version, out versionDateVal))
                    {
                        DateTime.TryParse(fhirValueSet.Date, out versionDateVal);
                    }
                    DateTime?versionDate = versionDateVal != DateTime.MinValue ? (DateTime?)versionDateVal : null;

                    if (newMember.StatusDate != versionDate)
                    {
                        newMember.StatusDate = versionDate;
                    }

                    if (newMember.StatusDate != null && newMember.Status != "active")
                    {
                        newMember.Status = "active";
                    }

                    valueSet.Members.Add(newMember);
                }
            }

            return(valueSet);
        }
    }