private void PopulateCodeSystemMembers(ImportValueSet importValueSet, DateTime?versionDate, XmlNodeList contentNodes, ValueSet currentValueSet, string specialization = null)
 {
     foreach (XmlNode cContentNode in contentNodes)
     {
         string codeSystemOid = cContentNode.Attributes["codeSystem"].Value;
         PopulateCodeSystemMembers(importValueSet, versionDate, codeSystemOid, currentValueSet, specialization);
     }
 }
        public void SaveValueSet(IObjectRepository tdb, ImportValueSet valueSet)
        {
            ValueSet tdbValueSet = FindOrAddValueSet(tdb, valueSet);

            foreach (ImportValueSetMember cImportMember in valueSet.Members)
            {
                FindOrAddValueSetMember(tdb, tdbValueSet, cImportMember);
            }
        }
        private void PopulateCodeSystemMembers(ImportValueSet importValueSet, DateTime?versionDate, string codeSystemOid, ValueSet currentValueSet, string specialization = null, string relationshipType = null)
        {
            string  codeSystemXpath = string.Format("/mif:vocabularyModel/mif:codeSystem[@codeSystemId='{0}']", codeSystemOid);
            XmlNode codeSystemNode  = this.sourceDoc.SelectSingleNode(codeSystemXpath, this.nsManager);

            if (codeSystemNode == null)
            {
                return;
            }

            XmlNodeList conceptNodes = null;

            if (specialization == null)
            {
                conceptNodes = codeSystemNode.SelectNodes("mif:releasedVersion/mif:concept", this.nsManager);
            }
            else
            {
                conceptNodes = codeSystemNode.SelectNodes("mif:releasedVersion/mif:concept[mif:conceptRelationship[@relationshipName='Specializes'][mif:targetConcept/@code='" + specialization + "']]", this.nsManager);
            }

            foreach (XmlNode cConceptNode in conceptNodes)
            {
                V importMember = Activator.CreateInstance <V>();

                XmlNode codeNode      = cConceptNode.SelectSingleNode("mif:code", this.nsManager);
                XmlNode printNameNode = cConceptNode.SelectSingleNode("mif:printName", this.nsManager);

                if (codeNode == null || printNameNode == null)
                {
                    continue;
                }

                string code = codeNode.Attributes["code"].Value;

                importMember.Code           = code;
                importMember.CodeSystemName = codeSystemNode.Attributes["title"].Value;
                importMember.CodeSystemOid  = codeSystemOid;
                importMember.DisplayName    = printNameNode.Attributes["text"].Value;
                importMember.Status         = codeNode.Attributes["status"].Value;
                importMember.StatusDate     = versionDate;

                AddImportMember(importValueSet, versionDate, currentValueSet, importMember, true, relationshipType);
            }
        }
        private void AddImportMember(ImportValueSet importValueSet, DateTime?versionDate, ValueSet currentValueSet, ImportValueSetMember importValueSetMember, bool includeHead, string relationshipType)
        {
            if (includeHead)
            {
                ValueSetMember currentMember = currentValueSet != null?
                                               currentValueSet.Members.SingleOrDefault(y => y.Code == importValueSetMember.Code && y.CodeSystem.Oid == importValueSetMember.CodeSystemOid) :
                                                   null;

                importValueSetMember.ImportStatus = DetermineValueSetMemberStatus(importValueSetMember, currentMember);

                importValueSet.Members.Add(importValueSetMember);
            }

            if (!string.IsNullOrEmpty(relationshipType))
            {
                PopulateCodeSystemMembers(importValueSet, versionDate, importValueSetMember.CodeSystemOid, currentValueSet, importValueSetMember.Code, relationshipType == "TransitiveClosure" ? "TransitiveClosure" : null);
            }
        }
        private void PopulateMembers <X>(ImportValueSet importValueSet, XmlNode versionNode, ValueSet currentValueSet)
            where X : ImportValueSetMember
        {
            string includingValueSetName = versionNode.ParentNode.Attributes["name"].Value;

            if (IsAlreadyIncluded(importValueSet.Oid, includingValueSetName))
            {
                return;
            }

            AddIncludedValueSet(importValueSet.Oid, includingValueSetName);

            XmlNodeList contentUnionNodes = versionNode.SelectNodes("mif:content[not(mif:combinedContent)] | mif:content/mif:combinedContent/mif:unionWithContent", this.nsManager);

            DateTime parsedVersionDate;
            DateTime?versionDate = null;

            if (DateTime.TryParse(versionNode.Attributes["versionDate"].Value, out parsedVersionDate))
            {
                versionDate = parsedVersionDate;
            }

            PopulateContentMembers(
                importValueSet,
                versionDate,
                contentUnionNodes,
                currentValueSet);

            XmlNodeList codeSystemContentNodes = versionNode.SelectNodes(
                "mif:content[not(*) and @codeSystem]", this.nsManager);

            PopulateCodeSystemMembers(
                importValueSet,
                versionDate,
                codeSystemContentNodes,
                currentValueSet);

            if (importValueSet.Members.Count == 0)
            {
                string msg = string.Format("Could not find any members for the valueset '{0}' using current logic.", importValueSet.Oid);
                Log.For(this).Critical(msg);
            }
        }
        private void PopulateContentMembers(ImportValueSet importValueSet, DateTime?versionDate, XmlNodeList contentNodes, ValueSet currentValueSet)
        {
            foreach (XmlNode cContentNode in contentNodes)
            {
                string      codeSystemOid = cContentNode.Attributes["codeSystem"].Value;
                XmlNodeList codeNodes     = cContentNode.SelectNodes("mif:codeBasedContent", this.nsManager);

                foreach (XmlNode cCodeNode in codeNodes)
                {
                    string code             = cCodeNode.Attributes["code"].Value;
                    string relationshipType = null;
                    bool   includeHead      = cCodeNode.Attributes["includeHeadCode"] != null?bool.Parse(cCodeNode.Attributes["includeHeadCode"].Value) : true;

                    XmlNode includeRelatedCodesNode = cCodeNode.SelectSingleNode("mif:includeRelatedCodes[@relationshipName='Generalizes']", this.nsManager);

                    if (includeRelatedCodesNode != null)
                    {
                        relationshipType = includeRelatedCodesNode.Attributes["relationshipTraversal"] != null ? includeRelatedCodesNode.Attributes["relationshipTraversal"].Value : null;
                    }

                    V importMember = Activator.CreateInstance <V>();

                    importMember.CodeSystemOid = codeSystemOid;
                    importMember.Code          = code;
                    importMember.StatusDate    = versionDate;
                    importMember.Status        = "active";

                    PopulateConceptFields(importMember);

                    ValueSetMember currentMember = currentValueSet != null?
                                                   currentValueSet.Members.SingleOrDefault(y => y.Code == code && y.CodeSystem.Oid == codeSystemOid) :
                                                       null;

                    importMember.ImportStatus = DetermineValueSetMemberStatus(importMember, currentMember);

                    AddImportMember(importValueSet, versionDate, currentValueSet, importMember, includeHead, relationshipType);
                }
            }
        }
        protected string DetermineValueSetStatus(ImportValueSet importValueSet, ValueSet currentValueSet)
        {
            if (currentValueSet == null)
            {
                return("Add");
            }
            else
            {
                bool valueSetIsChanged =
                    importValueSet.Code != currentValueSet.Code ||
                    importValueSet.Description != currentValueSet.Description ||
                    importValueSet.Name != currentValueSet.Name ||
                    importValueSet.ImportSourceId != currentValueSet.ImportSourceId;

                if (valueSetIsChanged)
                {
                    return("Update");
                }
            }

            return("None");
        }
        private ValueSet FindOrAddValueSet(IObjectRepository tdb, ImportValueSet valueSet)
        {
            ValueSet foundValueSet = (from vs in tdb.ValueSets
                                      join vsi in tdb.ValueSetIdentifiers on vs.Id equals vsi.ValueSetId
                                      where vsi.Identifier.ToLower().Trim() == valueSet.Oid.ToLower().Trim()
                                      select vs)
                                     .Distinct()
                                     .FirstOrDefault();

            if (valueSet.ImportStatus == "None")
            {
                return(foundValueSet);
            }

            bool   changed = false;
            string name    = TruncateString(valueSet.Name, 254);
            string code    = TruncateString(valueSet.Code, 254);
            string oid     = TruncateString(valueSet.Oid, 254);

            if (foundValueSet == null)
            {
                foundValueSet = new ValueSet();
                tdb.ValueSets.Add(foundValueSet);
                changed = true;
            }

            if (foundValueSet.Code != code)
            {
                foundValueSet.Code = code;
                changed            = true;
            }

            if (this.PopulateIdentifier(foundValueSet, oid))
            {
                changed = true;
            }

            if (foundValueSet.Description != valueSet.Description)
            {
                foundValueSet.Description = valueSet.Description;
                changed = true;
            }

            if (foundValueSet.Name != name)
            {
                foundValueSet.Name = name;
                changed            = true;
            }

            if (foundValueSet.Source != valueSet.SourceUrl)
            {
                foundValueSet.Source = valueSet.SourceUrl;
                changed = true;
            }

            if (valueSet.ImportSource == "PHIN VADS")
            {
                if (foundValueSet.ImportSource.HasValue && foundValueSet.ImportSource != ValueSetImportSources.PHINVADS)
                {
                    throw new Exception("Cannot re-import this value set as it was imported from a different source.");
                }

                if (!foundValueSet.ImportSource.HasValue)
                {
                    foundValueSet.ImportSource = ValueSetImportSources.PHINVADS;
                    changed = true;
                }
            }
            else if (valueSet.ImportSource == "HL7 RIM/RoseTree")
            {
                if (foundValueSet.ImportSource.HasValue && foundValueSet.ImportSource != ValueSetImportSources.ROSETREE)
                {
                    throw new Exception("Cannot re-import this value set as it was imported from a different source.");
                }

                if (!foundValueSet.ImportSource.HasValue)
                {
                    foundValueSet.ImportSource = ValueSetImportSources.ROSETREE;
                    changed = true;
                }
            }

            if (foundValueSet.ImportSourceId != valueSet.ImportSourceId)
            {
                foundValueSet.ImportSourceId = valueSet.ImportSourceId;
                changed = true;
            }

            if (changed)
            {
                foundValueSet.LastUpdate = DateTime.Now;
            }

            return(foundValueSet);
        }