예제 #1
0
        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);
        }
예제 #2
0
        public override bool ValidValue(object aValue)
        {
            Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue"));

            if (this.AnyAllowed())
            {
                return(true);
            }

            bool IsValidValue = true;

            DvOrdinal aValueDvOrdinal = aValue as DvOrdinal;

            if (aValueDvOrdinal != null)
            {
                bool foundInList = false;

                foreach (DvOrdinal dvOrdinal in this.List)
                {
                    if (dvOrdinal == aValueDvOrdinal)
                    {
                        if (string.IsNullOrEmpty(aValueDvOrdinal.Symbol.DefiningCode.CodeString) ||
                            string.IsNullOrEmpty(aValueDvOrdinal.Symbol.DefiningCode.TerminologyId.Value))
                        {
                            aValueDvOrdinal.Symbol.DefiningCode.CodeString =
                                dvOrdinal.Symbol.DefiningCode.CodeString;

                            aValueDvOrdinal.Symbol.DefiningCode.TerminologyId =
                                new TerminologyId(dvOrdinal.Symbol.DefiningCode.TerminologyId.Value);
                        }

                        foundInList = true;
                        break;
                    }
                }

                if (!foundInList)
                {
                    IsValidValue = false;
                    this.ValidationContext.AcceptValidationError(this,
                                                                 string.Format(AmValidationStrings.XNotInCDvOrdinalList, aValueDvOrdinal));
                }
                else
                {
                    if (!ValidationUtility.ValidValueTermDef(aValueDvOrdinal.Symbol, this.Parent, ValidationContext.TerminologyService))
                    {
                        IsValidValue = false;
                        this.ValidationContext.AcceptValidationError(this, string.Format(
                                                                         AmValidationStrings.DvOrdinalSymbolXIncorrectForCodeY,
                                                                         aValueDvOrdinal.Symbol.Value, aValueDvOrdinal.Symbol.DefiningCode.CodeString));
                    }
                }
            }
            else
            {
                IsValidValue = false;
                this.ValidationContext.AcceptValidationError(this,
                                                             string.Format(AmValidationStrings.InvalidDvOrdinalX, aValue));
            }

            return(IsValidValue);
        }