PageRenderTime 109ms CodeModel.GetById 40ms app.highlight 26ms RepoModel.GetById 34ms app.codeStats 1ms

/Rhino.Etl.Core/EtlProcess.cs

http://github.com/ayende/rhino-etl
C# | 180 lines | 107 code | 20 blank | 53 comment | 2 complexity | 75d357afd6ca53b6e565de17caf7e22d MD5 | raw file
  1using System.Configuration;
  2using Rhino.Etl.Core.Infrastructure;
  3
  4namespace Rhino.Etl.Core
  5{
  6    using System;
  7    using System.Collections.Generic;
  8    using System.Data;
  9    using Operations;
 10    using Pipelines;
 11
 12    /// <summary>
 13    /// A single etl process
 14    /// </summary>
 15    public abstract class EtlProcess : EtlProcessBase<EtlProcess>, IDisposable
 16    {
 17        private IPipelineExecuter pipelineExecuter = new ThreadPoolPipelineExecuter();
 18
 19        /// <summary>
 20        /// Gets the pipeline executer.
 21        /// </summary>
 22        /// <value>The pipeline executer.</value>
 23        public IPipelineExecuter PipelineExecuter
 24        {
 25            get { return pipelineExecuter; }
 26            set
 27            {
 28                Info("Setting PipelineExecutor to {0}", value.GetType().ToString());
 29                pipelineExecuter = value;
 30            }
 31        }
 32
 33
 34        /// <summary>
 35        /// Gets a new partial process that we can work with
 36        /// </summary>
 37        protected static PartialProcessOperation Partial
 38        {
 39            get
 40            {
 41                PartialProcessOperation operation = new PartialProcessOperation();
 42                return operation;
 43            }
 44        }
 45
 46        #region IDisposable Members
 47
 48        ///<summary>
 49        ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 50        ///</summary>
 51        ///<filterpriority>2</filterpriority>
 52        public void Dispose()
 53        {
 54            foreach (IOperation operation in operations)
 55            {
 56                operation.Dispose();
 57            }
 58        }
 59
 60        #endregion
 61
 62        /// <summary>
 63        /// Initializes this instance.
 64        /// </summary>
 65        protected abstract void Initialize();
 66
 67        /// <summary>
 68        /// Executes this process
 69        /// </summary>
 70        public void Execute()
 71        {
 72            Initialize();
 73            MergeLastOperationsToOperations();
 74            RegisterToOperationsEvents();
 75            Trace("Starting to execute {0}", Name);
 76            PipelineExecuter.Execute(Name, operations, TranslateRows);
 77
 78            PostProcessing();
 79        }
 80
 81        /// <summary>
 82        /// Translate the rows from one representation to another
 83        /// </summary>
 84        public virtual IEnumerable<Row> TranslateRows(IEnumerable<Row> rows)
 85        {
 86            return rows;
 87        }
 88
 89        private void RegisterToOperationsEvents()
 90        {
 91            foreach (IOperation operation in operations)
 92            {
 93                operation.OnRowProcessed += OnRowProcessed;
 94                operation.OnFinishedProcessing += OnFinishedProcessing;
 95            }
 96        }
 97
 98
 99        /// <summary>
100        /// Called when this process has finished processing.
101        /// </summary>
102        /// <param name="op">The op.</param>
103        protected virtual void OnFinishedProcessing(IOperation op)
104        {
105            Trace("Finished {0}: {1}", op.Name, op.Statistics);
106        }
107
108        /// <summary>
109        /// Allow derived class to deal with custom logic after all the internal steps have been executed
110        /// </summary>
111        protected virtual void PostProcessing()
112        {
113        }
114
115        /// <summary>
116        /// Called when a row is processed.
117        /// </summary>
118        /// <param name="op">The operation.</param>
119        /// <param name="dictionary">The dictionary.</param>
120        protected virtual void OnRowProcessed(IOperation op, Row dictionary)
121        {
122            if (op.Statistics.OutputtedRows % 1000 == 0)
123                Info("Processed {0} rows in {1}", op.Statistics.OutputtedRows, op.Name);
124            else
125                Debug("Processed {0} rows in {1}", op.Statistics.OutputtedRows, op.Name);
126        }
127
128        /// <summary>
129        /// Executes the command and return a scalar
130        /// </summary>
131        /// <typeparam name="T"></typeparam>
132        /// <param name="connectionName">Name of the connection.</param>
133        /// <param name="commandText">The command text.</param>
134        /// <returns></returns>
135        protected static T ExecuteScalar<T>(string connectionName, string commandText)
136        {
137            return ExecuteScalar<T>(ConfigurationManager.ConnectionStrings[connectionName], commandText);
138        }
139
140        /// <summary>
141        /// Executes the command and return a scalar
142        /// </summary>
143        /// <typeparam name="T"></typeparam>
144        /// <param name="connectionStringSettings">The connection string settings node to use</param>
145        /// <param name="commandText">The command text.</param>
146        /// <returns></returns>
147        protected static T ExecuteScalar<T>(ConnectionStringSettings connectionStringSettings, string commandText)
148        {
149            return Use.Transaction<T>(connectionStringSettings, delegate(IDbCommand cmd)
150            {
151                cmd.CommandText = commandText;
152                object scalar = cmd.ExecuteScalar();
153                return (T)(scalar ?? default(T));
154            });
155        }
156
157        /// <summary>
158        /// Gets all errors that occured during the execution of this process
159        /// </summary>
160        /// <returns></returns>
161        public IEnumerable<Exception> GetAllErrors()
162        {
163            foreach (Exception error in Errors)
164            {
165                yield return error;
166            }
167            foreach (Exception error in pipelineExecuter.GetAllErrors())
168            {
169                yield return error;
170            }
171            foreach (IOperation operation in operations)
172            {
173                foreach (Exception exception in operation.GetAllErrors())
174                {
175                    yield return exception;
176                }
177            }
178        }
179    }
180}