Click here to Skip to main content
15,892,697 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.7K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using System;
using System.Collections;
using Transformalize.Libs.FileHelpers.Engines;
using Transformalize.Libs.FileHelpers.Enums;
using Transformalize.Libs.FileHelpers.Helpers;

namespace Transformalize.Libs.FileHelpers.ErrorHandling
{
    /// <summary>This is the class that handles the errors of the engines process.</summary>
    /// <remarks>All the engines and DataStorages contains a ErrorManager.</remarks>
#if NET_2_0
    [DebuggerDisplay("{ErrorsDescription()}. ErrorMode: {ErrorMode.ToString()}")]
#endif
    public sealed class ErrorManager
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="ErrorManager" /> class.
        /// </summary>
        public ErrorManager()
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="ErrorManager" /> class. with the specified <see cref="ErrorMode" />.
        /// </summary>
        /// <param name="mode">Indicates the error behavior of the class.</param>
        public ErrorManager(ErrorMode mode)
        {
            mErrorMode = mode;
        }


#if NET_2_0
        private string ErrorsDescription()
        {
            if (ErrorCount == 1)
                return ErrorCount.ToString() + " Error";
            else if (ErrorCount == 0)
                return "No Errors";
            else
                return ErrorCount.ToString() + " Errors";
        }

        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private readonly ArrayList mErrorsArray = new ArrayList();

        /// <summary>
        ///     Is an array of <see cref="ErrorInfo" /> that contains the errors of the last operation in this class.
        /// </summary>
#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
#endif
        public ErrorInfo[] Errors
        {
            get { return (ErrorInfo[]) mErrorsArray.ToArray(typeof (ErrorInfo)); }
        }

#if NET_2_0
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
#endif
        private ErrorMode mErrorMode = ErrorMode.ThrowException;


        /// <summary>
        ///     Indicates the behavior of the <see cref="FileHelperEngine" /> when it found an error.
        /// </summary>
        public ErrorMode ErrorMode
        {
            get { return mErrorMode; }
            set { mErrorMode = value; }
        }


        /// <summary>Number of contained errors.</summary>
        public int ErrorCount
        {
            get { return mErrorsArray.Count; }
        }

        /// <summary>Indicates if contains one or more errors.</summary>
        public bool HasErrors
        {
            get { return mErrorsArray.Count > 0; }
        }

        /// <summary>Clears the error collection.</summary>
        public void ClearErrors()
        {
            mErrorsArray.Clear();
        }

        /// <summary>Add the specified ErrorInfo to the contained collection.</summary>
        /// <param name="error"></param>
        internal void AddError(ErrorInfo error)
        {
            mErrorsArray.Add(error);
        }

        /// <summary>Add the specified ErrorInfo to the contained collection.</summary>
        internal void AddErrors(ErrorManager errors)
        {
            mErrorsArray.AddRange(errors.mErrorsArray);
        }

//		public void ProcessError(Exception ex, string line)
//		{
//		}


        /// <summary>Saves the contained errors to the specified file.</summary>
        /// <param name="fileName">The file that contains the errors.</param>
        public void SaveErrors(string fileName)
        {
            string header;
            if (ErrorCount > 0)
                header = "FileHelpers - Errors Saved ";
            else
                header = "FileHelpers - NO Errors Found ";

            header += "at " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
            header += StringHelper.NewLine + "LineNumber | LineString |ErrorDescription";

            SaveErrors(fileName, header);
        }

        /// <summary>Saves the contained errors to the specified file.</summary>
        /// <param name="fileName">The file that contains the errors.</param>
        /// <param name="header">The header line of the errors file.</param>
        public void SaveErrors(string fileName, string header)
        {
            var engine = new FileHelperEngine(typeof (ErrorInfo));

            if (header.IndexOf(StringHelper.NewLine) == header.LastIndexOf(StringHelper.NewLine))
                header += StringHelper.NewLine;

            engine.HeaderText = header;
            engine.WriteFile(fileName, Errors);
        }

        /// <summary>Load errors from a file.</summary>
        /// <param name="fileName">The file that contains the errors.</param>
        public static ErrorInfo[] LoadErrors(string fileName)
        {
            var engine = new FileHelperEngine(typeof (ErrorInfo));
            return (ErrorInfo[]) engine.ReadFile(fileName);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions