Exemplo n.º 1
0
        private void UpdateConstraintContainedTemplate(TemplateConstraint constraint, string containedTemplateOid)
        {
            // Find the contained template if one is specified
            if (!string.IsNullOrEmpty(containedTemplateOid))
            {
                if (containedTemplateOid.ToLower() == constraint.Template.Oid.ToLower())
                {
                    this.errors.Add(string.Format(
                                        "Template with oid \"{0}\" has a constraint which contains the same template. Constraint cannot be imported.",
                                        constraint.Template.Oid));
                    throw new Exception("Constraint has an error.");
                }

                TDBTemplate containedTemplate = FindOrBuildTemplate(containedTemplateOid);

                if (containedTemplate == null)
                {
                    this.errors.Add(string.Format(
                                        "Could not find contained template \"{0}\" for constraint with number \"{1}\" in template with oid \"{2}\"",
                                        containedTemplateOid,
                                        constraint.Number,
                                        constraint.Template.Oid));
                    throw new Exception("Constraint has an error.");
                }

                if (constraint.ContainedTemplate != containedTemplate)
                {
                    constraint.ContainedTemplate = containedTemplate;
                }
            }
        }
Exemplo n.º 2
0
        private void AddImportConstraint(TDBTemplate template, TDBTemplateConstraint parentConstraint, ImportConstraint importConstraint)
        {
            if (importConstraint.isVerbose)
            {
                return;
            }

            TDBTemplateConstraint constraint = null;

            if (importConstraint.numberSpecified)
            {
                constraint = template.ChildConstraints.SingleOrDefault(y => y.ParentConstraint == parentConstraint && y.Number == importConstraint.number);
            }
            else if (!string.IsNullOrEmpty(importConstraint.displayNumber))
            {
                constraint = template.ChildConstraints.SingleOrDefault(y => y.ParentConstraint == parentConstraint && y.DisplayNumber == importConstraint.displayNumber);
            }

            if (constraint == null)
            {
                constraint                  = new TDBTemplateConstraint();
                constraint.Number           = importConstraint.number;
                constraint.DisplayNumber    = importConstraint.displayNumber;
                constraint.Template         = template;
                constraint.ParentConstraint = parentConstraint;
                this.tdb.TemplateConstraints.Add(constraint);

                // TODO: Order the constraint? Or let the template re-order the constraints when it is edited?
            }

            this.UpdateConstraintProperties(constraint, importConstraint);

            this.UpdateConstraintContainedTemplate(constraint, importConstraint.ContainedTemplate);

            this.UpdateConstraintBinding(constraint, importConstraint);

            this.UpdateConstraintCodeSystem(constraint, importConstraint);

            this.UpdateConstraintCategories(constraint, importConstraint);

            this.UpdateConstraintSamples(constraint, importConstraint);

            // Add each of the constraint's child constraints
            if (importConstraint.Constraint != null)
            {
                importConstraint.Constraint.ToList().ForEach(y => AddImportConstraint(template, constraint, y));
            }
        }
Exemplo n.º 3
0
        private TDBTemplate FindOrBuildTemplate(string identifier)
        {
            TDBTemplate template = this.tdb.Templates.SingleOrDefaultInclAdded(y => y.Oid.ToLower() == identifier.ToLower());

            // If we didn't find the implied template in the database, check our import
            if (template == null)
            {
                // Try to find if the template has already been built in the import
                template = this.importedTemplates.SingleOrDefault(y => y.Oid.ToLower() == identifier.ToLower());

                // If it hasn't been built yet, then find the template in the import and build it
                if (template == null)
                {
                    ImportTemplate importImpliedTemplate = this.importTemplates.SingleOrDefault(y => y.identifier.ToLower() == identifier.ToLower());

                    if (importImpliedTemplate != null)
                    {
                        template = AddImportTemplate(importImpliedTemplate);
                    }
                }
            }

            return(template);
        }
Exemplo n.º 4
0
        public static void SetupData(TestContext context)
        {
            // Setup mockRepo1
            StructureDefinitionControllerTest.mockRepo1 = new MockObjectRepository();
            StructureDefinitionControllerTest.mockRepo1.InitializeFHIR2Repository();
            StructureDefinitionControllerTest.mockRepo1.InitializeLCGAndLogin();

            var ig = StructureDefinitionControllerTest.mockRepo1.FindOrCreateImplementationGuide(Constants.IGTypeNames.FHIR_DSTU2, "Test IG");

            StructureDefinitionControllerTest.profile1 = StructureDefinitionControllerTest.mockRepo1.CreateTemplate("http://test.com/fhir/test", "Composition", "Test Composition", ig, "Composition", "Composition");
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()
            {
                Identifier = "http://test.com/extension",
                Type       = "String",
                Value      = "Test string extension value"
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()
            {
                Identifier = "http://test2.com/extension",
                Type       = "Date",
                Value      = "2016234234234" // Invalid date format, but should still be parsed by FHIR library
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()
            {
                Identifier = "http://test3.com/extension",
                Type       = "Coding",
                Value      = "xyz-123|display|urn:oid:2.16.113"
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()
            {
                Identifier = "http://test4.com/extension",
                Type       = "CodeableConcept",
                Value      = "xyz-123|display|urn:oid:2.16.113"
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()         // Extension has invalid value... It is skipped
            {
                Identifier = "http://test5.com/extension",
                Type       = "CodeableConcept",
                Value      = "test"
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()         // Extension has invalid value... It is skipped
            {
                Identifier = "http://test6.com/extension",
                Type       = "Boolean",
                Value      = "test"
            });
            StructureDefinitionControllerTest.profile1.Extensions.Add(new Trifolia.DB.TemplateExtension()         // Extension has invalid value... It is skipped
            {
                Identifier = "http://test7.com/extension",
                Type       = "Integer",
                Value      = "test"
            });

            // Setup mockRepo2 for DSTU2_TestGetTemplates()
            StructureDefinitionControllerTest.mockRepo2 = new MockObjectRepository();
            StructureDefinitionControllerTest.mockRepo2.InitializeFHIR2Repository();
            StructureDefinitionControllerTest.mockRepo2.InitializeLCG();

            var ig2 = StructureDefinitionControllerTest.mockRepo2.FindOrCreateImplementationGuide(Constants.IGTypeNames.FHIR_DSTU2, "Test IG");

            StructureDefinitionControllerTest.mockRepo2.CreateTemplate("http://test.com/fhir/test", "Composition", "Test Composition", ig2, "Composition", "Composition");

            // Setup strucDef's
            var dafPatientJson = Helper.GetSampleContents("Trifolia.Test.DocSamples.daf-patient_struc_def.json");

            StructureDefinitionControllerTest.dafPatientStrucDef1     = (StructureDefinition)FhirParser.ParseResourceFromJson(dafPatientJson);
            StructureDefinitionControllerTest.dafPatientStrucDef1.Url = "http://hl7.org/fhir/StructureDefinition/daf-patient1";
            StructureDefinitionControllerTest.dafPatientStrucDef2     = (StructureDefinition)FhirParser.ParseResourceFromJson(dafPatientJson);
            StructureDefinitionControllerTest.dafPatientStrucDef2.Id  = "daf-patient2";
            StructureDefinitionControllerTest.dafPatientStrucDef2.Url = "http://hl7.org/fhir/StructureDefinition/daf-patient2";
        }
Exemplo n.º 5
0
        private TDBTemplate AddImportTemplate(Trifolia.Shared.ImportExport.Model.TrifoliaTemplate importTemplate)
        {
            var alreadyBuildTemplate = this.importedTemplates.SingleOrDefault(y => y.Oid == importTemplate.identifier);

            if (alreadyBuildTemplate != null)
            {
                return(alreadyBuildTemplate);
            }

            TDBTemplate foundTemplate = this.tdb.Templates.SingleOrDefault(y => y.Oid == importTemplate.identifier);

            if (string.IsNullOrEmpty(importTemplate.identifier) || importTemplate.identifier.Length > 255)
            {
                errors.Add("Template OID is not specified.");
                return(null);
            }

            if (string.IsNullOrEmpty(importTemplate.title) || importTemplate.title.Length > 255)
            {
                errors.Add("Template name is not specified.");
                return(null);
            }

            var template = this.tdb.Templates.SingleOrDefaultInclAdded(y => y.Oid.ToLower() == importTemplate.identifier.ToLower());

            if (!shouldUpdate && template != null)
            {
                errors.Add("Template already exists and should not be updating");
                return(template);
            }

            if (template == null)
            {
                template     = new TDBTemplate();
                template.Oid = importTemplate.identifier;

                if (!template.IsIdentifierII() && !template.IsIdentifierOID() && !template.IsIdentifierURL())
                {
                    errors.Add("Template identifier " + template.Oid + " is not properly formatted. Must be urn:hl7ii:XXX:YYY or urn:oid:XXX or http(s)://XXX");
                    return(null);
                }

                if (this.DefaultAuthorUser != null)
                {
                    template.Author = this.DefaultAuthorUser;
                }
                else
                {
                    template.Author = CheckPoint.Instance.GetUser(this.tdb);
                }

                this.tdb.Templates.Add(template);
            }

            // Find implementation guide type
            ImplementationGuideType igType = this.tdb.ImplementationGuideTypes.SingleOrDefault(y => y.Name.ToLower() == importTemplate.implementationGuideType.ToLower());

            if (igType == null)
            {
                this.errors.Add(string.Format(
                                    "Could not find implementation guide type \"{0}\" for template with identifier \"{1}\"",
                                    importTemplate.implementationGuideType,
                                    importTemplate.identifier));
                return(null);
            }

            template.ImplementationGuideType = igType;

            if (importTemplate.ImplementationGuide != null && !string.IsNullOrEmpty(importTemplate.ImplementationGuide.name))
            {
                ImplementationGuide ig = this.tdb.ImplementationGuides.SingleOrDefaultInclAdded(y =>
                                                                                                y.Name.ToLower() == importTemplate.ImplementationGuide.name.ToLower() &&
                                                                                                (
                                                                                                    (y.Version == null && importTemplate.ImplementationGuide.version == 1) ||
                                                                                                    (y.Version != null && y.Version.Value == importTemplate.ImplementationGuide.version)
                                                                                                ));

                if (ig == null)
                {
                    this.errors.Add(string.Format(
                                        "Could not find implementation guide \"{0}\" for template with identifier \"{1}\"",
                                        importTemplate.ImplementationGuide.name,
                                        importTemplate.identifier));
                    return(null);
                }
                else if (ig.ImplementationGuideType != igType)
                {
                    this.errors.Add(string.Format(
                                        "The implementation guide type for the implementation guide \"{0}\" is not the same as the import.",
                                        importTemplate.ImplementationGuide.name));
                    return(null);
                }

                if (template.OwningImplementationGuide != ig)
                {
                    template.OwningImplementationGuide = ig;
                }

                if (!ig.ChildTemplates.Contains(template))
                {
                    ig.ChildTemplates.Add(template);
                }
            }
            else if (this.DefaultImplementationGuide != null)
            {
                template.OwningImplementationGuide = this.DefaultImplementationGuide;
            }
            else
            {
                errors.Add("No implementation guide specified for template/profile: " + importTemplate.identifier);
                return(null);
            }

            // Find the template type
            TemplateType templateType = igType.TemplateTypes.SingleOrDefault(y => y.Name.ToLower() == importTemplate.templateType.ToLower());

            if (templateType == null)
            {
                this.errors.Add(string.Format(
                                    "Could not find template type \"{0}\" for template with identifier \"{1}\"",
                                    importTemplate.templateType,
                                    importTemplate.identifier));
                return(null);
            }
            else if (template.TemplateType != templateType)
            {
                template.TemplateType = templateType;
            }

            // Find or build the implied template
            if (!string.IsNullOrEmpty(importTemplate.impliedTemplateOid))
            {
                if (importTemplate.impliedTemplateOid.ToLower() == importTemplate.identifier.ToLower())
                {
                    this.errors.Add(string.Format(
                                        "Template with identifier \"{0}\" implies itself. Cannot import template.",
                                        importTemplate.identifier));
                    return(null);
                }

                TDBTemplate impliedTemplate = FindOrBuildTemplate(importTemplate.impliedTemplateOid);

                // If we didn't find the template in the database or in the import, create an error for the template and don't add it
                if (impliedTemplate == null)
                {
                    this.errors.Add(string.Format(
                                        "Couldn't find implied template \"{0}\" in either the system or the import, for template with identifier \"{1}\"",
                                        importTemplate.impliedTemplateOid,
                                        importTemplate.identifier));
                    return(null);
                }
                else
                {
                    if (template.ImpliedTemplate != impliedTemplate)
                    {
                        template.ImpliedTemplate = impliedTemplate;
                    }

                    if (!impliedTemplate.ImplyingTemplates.Contains(template))
                    {
                        impliedTemplate.ImplyingTemplates.Add(template);
                    }
                }
            }

            this.UpdateTemplateProperties(template, importTemplate);

            this.UpdateTemplateSamples(template, importTemplate);

            // Extensions
            foreach (var existingExtension in template.Extensions.ToList())
            {
                if (importTemplate.Extension.Count(y => y.identifier == existingExtension.Identifier) == 0)
                {
                    this.tdb.TemplateExtensions.Remove(existingExtension);
                }
            }

            foreach (var importExtension in importTemplate.Extension)
            {
                var foundExtension = template.Extensions.SingleOrDefault(y => y.Identifier == importExtension.identifier);

                if (foundExtension == null)
                {
                    foundExtension = new TemplateExtension();
                    template.Extensions.Add(foundExtension);
                }

                foundExtension.Type  = importExtension.type;
                foundExtension.Value = importExtension.value;
            }

            try
            {
                // Add each of the template's constraints
                if (importTemplate.Constraint != null)
                {
                    importTemplate.Constraint.ToList().ForEach(y => AddImportConstraint(template, null, y));
                }
            }
            catch (Exception ex)
            {
                Log.For(this).Error("Error importing constraints for template.", ex);
                return(null);
            }

            // If the object is changed, make sure the user has permissions to the implementation guide
            if (this.tdb is TrifoliaDatabase)
            {
                var dataSource    = this.tdb as TrifoliaDatabase;
                var templateState = dataSource.Entry(template).State;

                if (templateState == System.Data.Entity.EntityState.Unchanged)
                {
                    var constraintStates = (from c in template.ChildConstraints
                                            where dataSource.Entry(c).State != System.Data.Entity.EntityState.Unchanged
                                            select c);
                    var constraintSampleStates = (from c in template.ChildConstraints
                                                  join cs in this.tdb.TemplateConstraintSamples on c equals cs.Constraint
                                                  where dataSource.Entry(cs).State != System.Data.Entity.EntityState.Unchanged
                                                  select cs);
                    var templateSampleStates = (from s in template.TemplateSamples
                                                where dataSource.Entry(s).State != System.Data.Entity.EntityState.Unchanged
                                                select s);

                    if (constraintStates.Count() > 0 || constraintSampleStates.Count() > 0 || templateSampleStates.Count() > 0)
                    {
                        templateState = System.Data.Entity.EntityState.Modified;
                    }
                }

                if (templateState != System.Data.Entity.EntityState.Unchanged && !CheckPoint.Instance.GrantEditImplementationGuide(template.OwningImplementationGuide.Id) && !CheckPoint.Instance.IsDataAdmin)
                {
                    this.Errors.Add("You do not have permission to modify template \"" + template.Name + "\" with identifier " + template.Oid);
                    return(null);
                }
            }

            this.importedTemplates.Add(template);

            return(template);
        }