<commit_before>﻿extern alias fhir_stu3;
using fhir_stu3.Hl7.Fhir.Model;
using fhir_stu3.Hl7.Fhir.Serialization;
using fhir_stu3.Hl7.Fhir.Specification.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Trifolia.Config;
using Trifolia.DB;
using Trifolia.Generation.IG.ConstraintGeneration;
using Trifolia.Logging;
using Trifolia.Shared;
using Trifolia.Shared.FHIR;
using Trifolia.Shared.FHIR.Profiles.STU3;
using ImplementationGuide = Trifolia.DB.ImplementationGuide;
using StructureDefinition = fhir_stu3.Hl7.Fhir.Model.StructureDefinition;
using SummaryType = fhir_stu3.Hl7.Fhir.Rest.SummaryType;

namespace Trifolia.Import.FHIR.STU3
{
    public class StructureDefinitionImporter
    {
        private IObjectRepository tdb;
        private string scheme;
        private string authority;
        private Dictionary<ImplementationGuide, IGSettingsManager> allIgSettings = new Dictionary<ImplementationGuide, IGSettingsManager>();
        private Dictionary<string, StructureDefinition> baseProfiles = new Dictionary<string, StructureDefinition>();
        private ImplementationGuideType implementationGuideType;
        private Bundle profileBundle;

        public StructureDefinitionImporter(IObjectRepository tdb, string scheme, string authority)
        {
            this.tdb = tdb;
            this.scheme = scheme;
            this.authority = authority;
            this.implementationGuideType = DSTU2Helper.GetImplementationGuideType(this.tdb, true);
            this.profileBundle = ProfileHelper.GetProfileBundle();
        }

        public Template Convert(StructureDefinition strucDef, Template template = null)
        {
            if (string.IsNullOrEmpty(strucDef.Type))
                throw new Exception("StructureDefinition.type is required");

            string strucDefDescription = strucDef.Description != null ? strucDef.Description.Value : null;

            if (template == null)
            {
                ImplementationGuide unassignedImplementationGuide = this.tdb.ImplementationGuides.SingleOrDefault(y =>
                    y.Name == DSTU2Helper.DEFAULT_IG_NAME &&
                    y.ImplementationGuideTypeId == this.implementationGuideType.Id);

                if (unassignedImplementationGuide == null)
                {
                    unassignedImplementationGuide = new ImplementationGuide()
                    {
                        Name = DSTU2Helper.DEFAULT_IG_NAME,
                        ImplementationGuideType = this.implementationGuideType,
                        Organization = this.tdb.Organizations.Single(y => y.Name == DSTU2Helper.DEFAULT_ORG_NAME)
                    };
                    this.tdb.ImplementationGuides.AddObject(unassignedImplementationGuide);
                }

                template = new Template()
                {
                    OwningImplementationGuide = unassignedImplementationGuide,
                    ImplementationGuideType = this.implementationGuideType,
                    Author = this.tdb.Users.Single(y => y.UserName == DSTU2Helper.DEFAULT_USER_NAME && y.Organization.Name == DSTU2Helper.DEFAULT_ORG_NAME),
                    Organization = this.tdb.Organizations.Single(y => y.Name == DSTU2Helper.DEFAULT_ORG_NAME),
                    IsOpen = true
                };
            }

            // Name
            if (template.Name != strucDef.Name)
                template.Name = strucDef.Name;

            // Descrition
            if (template.Description != strucDefDescription)
                template.Description = strucDefDescription;

            // Identifier -> Oid
            string identifier = strucDef.Url;

            if (string.IsNullOrEmpty(identifier))
                identifier = string.Format(STU3Helper.STRUCDEF_NEW_IDENTIFIER_FORMAT, Guid.NewGuid());

            if (template.Oid != identifier)
                template.Oid = identifier;

            string fhirBaseType = strucDef.Type != null ? strucDef.Type.ToString() : string.Empty;

            // ConstrainedType -> Template Type
            TemplateType templateType = this.tdb.TemplateTypes.SingleOrDefault(y =>
                y.ImplementationGuideTypeId == this.implementationGuideType.Id &&
                y.RootContextType == fhirBaseType);

            if (templateType == null)
                throw new Exception("Could not find Template Type for " + strucDef.Type);
<commit_msg>Fixing issue with structure definition importer using the wrong FHIR version of a helper class<commit_after>﻿extern alias fhir_stu3;
using fhir_stu3.Hl7.Fhir.Model;
using fhir_stu3.Hl7.Fhir.Serialization;
using fhir_stu3.Hl7.Fhir.Specification.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Trifolia.Config;
using Trifolia.DB;
using Trifolia.Generation.IG.ConstraintGeneration;
using Trifolia.Logging;
using Trifolia.Shared;
using Trifolia.Shared.FHIR;
using Trifolia.Shared.FHIR.Profiles.STU3;
using ImplementationGuide = Trifolia.DB.ImplementationGuide;
using StructureDefinition = fhir_stu3.Hl7.Fhir.Model.StructureDefinition;
using SummaryType = fhir_stu3.Hl7.Fhir.Rest.SummaryType;

namespace Trifolia.Import.FHIR.STU3
{
    public class StructureDefinitionImporter
    {
        private IObjectRepository tdb;
        private string scheme;
        private string authority;
        private Dictionary<ImplementationGuide, IGSettingsManager> allIgSettings = new Dictionary<ImplementationGuide, IGSettingsManager>();
        private Dictionary<string, StructureDefinition> baseProfiles = new Dictionary<string, StructureDefinition>();
        private ImplementationGuideType implementationGuideType;
        private Bundle profileBundle;

        public StructureDefinitionImporter(IObjectRepository tdb, string scheme, string authority)
        {
            this.tdb = tdb;
            this.scheme = scheme;
            this.authority = authority;
            this.implementationGuideType = STU3Helper.GetImplementationGuideType(this.tdb, true);
            this.profileBundle = ProfileHelper.GetProfileBundle();
        }

        public Template Convert(StructureDefinition strucDef, Template template = null)
        {
            if (string.IsNullOrEmpty(strucDef.Type))
                throw new Exception("StructureDefinition.type is required");

            string strucDefDescription = strucDef.Description != null ? strucDef.Description.Value : null;

            if (template == null)
            {
                ImplementationGuide unassignedImplementationGuide = this.tdb.ImplementationGuides.SingleOrDefault(y =>
                    y.Name == STU3Helper.DEFAULT_IG_NAME &&
                    y.ImplementationGuideTypeId == this.implementationGuideType.Id);

                if (unassignedImplementationGuide == null)
                {
                    unassignedImplementationGuide = new ImplementationGuide()
                    {
                        Name = STU3Helper.DEFAULT_IG_NAME,
                        ImplementationGuideType = this.implementationGuideType,
                        Organization = this.tdb.Organizations.Single(y => y.Name == STU3Helper.DEFAULT_ORG_NAME)
                    };
                    this.tdb.ImplementationGuides.AddObject(unassignedImplementationGuide);
                }

                template = new Template()
                {
                    OwningImplementationGuide = unassignedImplementationGuide,
                    ImplementationGuideType = this.implementationGuideType,
                    Author = this.tdb.Users.Single(y => y.UserName == STU3Helper.DEFAULT_USER_NAME && y.Organization.Name == STU3Helper.DEFAULT_ORG_NAME),
                    Organization = this.tdb.Organizations.Single(y => y.Name == STU3Helper.DEFAULT_ORG_NAME),
                    IsOpen = true
                };
            }

            // Name
            if (template.Name != strucDef.Name)
                template.Name = strucDef.Name;

            // Descrition
            if (template.Description != strucDefDescription)
                template.Description = strucDefDescription;

            // Identifier -> Oid
            string identifier = strucDef.Url;

            if (string.IsNullOrEmpty(identifier))
                identifier = string.Format(STU3Helper.STRUCDEF_NEW_IDENTIFIER_FORMAT, Guid.NewGuid());

            if (template.Oid != identifier)
                template.Oid = identifier;

            string fhirBaseType = strucDef.Type != null ? strucDef.Type.ToString() : string.Empty;

            // ConstrainedType -> Template Type
            TemplateType templateType = this.tdb.TemplateTypes.SingleOrDefault(y =>
                y.ImplementationGuideTypeId == this.implementationGuideType.Id &&
                y.RootContextType == fhirBaseType);

            if (templateType == null)
                throw new Exception("Could not find Template Type for " + strucDef.Type);

{'fraction_non_alphanumeric': 0.07837898018258739, 'fraction_numerical': 0.004676018704074816, 'mean_word_length': 2.3682039745031873, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '527734', 'n_tokens_mistral': 2422, 'n_tokens_neox': 2258, 'n_words': 436}