protected override void SetAttributeValue(string attributeName, object value) { // Set Constraint if (value != null && HasConstraint) { CAttribute attributeConstraint = Constraint.GetAttribute(attributeName); if (attributeConstraint != null) { CMultipleAttribute multipleAttributeConstraint = attributeConstraint as CMultipleAttribute; if (multipleAttributeConstraint != null) { IAggregate aggregate = value as IAggregate; Check.Assert(aggregate != null, "value must implement IAggregate when attribute constraint is CMultipleAttribute"); aggregate.Constraint = multipleAttributeConstraint; } else { bool isValid = false; foreach (CObject objectConstraint in attributeConstraint.Children) { if (objectConstraint.ValidValue(value)) { isValid = true; IRmType rmType = value as IRmType; if (rmType != null) { rmType.Constraint = objectConstraint; } break; } } } } } }
public static bool IsSameRmType(string rmTypeName, IRmType rmObject) { Check.Require(rmObject != null, string.Format(CommonStrings.XMustNotBeNull, "rmObject")); Check.Require(!string.IsNullOrEmpty(rmTypeName), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "rmTypeName")); string actualTypeName = rmObject.GetRmTypeName(); if (rmTypeName == actualTypeName) { return(true); } Type actualRmType = rmObject.GetType(); while (actualRmType != null && actualRmType != typeof(RmType)) { actualTypeName = RmFactory.GetRmTypeName(actualRmType); if (actualTypeName == rmTypeName) { return(true); } if (actualTypeName != null) { actualRmType = actualRmType.BaseType; } else { actualRmType = null; } } return(false); }
void IAggregate.BuildPath(Path path) { Check.Require(constraint != null, "constraint must not be null"); Check.Require(path != null, "path must not be null"); Check.Require(path.Current != null, "path.Current must not be null"); Check.Require(path.Current.HasPredicatePath, "Current path step must have predicate"); T value; if (Has(path.Current.PredicatePath)) { value = this.GetItem(path.Current.PredicatePath); } else { AM.Archetype.ConstraintModel.CObject objectConstraint = this.Constraint.GetChildObjectConstraint(path.Current.PredicatePath); if (objectConstraint == null) { throw new ApplicationException("Constrain not found for current node predicate"); } AM.Archetype.ConstraintModel.CDefinedObject definedObjectConstraint = objectConstraint as AM.Archetype.ConstraintModel.CDefinedObject; if (definedObjectConstraint == null) { throw new ApplicationException("Constrain must be a CDefinedObject"); } value = AsT(definedObjectConstraint.DefaultValue); if (value == null) { throw new ApplicationException("value must be of type T"); } this.Add(value); } if (path.NextStep()) { IRmType rmType = value as IRmType; if (rmType == null) { throw new ApplicationException("expected IRmType"); } else { rmType.BuildPath(path); } } }
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 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 static string GetRmTypeName(IRmType rmType) { Check.Require(rmType != null, "rmType must not be null"); return rmType.GetRmTypeName(); }
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); }
protected void BuildPath(Path path) { Check.Require(path != null, "path must not be null"); Check.Require(path.Current != null, "current path step must not be null"); string attributeName = path.Current.Attribute; object value = GetAttributeValue(attributeName); if (value == null) { CComplexObject complexObjectConstraint = this.Constraint as CComplexObject; if (complexObjectConstraint == null) { throw new NotImplementedException(); } CAttribute attributeConstraint = complexObjectConstraint.GetAttribute(attributeName); if (attributeConstraint == null) { throw new ApplicationException("constraint for attribute not found"); } CMultipleAttribute multipleAttributeConstraint = attributeConstraint as CMultipleAttribute; if (multipleAttributeConstraint == null) { if (attributeConstraint.Children.Count != 1) { throw new ApplicationException("Single attribute constraint must have exactly one children"); } CObject objectConstraint = attributeConstraint.Children[0]; CDefinedObject definedObjectConstraint = objectConstraint as CDefinedObject; if (definedObjectConstraint == null) { throw new NotImplementedException(); } value = definedObjectConstraint.DefaultValue; SetAttributeValue(attributeName, value); } else { Type itemType = null; value = multipleAttributeConstraint.CreateAggregate(itemType); SetAttributeValue(attributeName, value); } } if (path.NextStep()) { IRmType rmType = value as IRmType; if (rmType == null) { AssumedTypes.IAggregate container = value as AssumedTypes.IAggregate; if (container != null) { container.BuildPath(path); } else { throw new ApplicationException("expected IRmType"); } } else { rmType.BuildPath(path); } } }
public static string GetRmTypeName(IRmType rmType) { Check.Require(rmType != null, "rmType must not be null"); return(rmType.GetRmTypeName()); }
internal bool IsSameRmType(IRmType rmObject) { return IsSameRmType(this.rmTypename, rmObject); }
public static bool IsSameRmType(string rmTypeName, IRmType rmObject) { Check.Require(rmObject != null, string.Format(CommonStrings.XMustNotBeNull, "rmObject")); Check.Require(!string.IsNullOrEmpty(rmTypeName), string.Format(CommonStrings.XMustNotBeNullOrEmpty, "rmTypeName")); string actualTypeName = rmObject.GetRmTypeName(); if (rmTypeName == actualTypeName) return true; Type actualRmType = rmObject.GetType(); while (actualRmType != null && actualRmType != typeof(RmType)) { actualTypeName = RmFactory.GetRmTypeName(actualRmType); if (actualTypeName == rmTypeName) return true; if (actualTypeName != null) actualRmType = actualRmType.BaseType; else actualRmType = null; } return false; }
internal bool IsSameRmType(IRmType rmObject) { return(IsSameRmType(this.rmTypename, rmObject)); }