private static object AttributeListToObject(Type objectType, FeatureLayerInfo layerInfo, Graphic graphic, IEnumerable <string> attributeKeys, IEnumerable <string> propertyNames)
        {
            string typeIdFieldName = null;

            if (layerInfo != null && string.IsNullOrEmpty(layerInfo.TypeIdField) == false)
            {
                if (_fieldMapping != null && _fieldMapping.ContainsKey(layerInfo.TypeIdField))
                {
                    typeIdFieldName = _fieldMapping.KeyOfValue(layerInfo.TypeIdField);
                }
            }

            Action <object, string, object> myDynamicRangeValidationMethod = (instance, fieldName, value) =>
            {
                if (layerInfo == null || layerInfo.Fields == null || layerInfo.Fields.Count == 0)
                {
                    return;
                }

                var field = layerInfo.Fields.FirstOrDefault(f => string.Equals(f.FieldName, fieldName));
                if (field != null)
                {
                    if (FieldDomainUtils.IsDynamicDomain(field, layerInfo))
                    {
                        var fieldInfo   = instance.GetType().GetProperty(typeIdFieldName);
                        var typeIDValue = fieldInfo.GetValue(instance, null);
                        RangeDomainValidator.ValidateRange(layerInfo, field, typeIDValue, value);
                    }
                    else if (field.Domain != null && !(field.Domain is CodedValueDomain))
                    {
                        RangeDomainValidator.ValidateRange(field, value);
                    }
                }
            };

            var row = Activator.CreateInstance(objectType, new object[] { graphic, myDynamicRangeValidationMethod });

            foreach (string type in propertyNames ?? attributeKeys)
            {
                IDictionary <string, object> currentDict = graphic.Attributes;
                string propertyName = type;
                string attributeKey = type;
                if (propertyNames == null)
                {
                    if (_fieldMapping.ContainsKey(attributeKey))
                    {
                        propertyName = _fieldMapping[attributeKey];
                    }
                }
                else
                {
                    attributeKey = _fieldMapping.KeyOfValue(propertyName);
                }
                if (propertyName != null && currentDict != null)
                {
                    if (currentDict.ContainsKey(attributeKey))
                    {
                        SetProperty(currentDict[attributeKey], row, objectType.GetProperty(propertyName));
                    }
                }
            }
            return(row);
        }
Ejemplo n.º 2
0
        private static string errorMessage = Properties.Resources.Validation_InvalidRangeDomain;           // Error message shown after validation fails

        /// <summary>
        /// Validates subtype fields that have range domain defined.
        /// </summary>
        /// <param name="layerInfo">FeatureLayerInfo that contains the subtype information.</param>
        /// <param name="field">The field to validate.</param>
        /// <param name="typeIdValue">The current TypeIDField value to uses as lookup value for SubType</param>
        /// <param name="value">The current value to validate.</param>
        internal static void ValidateRange(FeatureLayerInfo layerInfo, Field field, object typeIdValue, object value)
        {
            if (layerInfo == null || field == null || typeIdValue == null || value == null)
            {
                return;
            }

            switch (field.Type)
            {
            case Field.FieldType.Date:
                if (value is DateTime)
                {
                    var dynamicRangeDomains = FieldDomainUtils.BuildDynamicRangeDomain <DateTime>(field, layerInfo);
                    if (dynamicRangeDomains != null && typeIdValue != null)
                    {
                        var domains = dynamicRangeDomains.Where(kvp => kvp.Key.Equals(typeIdValue));
                        if (domains != null && domains.Any())
                        {
                            var domain = domains.First().Value;
                            if (((DateTime)value) < domain.MinimumValue || ((DateTime)value) > domain.MaximumValue)
                            {
                                throw new ArgumentException(string.Format(errorMessage, domain.MinimumValue, domain.MaximumValue));
                            }
                        }
                    }
                }
                break;

            case Field.FieldType.Double:
                if (value is Double)
                {
                    var dynamicRangeDomains = FieldDomainUtils.BuildDynamicRangeDomain <Double>(field, layerInfo);
                    if (dynamicRangeDomains != null && typeIdValue != null)
                    {
                        var domains = dynamicRangeDomains.Where(kvp => kvp.Key.Equals(typeIdValue));
                        if (domains != null && domains.Any())
                        {
                            var domain = domains.First().Value;
                            if (((Double)value) < domain.MinimumValue || ((Double)value) > domain.MaximumValue)
                            {
                                throw new ArgumentException(string.Format(errorMessage, domain.MinimumValue, domain.MaximumValue));
                            }
                        }
                    }
                }
                break;

            case Field.FieldType.Integer:
                if (value is Int32)
                {
                    var dynamicRangeDomains = FieldDomainUtils.BuildDynamicRangeDomain <Int32>(field, layerInfo);
                    if (dynamicRangeDomains != null && typeIdValue != null)
                    {
                        var domains = dynamicRangeDomains.Where(kvp => kvp.Key.Equals(typeIdValue));
                        if (domains != null && domains.Any())
                        {
                            var domain = domains.First().Value;
                            if (((Int32)value) < domain.MinimumValue || ((Int32)value) > domain.MaximumValue)
                            {
                                throw new ArgumentException(string.Format(errorMessage, domain.MinimumValue, domain.MaximumValue));
                            }
                        }
                    }
                }
                break;

            case Field.FieldType.Single:
                if (value is Single)
                {
                    var dynamicRangeDomains = FieldDomainUtils.BuildDynamicRangeDomain <Single>(field, layerInfo);
                    if (dynamicRangeDomains != null && typeIdValue != null)
                    {
                        var domains = dynamicRangeDomains.Where(kvp => kvp.Key.Equals(typeIdValue));
                        if (domains != null && domains.Any())
                        {
                            var domain = domains.First().Value;
                            if (((Single)value) < domain.MinimumValue || ((Single)value) > domain.MaximumValue)
                            {
                                throw new ArgumentException(string.Format(errorMessage, domain.MinimumValue, domain.MaximumValue));
                            }
                        }
                    }
                }
                break;

            case Field.FieldType.SmallInteger:
                if (value is Int16)
                {
                    var dynamicRangeDomains = FieldDomainUtils.BuildDynamicRangeDomain <Int16>(field, layerInfo);
                    if (dynamicRangeDomains != null && typeIdValue != null)
                    {
                        var domains = dynamicRangeDomains.Where(kvp => kvp.Key.Equals(typeIdValue));
                        if (domains != null && domains.Any())
                        {
                            var domain = domains.First().Value;
                            if (((Int16)value) < domain.MinimumValue || ((Int16)value) > domain.MaximumValue)
                            {
                                throw new ArgumentException(string.Format(errorMessage, domain.MinimumValue, domain.MaximumValue));
                            }
                        }
                    }
                }
                break;
            }
        }