public override bool ValidValue(object aValue) { Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue")); CComplexObject rootDefinition = AmFactory.GetRootDefinition(this); CObject cObjAtTargetPath = Archetype.GetCObjectAtTargetPath(rootDefinition, this.TargetPath); return(cObjAtTargetPath.ValidValue(aValue)); }
protected override System.Collections.Generic.List <string> GetPhysicalPaths() { CComplexObject rootDefinition = AmFactory.GetRootDefinition(this); CObject cObjAtTargetPath = Archetype.GetCObjectAtTargetPath(rootDefinition, this.TargetPath); if (cObjAtTargetPath == null) { throw new ApplicationException(string.Format( AmValidationStrings.NoNodeMatchAtPath, this.TargetPath)); } return(cObjAtTargetPath.PhysicalPaths); }
private bool IsOccurrencesValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "dataChildren")); bool result = true; System.Collections.Generic.List <MatchingItems> matches = new System.Collections.Generic.List <MatchingItems>(); AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; foreach (CObject constraint in Children) { if (constraint.Occurrences.Lower > 0 || !constraint.Occurrences.UpperUnbounded) { matches.Add(new MatchingItems(dataChildren, constraint)); } } } finally { ValidationContext.AcceptError = previousErrorDelegate; } foreach (MatchingItems match in matches) { match.RemoveItemsAlreadyMatchedByNameAndNodeId(matches); CObject constraint = match.Constraint; int lower = match.Lower; long upper = match.Upper; int actual = match.Count; if (actual < lower) { result = false; ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.NotEnoughOccurrences, constraint.NodeId, lower, actual)); } if (actual > upper) { result = false; ValidationContext.AcceptValidationError(constraint, string.Format(AmValidationStrings.TooManyOccurrences, constraint.NodeId, upper, actual)); } } return(result); }
public MatchingItems(AssumedTypes.IList dataChildren, CObject constraint) : base() { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "dataChildren")); Check.Require(constraint != null, string.Format(CommonStrings.XMustNotBeNull, "constraint")); Constraint = constraint; NameAttribute = NameAttributeConstraint(constraint); Lower = constraint.Occurrences.Lower; Upper = constraint.Occurrences.UpperUnbounded ? long.MaxValue : constraint.Occurrences.Upper; CArchetypeRoot archetypeRoot = constraint as CArchetypeRoot; NodeId = archetypeRoot != null ? archetypeRoot.ArchetypeId.Value : constraint.NodeId; if (!(constraint is ArchetypeSlot)) { OpenEhr.AssumedTypes.Impl.ILocatableList locatableItems = dataChildren as OpenEhr.AssumedTypes.Impl.ILocatableList; if (locatableItems != null) { Check.Assert(!string.IsNullOrEmpty(NodeId), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "NodeId")); if (locatableItems.Contains(NodeId)) { foreach (OpenEhr.RM.Common.Archetyped.Impl.Locatable locatable in (System.Collections.IEnumerable)locatableItems[NodeId]) { if (NameAttribute == null || NameAttribute.ValidValue(locatable.Name)) { Add(locatable); } } } } else { foreach (object item in dataChildren) { IRmType rmType = item as IRmType; if (rmType != null && constraint.IsSameRmType(rmType)) { Add(item); } } } } Check.Ensure(Constraint == constraint); }
public static string LocalTermDefText(string codeString, CObject cObject) { Check.Require(!string.IsNullOrEmpty(codeString), "codeString must not be null or empty."); Check.Require(cObject != null, "cObject must not be null"); CArchetypeRoot cArchetypeRoot = GetCArchetypeRoot(cObject); Check.Assert(cArchetypeRoot.TermDefinitions.HasKey(codeString)); string termDefText = cArchetypeRoot.TermDefinitions.Item(codeString).Items.Item("text"); Check.Ensure(!string.IsNullOrEmpty(termDefText)); return termDefText; }
internal static bool ValidateCObject(CObject cObject, ITerminologyService terminologyService) { AmValidator amValidator = new AmValidator(terminologyService); try { amValidator.Validate(cObject); } catch (Exception ex) { if (ex.GetType() == typeof(RmInvariantException)) return false; else throw ex; } return true; }
internal static CArchetypeRoot GetCArchetypeRoot(CObject cObject) { Check.Require(cObject != null, "cObject must not be null"); CObject parentObject = cObject; CArchetypeRoot cArchetypeRoot = null; while (parentObject != null && (cArchetypeRoot = parentObject as CArchetypeRoot) == null) { CAttribute cattribute = parentObject.Parent; Check.Assert(cattribute != null, "cattribute must not be null"); parentObject = cattribute.parent; } if (cArchetypeRoot == null) throw new ApplicationException("Operational template must contain CArchetypeRoot"); return cArchetypeRoot; }
public static bool HasNameAttributeConstraint(CObject objectConstraint, DvText name, bool requireNameConstraint) { CAttribute nameConstraint = NameAttributeConstraint(objectConstraint); if (nameConstraint == null) { if (requireNameConstraint) { return(false); } return(true); } bool nameFound = nameConstraint.ValidValue(name); return(nameFound); }
static private CAttribute NameAttributeConstraint(CObject objectConstraint) { Check.Require(objectConstraint != null, string.Format(CommonStrings.XMustNotBeNull, "objectConstraint")); CComplexObject cComplexObject = objectConstraint as CComplexObject; if (cComplexObject != null && cComplexObject.Attributes != null) { foreach (CAttribute attribute in cComplexObject.Attributes) { if (attribute.RmAttributeName == "name") { return(attribute); } } } return(null); }
public override bool ValidValue(object dataValue) { Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue")); IRmType rmType = dataValue as IRmType; Check.Require(rmType != null, string.Format(AmValidationStrings.ValueMustImplementIRmType, dataValue.GetType().ToString())); bool result = true; rmType.Constraint = this; if (!IsSameRmType(rmType)) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.IncorrectRmType, RmTypeName, rmType.GetRmTypeName())); } if (!result || !AnyAllowed()) { OpenEhr.RM.Common.Archetyped.Impl.Locatable locatable = dataValue as OpenEhr.RM.Common.Archetyped.Impl.Locatable; if (locatable != null) { ValidationUtility.PopulateLocatableAttributes(this, locatable); if (Parent != null && ArchetypeNodeId != locatable.ArchetypeNodeId) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.IncorrectNodeId, ArchetypeNodeId, locatable.ArchetypeNodeId)); } } System.ComponentModel.PropertyDescriptorCollection propertyDescriptorCollection = System.ComponentModel.TypeDescriptor.GetProperties(dataValue); if (Attributes != null) { foreach (CAttribute cAttribute in Attributes) { object attributeObject = null; string attributeName = RmFactory.GetOpenEhrV1RmName(cAttribute.RmAttributeName); System.ComponentModel.PropertyDescriptor property = propertyDescriptorCollection.Find(attributeName, true); // if the attributeName is not a class property, it must be a class function. if (property == null) { System.Reflection.MethodInfo method = dataValue.GetType().GetMethod(attributeName); if (method == null) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.UnexpectedAttributeX, attributeName)); continue; } else { attributeObject = method.Invoke(dataValue, null); } } else { attributeObject = property.GetValue(dataValue); } if (attributeObject == null) { if (cAttribute.Existence.Lower > 0) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TmExpectedConstraintMissing, cAttribute.RmAttributeName)); } } else if (cAttribute.Existence.Upper == 0) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TmForbiddenConstraint, cAttribute.RmAttributeName)); } else if (!cAttribute.ValidValue(attributeObject)) { result = false; } else { DvCodedText codedText = dataValue as DvCodedText; if (codedText != null && cAttribute.RmAttributeName == "defining_code") { // validate the code string before validating the coded value if (codedText.DefiningCode.TerminologyId.Value == "local") { CObject parentObject = cAttribute.parent; CArchetypeRoot cArchetypeRoot = ValidationUtility.GetCArchetypeRoot(parentObject); if (!cArchetypeRoot.TermDefinitions.HasKey(codedText.DefiningCode.CodeString)) { result = false; string code = codedText.DefiningCode == null ? "" : codedText.DefiningCode.CodeString; ValidationContext.AcceptValidationError(this, string.Format("code {0} is not existing archetype term", code)); } } if (result && !ValidationUtility.ValidValueTermDef(codedText, cAttribute, ValidationContext.TerminologyService)) { result = false; string code = codedText.DefiningCode == null ? "" : codedText.DefiningCode.CodeString; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.TextValueXInvalidForCodeY, codedText.Value, code)); } } } } } } return(result); }
public CObject ConstraintAtPath(string path) { if (string.IsNullOrEmpty(path)) { return(this); } if (constraintAtPath.Contains(path)) { return(constraintAtPath[path] as CObject); } CObject constraintFound = null; ConstraintPath constraintPath = new ConstraintPath(path); if (path.StartsWith("/") && this.Parent != null) { ArchetypeConstraint rootConstraint = this.ConstraintParent; while (rootConstraint.ConstraintParent != null) { rootConstraint = rootConstraint.ConstraintParent; } CComplexObject cComplexObject = rootConstraint as CComplexObject; if (cComplexObject == null) { throw new ArgumentException(AmValidationStrings.RootConstraintInvalid); } constraintFound = cComplexObject.ConstraintAtPath(path); } else { foreach (CAttribute attribute in this.attributes) { if (attribute.RmAttributeName == constraintPath.FirstStepAttributeName) { System.Collections.Generic.IList <CObject> matchedChildren = new System.Collections.Generic.List <CObject>(); foreach (CObject cObject in attribute.Children) { if (cObject.NodeId == constraintPath.FirstStepNodeId) { matchedChildren.Add(cObject); } else if (cObject is CArchetypeRoot && cObject.ArchetypeNodeId == constraintPath.FirstStepNodeId) { matchedChildren.Add(cObject); } } if (matchedChildren.Count <= 0) { throw new ArgumentException(string.Format(AmValidationStrings. MissingChildrenWithNodeIdX, constraintPath.FirstStepNodeId)); } else if (matchedChildren.Count == 1) { constraintFound = matchedChildren[0]; } else if (!constraintPath.HasNameConstraint()) { throw new ArgumentException(string.Format(AmValidationStrings.PathYNotUniqueAtX, constraintPath.ToString(), constraintPath.FirstStepNodeId)); } else { DvText name = (!string.IsNullOrEmpty(constraintPath.FirstStepNameValue) ? new DvText(constraintPath.FirstStepNameValue) : new DvCodedText(constraintPath.FirstStepName)); foreach (CObject cObject in matchedChildren) { if (CMultipleAttribute.HasNameAttributeConstraint(cObject, name)) { constraintFound = cObject; break; } } } break; } } } if (constraintFound == null) { throw new ArgumentException(string.Format(AmValidationStrings.NoConstraintForPathX, path)); } CComplexObject complexObject = constraintFound as CComplexObject; if (complexObject != null) { constraintFound = complexObject.ConstraintAtPath(constraintPath.NextSteps); } this.constraintAtPath.Add(path, constraintFound); Check.Ensure(constraintFound != null); return(constraintFound); }
public static bool HasNameAttributeConstraint(CObject objectConstraint, DvText name) { return(HasNameAttributeConstraint(objectConstraint, name, false)); }
private bool IsOrderedChildrenValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children")); int n = 0; bool result = true; foreach (object dataItem in dataChildren) { int startingPoint = n; CObject matchedCObject = null; string dataItemRmType = ((IRmType)dataItem).GetRmTypeName(); ILocatable locatable = dataItem as ILocatable; while (n < Children.Count) { CObject eachChild = Children[n]; if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId) { if (eachChild.IsSameRmType(dataItem as IRmType)) { matchedCObject = eachChild; CComplexObject complexObject = eachChild as CComplexObject; if (complexObject != null) { CAttribute nameAttribute = NameAttributeConstraint(complexObject); if (nameAttribute != null) { bool nameMatched = false; AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; nameMatched = nameAttribute.ValidValue(locatable.Name); } finally { ValidationContext.AcceptError = previousErrorDelegate; } if (nameMatched) { break; } else { n++; continue; } } } break; } } n++; } if (matchedCObject == null) { n = startingPoint; bool validationResult = true; if (MatchedWithSlot(locatable, out validationResult)) { result = validationResult; } else { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.YNotAllowedByAttributeXConstraint, RmAttributeName, dataItemRmType)); } } else if (!matchedCObject.ValidValue(dataItem)) { result = false; } } return(result); }
private bool IsUnorderedChildrenValid(AssumedTypes.IList dataChildren) { Check.Require(dataChildren != null, string.Format(CommonStrings.XMustNotBeNull, "children")); bool result = true; foreach (object dataItem in dataChildren) { System.Collections.Generic.List <CObject> matchedChildren = new System.Collections.Generic.List <CObject>(); System.Collections.Generic.List <ArchetypeSlot> slots = new System.Collections.Generic.List <ArchetypeSlot>(); IRmType rmType = dataItem as IRmType; Check.Assert(rmType != null, string.Format(AmValidationStrings.XMustImplementY, dataItem.GetType().ToString(), "IRmType")); ILocatable locatable = dataItem as ILocatable; // get all child constraint objects with this data item's node_id foreach (CObject eachChild in Children) { if (locatable == null || locatable.ArchetypeNodeId == eachChild.ArchetypeNodeId) { if (eachChild.IsSameRmType(rmType)) { matchedChildren.Add(eachChild); } } } bool matchedWithSlot = false; if (matchedChildren.Count == 0) { bool validationResult = true; matchedWithSlot = MatchedWithSlot(locatable, out validationResult); if (matchedWithSlot) { result &= validationResult; } else { // child constraint object not found for this data item result = false; string errorRmTypeName = rmType.GetRmTypeName(); ILocatable locatableDataItem = dataItem as ILocatable; if (locatableDataItem != null) { ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXWithIdYNotAllowedByAttributeZ, errorRmTypeName, locatableDataItem.ArchetypeNodeId, RmAttributeName)); } else { ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ItemXNotAllowedByAttributeY, errorRmTypeName, RmAttributeName)); } } } CObject unnamedMatchedObject = null; bool validResult = false; // attempt to match data item against child constraint objects with a name attribute constraint foreach (CObject matchedObject in matchedChildren) { CComplexObject complexObject = matchedObject as CComplexObject; if (complexObject == null) { throw new ApplicationException(AmValidationStrings.MultiAttributeChildNotComplexObj); } CAttribute nameAttribute = NameAttributeConstraint(complexObject); if (nameAttribute != null) { bool nameAttributeFound = false; AcceptValidationError previousErrorDelegate = ValidationContext.AcceptError; try { ValidationContext.AcceptError = null; nameAttributeFound = nameAttribute.ValidValue(locatable.Name); } finally { ValidationContext.AcceptError = previousErrorDelegate; } if (nameAttributeFound) { validResult = matchedObject.ValidValue(dataItem); if (validResult) { break; } else { result = false; } } } else { // keep child constraint object with no name attribute constraint for later if (unnamedMatchedObject != null) { throw new ApplicationException(AmValidationStrings.ExpectingOnlyOneUnnamedChild); } unnamedMatchedObject = matchedObject; } } // no matching named object constraint, so attempt to validate against unnamed object constraint if (!validResult && !matchedWithSlot) { if (unnamedMatchedObject == null) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.NotAllowedByAttributeXConstraint, RmAttributeName)); } else if (!unnamedMatchedObject.ValidValue(dataItem)) { result = false; } } } return(result); }
public void ReadCObject(XmlReader reader, CObject cObject) { this.reader = reader; ReadXml(cObject); }
private void ReadXmlBase(CObject cObject) { DesignByContract.Check.Require(cObject != null, "cObject must not be null."); if (reader.LocalName != "rm_type_name") { throw new ValidationException("expected local name is rm_type_name, but it is " + reader.LocalName); } cObject.RmTypeName = reader.ReadElementContentAsString("rm_type_name", OpenEhrNamespace); reader.MoveToContent(); if (reader.LocalName != "occurrences") throw new InvalidXmlException("occurrences", reader.LocalName); cObject.Occurrences = new OpenEhr.AssumedTypes.Interval<int>(); this.ReadXml(cObject.Occurrences); if (reader.LocalName != "node_id") throw new InvalidXmlException("node_id", reader.LocalName); cObject.NodeId = reader.ReadElementContentAsString("node_id", OpenEhrNamespace); reader.MoveToContent(); }
protected void ValidateBase(CObject cObject) { this.Validate((ArchetypeConstraint)cObject); Invariant(!string.IsNullOrEmpty(cObject.RmTypeName), string.Format( CommonStrings.XMustNotBeNullOrEmpty, "CObject.RmTypeName")); Invariant(cObject.Occurrences!= null, string.Format( CommonStrings.XMustNotBeNull, "CObject.Occurrences")); this.Validate(cObject.Occurrences); // TODO: nodeId invariants checking if (cObject.Parent != null) { Invariant(cObject.Parent.GetType() != typeof(CSingleAttribute) || cObject.Occurrences.Upper <= 1, AmValidationStrings.SingleParentOccurrencesBad); } }
protected void Validate(CObject cObject) { if (cObject == null) throw new ArgumentNullException(string.Format( CommonStrings.XMustNotBeNull, "cObject")); const string methodName = "Validate"; try { System.Reflection.MethodInfo method = this.GetType().GetMethod(methodName, System.Reflection.BindingFlags.ExactBinding | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, Type.DefaultBinder, new Type[] { cObject.GetType() }, new System.Reflection.ParameterModifier[0]); if (method != null) { // Avoid StackOverflow exceptions by executing only if the method and visitable // are different from the last parameters used. if (method != lastCObjectMethod || cObject != lastCObject) { lastCObjectMethod = method; lastCObject = cObject; method.Invoke(this, new Object[] { cObject }); } else { string message = string.Format(CommonStrings.LoopingMethodTerminated, methodName, cObject.GetType().ToString()); System.Diagnostics.Debug.WriteLine(message); throw new ApplicationException(message); } } else { string message = string.Format(CommonStrings.MethodXNotImplementedForParamTypeY, methodName, cObject.GetType().ToString()); System.Diagnostics.Debug.WriteLine(message); throw new ApplicationException(message); } } catch (Exception ex) { if (ex.InnerException != null) if (ex.InnerException is ApplicationException && ex.InnerException.InnerException != null && ex.InnerException.Message == ex.InnerException.InnerException.Message) throw new ApplicationException(ex.InnerException.Message, ex.InnerException.InnerException); else throw new ApplicationException(ex.InnerException.Message, ex.InnerException); else throw new ApplicationException(ex.Message, ex); } }
public void WriteCObject(XmlWriter writer, CObject cObject) { this.writer = writer; //Changed by LMT 29/Apr/2009 if(cObject is CComplexObject) WriteXmlBase(cObject); else this.WriteXml(cObject); }
private void WriteXmlBase(CObject cObject) { Check.Require(cObject != null, string.Format(CommonStrings.XMustNotBeNull, "cObject")); Check.Require(!string.IsNullOrEmpty(cObject.RmTypeName), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "cObject.RmTypeName")); Check.Require(cObject.Occurrences != null, string.Format(CommonStrings.XMustNotBeNull, "cObject.Occurrences")); string openEhrPrefix = UseOpenEhrPrefix(writer); writer.WriteElementString(openEhrPrefix, "rm_type_name", OpenEhrNamespace, cObject.RmTypeName); writer.WriteStartElement(openEhrPrefix, "occurrences", OpenEhrNamespace); this.WriteXml(cObject.Occurrences); writer.WriteEndElement(); writer.WriteElementString(openEhrPrefix, "node_id", OpenEhrNamespace, cObject.NodeId); }
private void ReadXml(CObject cObject) { if (cObject == null) throw new ArgumentNullException(string.Format(CommonStrings.XIsNull, "cObject")); const string methodName = "ReadXml"; try { System.Reflection.MethodInfo method = this.GetType().GetMethod(methodName, System.Reflection.BindingFlags.ExactBinding | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, Type.DefaultBinder, new Type[] { cObject.GetType() }, new System.Reflection.ParameterModifier[0]); if (method != null) { if (method != lastMethodReadCObject || cObject!= lastCObjectRead) { lastMethodReadCObject = method; lastCObjectRead = cObject; method.Invoke(this, new Object[] { cObject }); } else { string message = string.Format(CommonStrings.LoopingMethodTerminated, methodName, cObject.GetType().ToString()); System.Diagnostics.Debug.WriteLine(message); throw new ApplicationException(message); } } else { string message = string.Format(CommonStrings.MethodXNotImplementedForParamTypeY, methodName, cObject.GetType().ToString()); System.Diagnostics.Debug.WriteLine(message); throw new ApplicationException(message); } } catch (System.Reflection.TargetInvocationException ex) { if (ex.InnerException != null) throw new ApplicationException(ex.InnerException.Message, ex.InnerException); else throw new ApplicationException(ex.Message, ex); } }