public CodedDomainValueProvider([NotNull] ITable table,
                                        [NotNull] string codedDomainField,
                                        [CanBeNull] IFormatProvider formatProvider)
        {
            _codedFieldIndex = table.FindField(codedDomainField);

            IField field  = table.Fields.Field[_codedFieldIndex];
            var    domain = (ICodedValueDomain)field.Domain;

            _valueDict = new Dictionary <object, T?>();
            foreach (CodedValue codedValue in DomainUtils.GetCodedValueList(domain))
            {
                object code = codedValue.Value;
                string name = codedValue.Name;

                try
                {
                    var value =
                        (T)Assert.NotNull(Convert.ChangeType(name, typeof(T), formatProvider));

                    _valueDict.Add(code, value);
                }
                catch (Exception e)
                {
                    _msg.VerboseDebugFormat("Unable to convert '{0}' to type {1}: {2}",
                                            name, typeof(T).Name, e.Message);
                    _valueDict.Add(code, null);
                }
            }
        }
예제 #2
0
        private int CheckDomain([NotNull] DomainUsage domainUsage)
        {
            IDomain domain = domainUsage.Domain;

            var codedValueDomain = domain as ICodedValueDomain;

            if (codedValueDomain == null)
            {
                return(NoError);
            }

            List <CodedValue> codedValues = DomainUtils.GetCodedValueList(codedValueDomain);

            int nonEqualNameValueCount = 0;

            int errorCount = 0;

            foreach (CodedValue codedValue in codedValues)
            {
                if (!Equals(codedValue.Value.ToString().Trim(), codedValue.Name.Trim()))
                {
                    nonEqualNameValueCount++;
                }

                if (!StringUtils.IsNotEmpty(codedValue.Name))
                {
                    if (!_allowEmptyName)
                    {
                        errorCount +=
                            ReportSchemaPropertyError(
                                Codes[Code.NoName], domain.Name,
                                new[] { codedValue.Value },
                                "Value [{0}] in coded value domain '{1}' does not have an associated name",
                                codedValue.Value, domain.Name);
                    }

                    continue;
                }

                string          message;
                TextLengthIssue?lengthIssue = SchemaTestUtils.HasValidLength(
                    codedValue.Name, _maximumNameLength, "name", out message);

                if (lengthIssue != null)
                {
                    errorCount +=
                        ReportSchemaPropertyError(GetIssueCode(lengthIssue.Value), domain.Name,
                                                  new[] { codedValue.Value },
                                                  "Value [{0}] in coded value domain '{1}': {2} ('{3}')",
                                                  codedValue.Value, domain.Name, message,
                                                  codedValue.Name);
                }
            }

            // report non-unique names
            errorCount += CheckUniqueNames(domainUsage, codedValues);

            // report error if the number of coded values is less than the minimum count
            if (codedValues.Count < _minimumValueCount)
            {
                string format = codedValues.Count == 1
                                                        ? "Domain '{0}' has {1} coded value. Minimum: {2}"
                                                        : "Domain '{0}' has {1} coded values. Minimum: {2}";

                string description = string.Format(format,
                                                   domainUsage.DomainName, codedValues.Count,
                                                   _minimumValueCount);

                errorCount += ReportSchemaPropertyError(Codes[Code.TooFewCodedValues],
                                                        domainUsage.DomainName, description);
            }

            // report error if the number of coded values with non-equal name/value pair is
            // less than the minimum count for non-equal values (and the total coded value
            // count exceeds that minimum value; otherwise an error would always be reported)
            if (nonEqualNameValueCount < _minimumNonEqualNameValueCount &&
                codedValues.Count >= _minimumNonEqualNameValueCount)
            {
                string description = string.Format(
                    "Domain '{0}' has {1} coded values with a name that is different from the value. Minimum: {2}",
                    domainUsage.DomainName, nonEqualNameValueCount,
                    _minimumNonEqualNameValueCount);

                errorCount +=
                    ReportSchemaPropertyError(Codes[Code.NotEnoughNamesDifferentFromValue],
                                              domainUsage.DomainName, description);
            }

            return(errorCount);
        }