Esempio n. 1
0
        /// <summary>
        /// Validates the target.
        /// </summary>
        /// <param name="temp">The temporary.</param>
        /// <param name="current">The current.</param>
        /// <param name="rdr">The RDR.</param>
        /// <param name="mappings">The mappings.</param>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        private ValidationStatus ValidateTarget(object temp, int current, IDataReader rdr,
                                                IDictionary <string, string> mappings, IStatelessSession session)
        {
            //bool hasError = false;
            var validationResult = ValidationEngine.ValidateInstance(temp);
            // var updateCount = validationResult.PropertyErrors.Count + validationResult.PropertyWarnings.Count;
            bool hasError = validationResult.PropertyErrors.Any() || validationResult.ClassErrors.Any();

            if (validationResult.ClassErrors.Any())
            {
                hasError = true;
                foreach (var res in validationResult.ClassErrors)
                {
                    string member = string.Empty;
                    string msg    = null;
                    object value  = null;
                    if (res.Property != null)
                    {
                        member = res.Property.Name;

                        if (mappings.ContainsKey(member))
                        {
                            var column = mappings[member];
                            var ord    = rdr.GetOrdinal(column);
                            value = rdr.GetValue(ord);
                            msg   = string.Format("Value excluded: {0}",
                                                  value == null
                                    ? "<<NULL>>"
                                    : string.IsNullOrEmpty(value.ToString())
                                        ? @""""
                                        : value.ToString());
                        }
                    }


                    if (res.ErrorState == ValidationErrorState.ValidationError && !string.IsNullOrEmpty(res.Message))
                    {
                        msg = string.Format("{1} (Value excluded: {0})",
                                            (value == null
                               ? "<<NULL>>"
                               : string.IsNullOrEmpty(value.ToString()) ? @"""" : value.ToString()),
                                            res.Message);
                    }

                    if (res.ErrorState == ValidationErrorState.ValidationError && !string.IsNullOrEmpty(res.Message))
                    {
                        member = "Row Level";
                        msg    = res.Message;
                    }

                    GuardError(member, msg, current, res.ErrorState == ValidationErrorState.ExcludedByCrosswalk ? RecordState.ExcludedByCrosswalk : RecordState.ValidationError, session); //RecordState.ExcludedByCrosswalk
                }
            }

            if (validationResult.PropertyErrors.Any())
            {
                // updateCount += validationResult.PropertyErrors.Count;
                hasError = true;
                foreach (var err in validationResult.PropertyErrors.ToList())
                {
                    Element elemTest;
                    ElementDictionary.TryGetValue(err.Property.Name.ToLower(), out elemTest);
                    if (elemTest != null)
                    {
                        GuardError(elemTest.Description,
                                   ElementMappingModel.ParseValueError(err.AttemptedValue, err.Message),
                                   current, RecordState.ValidationError, session);
                    }
                }
            }
            if (validationResult.PropertyWarnings.Any())
            {
                foreach (var err in validationResult.PropertyWarnings)
                {
                    Element elemTest;
                    ElementDictionary.TryGetValue(err.Property.Name.ToLower(), out elemTest);
                    if (elemTest != null)
                    {
                        GuardError(elemTest.Description,
                                   ElementMappingModel.ParseValueError(err.AttemptedValue, err.Message),
                                   current, RecordState.Warning, session);
                    }
                }
            }


            var result = new ValidationStatus();

            result.HasErrors = hasError;
            return(result);
        }
        /// <summary>
        /// Validates the target.
        /// </summary>
        /// <param name="temp">The temporary.</param>
        /// <param name="current">The current.</param>
        /// <param name="rdr">The RDR.</param>
        /// <param name="mappings">The mappings.</param>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        private ValidationStatus ValidateTarget(object temp, int current, IDataReader rdr,
                                                IDictionary <string, string> mappings, IStatelessSession session)
        {
            bool hasError         = false;
            var  validationResult = ValidationEngine.ValidateInstance(temp);
            var  updateCount      = validationResult.PropertyErrors.Count + validationResult.PropertyWarnings.Count;

            hasError = validationResult.PropertyErrors.Any() || validationResult.ClassErrors.Any();

            if (validationResult.ClassErrors.Any())
            {
                updateCount += validationResult.ClassErrors.Count;
                hasError     = true;

                foreach (var res in validationResult.ClassErrors)
                {
                    string member = string.Empty;
                    string msg    = null;
                    object value  = null;

                    if (res.Property != null)
                    {
                        member = res.Property.Name;
                        var column = mappings[member];
                        var ord    = rdr.GetOrdinal(column);
                        value = rdr.GetValue(ord);
                        msg   = string.Format("Value excluded: {0}",
                                              value == null
                                ? "<<NULL>>"
                                : string.IsNullOrEmpty(value.ToString())
                                    ? @""""
                                    : value.ToString());
                    }

                    if (res.ErrorState == ValidationErrorState.ValidationError && !string.IsNullOrEmpty(res.Message))
                    {
                        msg = string.Format("{1} (Value excluded: {0})",
                                            (value == null
                               ? "<<NULL>>"
                               : string.IsNullOrEmpty(value.ToString()) ? @"""" : value.ToString()),
                                            res.Message);
                    }

                    GuardError(member, msg, current, RecordState.ExcludedByCrosswalk, session);
                    //TODO: Output messages to screen, other log file per import, someplace besited the main session.log
                    //this.DataContextObject.Logger.Write(string.Format(VALIDATION_LOG_MESSAGE, DataContextObject.SelectedDataType.DataTypeName, DataContextObject.Title, "Class Error", msg), Category.Info, Priority.High);
                }
            }

            if (validationResult.PropertyErrors.Any())
            {
                hasError = true;

                foreach (var err in validationResult.PropertyErrors)
                {
                    Element elemTest;
                    ElementDictionary.TryGetValue(err.Property.Name.ToLower(), out elemTest);
                    if (elemTest != null)
                    {
                        GuardError(elemTest.Description,
                                   ElementMappingModel.ParseValueError(err.AttemptedValue, err.Message),
                                   current, RecordState.ValidationError, session);
                    }
                    //TODO: Output messages to screen, other log file per import, someplace besited the main session.log
                    //this.DataContextObject.Logger.Write(string.Format(VALIDATION_LOG_MESSAGE, DataContextObject.SelectedDataType.DataTypeName, DataContextObject.Title, "Error", err.Message), Category.Info, Priority.High);
                }
            }

            if (validationResult.PropertyWarnings.Any())
            {
                foreach (var err in validationResult.PropertyWarnings)
                {
                    Element elemTest;
                    ElementDictionary.TryGetValue(err.Property.Name.ToLower(), out elemTest);
                    if (elemTest != null)
                    {
                        GuardError(elemTest.Description,
                                   ElementMappingModel.ParseValueError(err.AttemptedValue, err.Message),
                                   current, RecordState.Warning, session);
                    }
                    //TODO: Output messages to screen, other log file per import, someplace besited the main session.log
                    //this.DataContextObject.Logger.Write(string.Format(VALIDATION_LOG_MESSAGE, DataContextObject.SelectedDataType.DataTypeName, DataContextObject.Title, "Warning", err.Message), Category.Info, Priority.High);
                }
            }


            var result = new ValidationStatus();

            result.HasErrors = hasError;
            return(result);
        }