public override bool ValidValue(object dataValue) { Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue")); AssumedTypes.IAggregate aggregate = dataValue as AssumedTypes.IAggregate; Check.Require(aggregate != null, string.Format( AmValidationStrings.XMustImplementY, dataValue.GetType().ToString(), "IAggregate")); bool result = true; aggregate.Constraint = this; AssumedTypes.IList iList = dataValue as AssumedTypes.IList; if (iList == null) { throw new ApplicationException(string.Format(AmValidationStrings.XMustImplementY, dataValue.GetType().ToString(), "IList")); } if (!Cardinality.Interval.Has(iList.Count)) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.CardinalityOutOfBounds, iList.Count, RmAttributeName)); } result &= Cardinality.IsOrdered ? IsOrderedChildrenValid(iList) : IsUnorderedChildrenValid(iList); result &= IsOccurrencesValid(iList); return(result); }
private bool MatchedWithSlot(ILocatable dataItem, out bool isValid) { isValid = true; foreach (CObject child in Children) { ArchetypeSlot slot = child as ArchetypeSlot; if (slot != null && slot.CanFillWith(dataItem.ArchetypeNodeId) && !slot.IsFull) { CArchetypeRoot cArchetypeRoot = ValidationContext.FetchOperationalObject(this, new ArchetypeId(dataItem.ArchetypeNodeId)); if (cArchetypeRoot == null) { isValid = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ArchetypeXLoadFailed, dataItem.ArchetypeNodeId)); } else { slot.AddSlotFiller(cArchetypeRoot); cArchetypeRoot.SetValidationContext(ValidationContext); isValid = cArchetypeRoot.ValidValue(dataItem); } return(true); } } return(false); }
public override bool ValidValue(object aValue) { bool result = true; Locatable locatable = aValue as Locatable; if (locatable == null) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ExpectingValueXToBeTypeY, aValue, "Locatable")); } //TODO: validate template ID - probably need to do this in OperationalTemplate class if (!locatable.IsArchetypeRoot) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ExpectingValueXToBeTypeY, aValue, "CArchetypeRoot")); } if (locatable.ArchetypeNodeId != archetypeId.Value) { result = false; ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.ExpectingNodeIdXButGotY, archetypeId.Value, locatable.ArchetypeNodeId)); } if (!base.ValidValue(aValue)) { result = false; } return(result); }
public override bool ValidValue(object dataValue) { Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue")); int count = Children == null ? 0 : Children.Count; bool result = count == 0; bool wereErrorsSuppressed = ValidationContext.IsSuppressingAcceptErrors; try { ValidationContext.IsSuppressingAcceptErrors = count > 1; for (int i = 0; !result && i < count; i++) { result = Children[i].ValidValue(dataValue); } } finally { ValidationContext.IsSuppressingAcceptErrors = wereErrorsSuppressed; } if (!result && count > 1) { ValidationContext.AcceptValidationError(this, string.Format(AmValidationStrings.NotAllowedByAttributeXConstraint, RmAttributeName)); } return(result); }
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 override bool ValidValue(object dataValue) { Check.Require(dataValue != null, string.Format(CommonStrings.XMustNotBeNull, "dataValue")); bool result = true; if (!AnyAllowed()) { string errorMessage = item.ValidValue(dataValue); result = string.IsNullOrEmpty(errorMessage); if (!result) { ValidationContext.AcceptValidationError(this, errorMessage); } } return(result); }
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); }
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); }