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