protected void ValidateArchetype(Archetype archetype) { this.Validate((AuthoredResource)archetype); Invariant(archetype.ArchetypeId != null, string.Format( CommonStrings.XMustNotBeNull, "Archetype.ArchetypeId")); this.Validate(archetype.ArchetypeId); Invariant(archetype.Ontology.HasTermCode(archetype.Definition.NodeId), AmValidationStrings.OntologyMissingDefinitionTerm); Invariant(archetype.Uid == null || !string.IsNullOrEmpty(archetype.Uid.Value), string.Format(CommonStrings.IfXIsNotNullMustBeEmpty, "Archetype.Uid")); if (archetype.Uid != null) this.Validate(archetype.Uid); Invariant(!string.IsNullOrEmpty(archetype.Concept), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "Archetype.Concept")); if (archetype.ParentArchetypeId != null) this.Validate(archetype.ParentArchetypeId); Invariant(archetype.Version() != null, string.Format(CommonStrings.XMustNotBeNull, "Archetype.Version()")); Invariant(archetype.Version() == archetype.ArchetypeId.VersionId, AmValidationStrings.ArchetypeVersionNotEqual); Invariant(archetype.OriginalLanguage!=null, string.Format(CommonStrings.XMustNotBeNull, "Archetype.OriginalLanguage")); this.Validate(archetype.OriginalLanguage); Invariant(archetype.Description != null, string.Format(CommonStrings.XMustNotBeNull, "Archetype.Description")); Invariant(archetype.Definition!= null, string.Format(CommonStrings.XMustNotBeNull, "Archetype.Definition")); this.Validate(archetype.Definition); Invariant(archetype.Ontology != null, "Archetype.Ontology must not be null."); this.Validate(archetype.Ontology); Invariant(!archetype.IsSpecialised() ^ (archetype.IsSpecialised() && archetype.SpecialisationDepth() > 0), AmValidationStrings.ArchetypeSpecialisationInvariantFail); Invariant(archetype.Invariants == null || !archetype.Invariants.IsEmpty(), string.Format(CommonStrings.IfXIsNotNullMustBeEmpty, "Archetype.Invariants")); if (archetype.Invariants != null) { foreach (Assertion eachInvariant in archetype.Invariants) this.Validate(eachInvariant); } }
public void ReadArchetype(XmlReader reader, Archetype archetype) { DesignByContract.Check.Require(reader != null, string.Format(CommonStrings.XMustNotBeNull, "reader")); DesignByContract.Check.Require(archetype != null, string.Format(CommonStrings.XMustNotBeNull, "archetype")); this.reader = reader; this.archetype = archetype; if (reader.NodeType == System.Xml.XmlNodeType.None) reader.MoveToContent(); reader.ReadStartElement(); reader.MoveToContent(); ((AuthoredResource)(archetype)).ReadXml(reader); if (reader.LocalName == "uid") { archetype.Uid = new HierObjectId(); archetype.Uid.ReadXml(reader); } if (reader.LocalName != "archetype_id") throw new InvalidXmlException("archetype_id", reader.LocalName); archetype.ArchetypeId = new ArchetypeId(); archetype.ArchetypeId.ReadXml(reader); if (reader.LocalName == "adl_version") { archetype.AdlVersion = reader.ReadElementContentAsString("adl_version", OpenEhrNamespace); reader.MoveToContent(); } if (reader.LocalName != "concept") throw new InvalidXmlException("concept", reader.LocalName); archetype.Concept = reader.ReadElementContentAsString("concept", OpenEhrNamespace); reader.MoveToContent(); if (reader.LocalName == "parent_archetype_id") { archetype.ParentArchetypeId = new ArchetypeId(); archetype.ParentArchetypeId.ReadXml(reader); } if (reader.LocalName != "definition") throw new InvalidXmlException("definition", reader.LocalName); archetype.Definition = new CComplexObject(); this.ReadXml(archetype.Definition); if (reader.LocalName == "invariants") { System.Collections.Generic.List<Assertion> invariantsList = new System.Collections.Generic.List<OpenEhr.AM.Archetype.Assertion.Assertion>(); do { Assertion assertion = new OpenEhr.AM.Archetype.Assertion.Assertion(); this.ReadXml(assertion); invariantsList.Add(assertion); } while (reader.LocalName == "invariants"); DesignByContract.Check.Assert(invariantsList.Count > 0, "invariantsList must not be empty."); archetype.Invariants = new AssumedTypes.Set<OpenEhr.AM.Archetype.Assertion.Assertion>(invariantsList); } if (reader.LocalName != "ontology") throw new ValidationException(string.Format(CommonStrings.ExpectedLocalNameIsXNotY, "ontology", reader.LocalName)); archetype.Ontology = new ArchetypeOntology(); this.ReadXml(archetype.Ontology); archetype.Ontology.ParentArchetype = archetype; archetype.Ontology.SpecialisationDepth = AM.Archetype.Ontology.ArcheytpeTermCodeTools.SpecialisationDepthFromCode(archetype.Definition.NodeId); DesignByContract.Check.Assert(reader.NodeType == System.Xml.XmlNodeType.EndElement, "Expected endElement"); reader.ReadEndElement(); reader.MoveToContent(); }
public static void Validate(Archetype archetype, ITerminologyService terminologyService) { AmValidator amValidator = new AmValidator(terminologyService); amValidator.ValidateArchetype(archetype); }
public void WriteArchetype(XmlWriter writer, Archetype archetype) { DesignByContract.Check.Require(writer != null, string.Format(CommonStrings.XMustNotBeNull, "writer")); DesignByContract.Check.Require(archetype.ArchetypeId != null, string.Format(CommonStrings.XMustNotBeNull, "archetype.ArchetypeId")); DesignByContract.Check.Require(!string.IsNullOrEmpty(archetype.Concept), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "archetype.Concept")); DesignByContract.Check.Require(archetype.Definition != null, string.Format(CommonStrings.XMustNotBeNull, "archetype.Definition")); DesignByContract.Check.Require(archetype.Ontology != null, string.Format(CommonStrings.XMustNotBeNull, "archetype.Ontology")); this.archetype = archetype; this.writer = writer; ((AuthoredResource)archetype).WriteXml(writer); if (archetype.Uid != null) { writer.WriteStartElement(UseOpenEhrPrefix(writer), "uid", OpenEhrNamespace); archetype.Uid.WriteXml(writer); writer.WriteEndElement(); } writer.WriteStartElement(UseOpenEhrPrefix(writer), "archetype_id", OpenEhrNamespace); archetype.ArchetypeId.WriteXml(writer); writer.WriteEndElement(); if (!string.IsNullOrEmpty(archetype.AdlVersion)) { writer.WriteStartElement(UseOpenEhrPrefix(writer), "adl_version", OpenEhrNamespace); writer.WriteString(archetype.AdlVersion); writer.WriteEndElement(); } writer.WriteElementString(UseOpenEhrPrefix(writer), "concept", OpenEhrNamespace, archetype.Concept); if (archetype.ParentArchetypeId != null) { writer.WriteStartElement(UseOpenEhrPrefix(writer), "parent_archetype_id", OpenEhrNamespace); archetype.ParentArchetypeId.WriteXml(writer); writer.WriteEndElement(); } writer.WriteStartElement(UseOpenEhrPrefix(writer), "definition", OpenEhrNamespace); this.WriteXml(archetype.Definition); writer.WriteEndElement(); if (archetype.Invariants != null) { foreach (Assertion assertion in archetype.Invariants) { writer.WriteStartElement(UseOpenEhrPrefix(writer), "invariants", OpenEhrNamespace); this.WriteXml(assertion); writer.WriteEndElement(); } } writer.WriteStartElement(UseOpenEhrPrefix(writer), "ontology", OpenEhrNamespace); this.WriteXml(archetype.Ontology); writer.WriteEndElement(); }