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 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); }
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); }