Пример #1
0
        private void UpdateTemplateSamples(Template template, ImportTemplate importTemplate)
        {
            var foundSamples = new List <TemplateSample>();

            if (importTemplate.Sample != null && importTemplate.Sample.Count > 0)
            {
                foreach (var importSample in importTemplate.Sample)
                {
                    if (string.IsNullOrEmpty(importSample.name))
                    {
                        errors.Add("Template sample does not have a name");
                        continue;
                    }

                    // Look for a match sample by both name and sample text. Maybe they changed the name, or maybe the change the sample text.
                    // If they only changed one, then we should update the existing entry.
                    // If they updated both, we won't find a match, and should add a new one. All non-matched samples will be deleted
                    var foundSample = template.TemplateSamples.SingleOrDefault(y => y.Name.ToLower() == importSample.name.ToLower());

                    if (foundSample == null && importSample.Value != null)
                    {
                        foundSample = template.TemplateSamples.SingleOrDefault(y => y.XmlSample != null && y.XmlSample.ToLower().Replace("\r", "").Replace("\n", "") == importSample.Value.ToLower().Replace("\r", "").Replace("\n", ""));
                    }

                    if (foundSample == null)
                    {
                        foundSample = new TemplateSample()
                        {
                            Template  = template,
                            Name      = importSample.name,
                            XmlSample = importSample.Value
                        };
                        this.tdb.TemplateSamples.Add(foundSample);
                    }
                    else
                    {
                        if (foundSample.Name != importSample.name)
                        {
                            foundSample.Name = importSample.name;
                        }

                        if (AreStringsDifferent(foundSample.XmlSample, importSample.Value))
                        {
                            foundSample.XmlSample = importSample.Value;
                        }
                    }

                    foundSamples.Add(foundSample);
                }
            }

            var deleteSamples = (from ts in template.TemplateSamples
                                 where !foundSamples.Contains(ts)
                                 select ts).ToList();

            foreach (var deleteSample in deleteSamples)
            {
                this.tdb.TemplateSamples.Remove(deleteSample);
            }
        }
Пример #2
0
        public void ExportTemplatesModelTest1()
        {
            MockObjectRepository tdb = new MockObjectRepository();

            tdb.InitializeCDARepository();

            Organization        org        = tdb.FindOrAddOrganization("LCG Test");
            ImplementationGuide ig         = tdb.FindOrAddImplementationGuide(tdb.FindImplementationGuideType(MockObjectRepository.DEFAULT_CDA_IG_TYPE_NAME), "Test Implementation Guide");
            IGSettingsManager   igSettings = new IGSettingsManager(tdb, ig.Id);

            Template template = tdb.GenerateTemplate("1.2.3.4", "Document", "Test Template", ig, "observation", "Observation", "Test Description", "Test Notes", org);
            var      tc1      = tdb.GenerateConstraint(template, null, null, "entryRelationship", "SHALL", "1..1");
            var      tc2      = tdb.GenerateConstraint(template, tc1, null, "observation", "SHOULD", "0..1");
            var      tc3      = tdb.GenerateConstraint(template, tc2, null, "value", "SHALL", "1..1", "CD", value: "4321", displayName: "Test");

            tc3.Description = "Test description";
            tc3.Label       = "Test Label";
            var tc4 = tdb.GeneratePrimitive(template, null, "SHALL", "This is a test");

            ExportTemplate export = template.Export(tdb, igSettings);

            Assert.IsNotNull(export);
            Assert.AreEqual(template.Oid, export.identifier);
            Assert.AreEqual(template.Name, export.title);
            Assert.AreEqual(template.TemplateType.Name, export.templateType);
            Assert.AreEqual(template.PrimaryContext, export.context);
            Assert.AreEqual(template.PrimaryContextType, export.contextType);
            Assert.AreEqual(template.Description, export.Description);
            Assert.AreEqual(template.Notes, export.Notes);
            Assert.AreEqual(template.OrganizationName, export.organizationName);

            // Assert constraints
            Assert.IsNotNull(export.Constraint);
            Assert.AreEqual(2, export.Constraint.Count);

            // tc1
            Assert.AreEqual(tc1.Context, export.Constraint[0].context);
            Assert.AreEqual(ExportConformanceTypes.SHALL, export.Constraint[0].conformance);
            Assert.AreEqual(tc1.Cardinality, export.Constraint[0].cardinality);

            // tc4
            Assert.IsNull(export.Constraint[1].context);
            Assert.AreEqual(true, export.Constraint[1].isPrimitive);
            Assert.AreEqual(tc4.PrimitiveText, export.Constraint[1].NarrativeText);

            // tc2
            Assert.AreEqual(1, export.Constraint[0].Constraint.Count);
            Assert.AreEqual(tc2.Context, export.Constraint[0].Constraint[0].context);
            Assert.AreEqual(ExportConformanceTypes.SHOULD, export.Constraint[0].Constraint[0].conformance);
            Assert.AreEqual(tc2.Cardinality, export.Constraint[0].Constraint[0].cardinality);

            // tc3
            Assert.AreEqual(1, export.Constraint[0].Constraint[0].Constraint.Count);
            Assert.AreEqual(tc3.Context, export.Constraint[0].Constraint[0].Constraint[0].context);
            Assert.AreEqual(ExportConformanceTypes.SHALL, export.Constraint[0].Constraint[0].Constraint[0].conformance);
            Assert.AreEqual(tc3.Cardinality, export.Constraint[0].Constraint[0].Constraint[0].cardinality);
            Assert.AreEqual(tc3.Description, export.Constraint[0].Constraint[0].Constraint[0].Description);
            Assert.AreEqual(tc3.Label, export.Constraint[0].Constraint[0].Constraint[0].Label);
            Assert.IsFalse(string.IsNullOrEmpty(export.Constraint[0].Constraint[0].Constraint[0].Description));
        }
Пример #3
0
        public void ExportTemplatesModelTest2()
        {
            MockObjectRepository tdb = new MockObjectRepository();

            tdb.InitializeCDARepository();

            Organization        org        = tdb.FindOrAddOrganization("LCG Test");
            ImplementationGuide ig         = tdb.FindOrAddImplementationGuide(tdb.FindImplementationGuideType(MockObjectRepository.DEFAULT_CDA_IG_TYPE_NAME), "Test Implementation Guide");
            IGSettingsManager   igSettings = new IGSettingsManager(tdb, ig.Id);

            Template template = tdb.GenerateTemplate("1.2.3.4", "Document", "Test Template", ig, "observation", "Observation", "Test Description", "Test Notes");

            ExportTemplate export = template.Export(tdb, igSettings);

            Assert.IsNotNull(export);
            Assert.AreEqual(template.Oid, export.identifier);
            Assert.AreEqual(template.Name, export.title);
            Assert.AreEqual(template.TemplateType.Name, export.templateType);
            Assert.AreEqual(template.PrimaryContext, export.context);
            Assert.AreEqual(template.PrimaryContextType, export.contextType);
            Assert.AreEqual(template.Description, export.Description);
            Assert.AreEqual(template.Notes, export.Notes);
            Assert.IsNull(export.organizationName);
            Assert.AreEqual(template.OwningImplementationGuide.Name, export.ImplementationGuide.name);
        }
Пример #4
0
        private bool ImportConstraintExists(ImportTemplate importTemplate, int number)
        {
            foreach (var importConstraint in importTemplate.Constraint)
            {
                if (ImportConstraintExists(importConstraint, number))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #5
0
        private void UpdateTemplateProperties(Template template, ImportTemplate importTemplate)
        {
            if (template.Name != importTemplate.title)
            {
                template.Name = importTemplate.title;
            }

            if (template.Bookmark != importTemplate.bookmark)
            {
                template.Bookmark = importTemplate.bookmark;
            }

            if (template.IsOpen != importTemplate.isOpen)
            {
                template.IsOpen = importTemplate.isOpen;
            }

            if (AreStringsDifferent(template.Notes, importTemplate.Notes))
            {
                template.Notes = importTemplate.Notes;
            }

            if (AreStringsDifferent(template.Description, importTemplate.Description))
            {
                template.Description = importTemplate.Description;
            }

            if (template.PrimaryContext != importTemplate.context)
            {
                template.PrimaryContext = importTemplate.context;
            }

            if (template.PrimaryContextType != importTemplate.contextType)
            {
                template.PrimaryContextType = importTemplate.contextType;
            }
        }
Пример #6
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);
        }
Пример #7
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);
        }