internal DeliveryFileDownloadOperation(DeliveryFile file, FileDownloadOperation operation)
        {
            this.DeliveryFile = file;

            _innerOperation             = operation;
            _innerOperation.Progressed += new EventHandler <ProgressEventArgs>(_innerOperation_Progressed);
            _innerOperation.Ended      += new EventHandler <EndedEventArgs>(_innerOperation_Ended);
        }
 internal DeliveryFileDownloadOperation(DeliveryFile file, Stream sourceStream, string targetLocation, long length = -1)
     : base(sourceStream, targetLocation, length)
 {
     Init(file, targetLocation);
 }
 internal DeliveryFileDownloadOperation(DeliveryFile file, WebRequest request, string targetLocation, long length = -1)
     : base(request, targetLocation, length)
 {
     Init(file, targetLocation);
 }
 internal DeliveryFileDownloadOperation(DeliveryFile file, string sourceUrl, string targetLocation)
     : base(sourceUrl, targetLocation)
 {
     Init(file, targetLocation);
 }
 private void Init(DeliveryFile file, string targetLocation)
 {
     this.DeliveryFile = file;
     this.SetTargetLocation(targetLocation);
     this.Ended += new EventHandler(this.OnEnded);
 }
        internal static Delivery GetDelivery(Guid deliveryID, bool deep = true, SqlConnection connection = null)
        {
            Delivery delivery        = null;
            bool     innerConnection = connection == null;

            if (innerConnection)
            {
                connection = DeliveryDBClient.Connect();
            }

            try
            {
                SqlCommand cmd = DataManager.CreateCommand(String.Format("{0}(@deliveryID:Char, @deep:bit)", AppSettings.Get(typeof(DeliveryDB), Const.SP_DeliveryGet)), System.Data.CommandType.StoredProcedure);
                cmd.Connection = connection;
                cmd.Parameters["@deliveryID"].Value = deliveryID.ToString("N");
                cmd.Parameters["@deep"].Value       = deep;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        #region Delivery
                        // ..................

                        delivery = new Delivery(reader.Convert <string, Guid>("DeliveryID", s => Guid.Parse(s)))
                        {
                            FullyLoaded = deep,
                            Account     = reader.Convert <int?, Account>("Account_ID", id => id.HasValue ? new Account()
                            {
                                ID         = id.Value,
                                OriginalID = reader.Get <string>("Account_OriginalID"),
                            } : null),
                            Channel = reader.Convert <int?, Channel>("ChannelID", id => id.HasValue ? new Channel()
                            {
                                ID = id.Value
                            } : null),
                            DateCreated   = reader.Get <DateTime>("DateCreated"),
                            DateModified  = reader.Get <DateTime>("DateModified"),
                            Description   = reader.Get <string>("Description"),
                            FileDirectory = reader.Get <string>("FileDirectory"),
                        };

                        delivery.InternalSetTimePeriod(
                            DateTimeRange.Parse(reader.Get <string>("TimePeriodDefinition")),
                            reader.Get <DateTime>("TimePeriodStart"),
                            reader.Get <DateTime>("TimePeriodEnd")
                            );

                        // ..................
                        #endregion

                        if (deep)
                        {
                            #region DeliveryParameters
                            // ..................

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    delivery.Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                }
                            }
                            // ..................
                            #endregion

                            #region DeliveryFile
                            // ..................

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = new DeliveryFile();
                                    deliveryFile.FileID          = reader.Convert <string, Guid>("DeliveryID", s => Guid.Parse(s));
                                    deliveryFile.FileCompression = reader.Get <FileCompression>("FileCompression");
                                    deliveryFile.SourceUrl       = reader.Get <string>("SourceUrl");
                                    deliveryFile.Name            = reader.Get <string>("Name");
                                    deliveryFile.Location        = reader.Get <string>("Location");
                                    deliveryFile.Status          = reader.Get <DeliveryFileStatus>("Status");
                                    deliveryFile.FileSignature   = reader.Get <string>("FileSignature");
                                    delivery.Files.Add(deliveryFile);
                                }
                            }
                            // ..................
                            #endregion

                            #region DeliveryFileParameters
                            // ..................

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = delivery.Files[reader.Get <string>("Name")];
                                    deliveryFile.Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                }
                            }

                            // ..................
                            #endregion

                            #region DeliveryOutput

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    var deliveryOutput = new DeliveryOutput()
                                    {
                                        OutputID = reader.Convert <string, Guid>("OutputID", s => Guid.Parse(s)),
                                        Account  = reader.Convert <int?, Account>("AccountID", id => id.HasValue ? new Account()
                                        {
                                            ID         = id.Value,
                                            OriginalID = reader.Get <string>("AccountOriginalID")
                                        } : null),
                                        Channel = reader.Convert <int?, Channel>("ChannelID", id => id.HasValue ? new Channel()
                                        {
                                            ID = id.Value
                                        } : null),
                                        Signature          = reader.Get <string>("Signature"),
                                        Status             = reader.Get <DeliveryOutputStatus>("Status"),
                                        TimePeriodStart    = reader.Get <DateTime>("TimePeriodStart"),
                                        TimePeriodEnd      = reader.Get <DateTime>("TimePeriodEnd"),
                                        PipelineInstanceID = reader.Get <long>("PipelineInstanceID")
                                    };
                                    delivery.Outputs.Add(deliveryOutput);
                                }
                            }

                            #endregion

                            #region DeliveryOutputParameters
                            // ..................

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryOutput deliveryOutput = delivery.Outputs[reader.Get <string>("OutputID")];
                                    deliveryOutput.Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                }
                            }

                            // ..................
                            #endregion

                            #region checksum
                            // ..................

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    var deliveryOutput = delivery.Outputs[reader.Get <string>("OutputID")];
                                    if (deliveryOutput.Checksum == null)
                                    {
                                        deliveryOutput.Checksum = new Dictionary <string, double>();
                                    }
                                    deliveryOutput.Checksum.Add(reader["MeasureName"].ToString(), Convert.ToDouble(reader["Total"]));
                                }
                            }
                            // ..................
                            #endregion
                        }
                    }
                }
            }
            finally
            {
                if (innerConnection)
                {
                    connection.Dispose();
                }
            }

            return(delivery);
        }
        internal static Delivery Get(Guid deliveryID, bool deep = true, SqlConnection connection = null)
        {
            Delivery delivery        = null;
            bool     innerConnection = connection == null;

            if (innerConnection)
            {
                connection = DeliveryDBClient.Connect();
            }

            try
            {
                SqlCommand cmd = DataManager.CreateCommand("Delivery_Get(@deliveryID:Char, @deep:bit)", System.Data.CommandType.StoredProcedure);
                cmd.Connection = connection;
                cmd.Parameters["@deliveryID"].Value = deliveryID.ToString("N");
                cmd.Parameters["@deep"].Value       = deep;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        #region Delivery

                        delivery = new Delivery(reader.Convert <string, Guid>("DeliveryID", s => Guid.Parse(s)))
                        {
                            FullyLoaded = deep
                        };

                        delivery.Account = reader.Convert <int?, Account>("AccountID", id => id.HasValue ? new Account()
                        {
                            ID = id.Value
                        } : null);
                        delivery.Channel = reader.Convert <int?, Channel>("ChannelID", id => id.HasValue ? new Channel()
                        {
                            ID = id.Value
                        } : null);
                        delivery.Account.OriginalID      = reader.Get <string>("OriginalID");
                        delivery.DateCreated             = reader.Get <DateTime>("DateCreated");
                        delivery.DateModified            = reader.Get <DateTime>("DateModified");
                        delivery.Signature               = reader.Get <string>("Signature");
                        delivery.Description             = reader.Get <string>("Description");
                        delivery.TargetLocationDirectory = reader.Get <string>("TargetLocationDirectory");
                        delivery.InternalSetTargetPeriod(
                            DateTimeRange.Parse(reader.Get <string>("TargetPeriodDefinition")),
                            reader.Get <DateTime>("TargetPeriodStart"),
                            reader.Get <DateTime>("TargetPeriodEnd")
                            );
                        delivery.IsCommited = Convert.ToBoolean(reader["Committed"]);

                        #endregion
                        if (deep)
                        {
                            #region DeliveryParameters

                            if (deep)
                            {
                                if (reader.NextResult())
                                {
                                    while (reader.Read())
                                    {
                                        delivery.Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                    }
                                }
                            }
                            #endregion
                            #region DeliveryHistory

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    delivery.History.Add(new DeliveryHistoryEntry((DeliveryOperation)reader["Operation"], reader.Get <long>("ServiceInstanceID"), new Dictionary <string, object>()));
                                }
                            }
                            #endregion
                            #region DeliveryHistoryParameters
                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    delivery.History[reader.Get <int>("Index")].Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                }
                            }
                            #endregion
                            #region DeliveryFile

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = new DeliveryFile();
                                    deliveryFile.Account = reader.Convert <int?, Account>("AccountID", id => id.HasValue ? new Account()
                                    {
                                        ID = id.Value
                                    } : null);
                                    deliveryFile.FileID     = reader.Convert <string, Guid>("DeliveryID", s => Guid.Parse(s));
                                    deliveryFile.FileFormat = (FileCompression)reader["FileCompression"];
                                    deliveryFile.SourceUrl  = reader.Get <string>("SourceUrl");
                                    deliveryFile.Name       = reader.Get <string>("Name");
                                    deliveryFile.Location   = reader.Get <string>("Location");
                                    delivery.Files.Add(deliveryFile);
                                }
                            }
                            #endregion
                            #region DeliveryFileParameters

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = delivery.Files[reader.Get <string>("Name")];
                                    deliveryFile.Parameters.Add(reader.Get <string>("Key"), DeserializeJson(reader.Get <string>("Value")));
                                }
                            }
                            #endregion
                            #region DeliveryFileHistory

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = delivery.Files[reader["Name"].ToString()];
                                    deliveryFile.History.Add(new DeliveryHistoryEntry((DeliveryOperation)reader["Operation"], Convert.ToInt64(reader["ServiceInstanceID"])));
                                }
                            }
                            #endregion
                            #region DeliveryFileHistoryParameters

                            if (reader.NextResult())
                            {
                                while (reader.Read())
                                {
                                    DeliveryFile deliveryFile = delivery.Files[reader["Name"].ToString()];

                                    deliveryFile.History[reader.Get <int>("Index")].Parameters.Add(reader["Key"].ToString(), reader.Get <object>("Value"));
                                }
                            }
                            #endregion
                        }
                    }
                }
            }
            finally
            {
                if (innerConnection)
                {
                    connection.Dispose();
                }
            }

            /*
             #if DEBUG
             *          Log.Write(String.Format("Delivery - {3}found: {0} (activate: {2}, {1} results)\n", deliveryID, resultCount, activate, delivery == null ? "not " : "" ), LogMessageType.Information);
             *
             #endif
             */
            return(delivery);
        }