private void SetStatisticsInExmDatabase(Job job, MessageItem messageItem, DateTime dateMessageSent, DateTime dateMessageFinished)
        {
            var sleepTime = 1000;

            for (var i = 0; i < RetryCount; i++)
            {
                try
                {
                    var messageRecipients = _recipientManager.GetMessageRecipients(200);
                    var totalCount        = checked ((int)messageRecipients.TotalCount);
                    var num                = messageItem.MessageType == MessageType.Regular ? 1 : 0;
                    var recipientsCount    = num != 0 ? FieldUpdate.Set(totalCount) : null;
                    var includedRecipients = num != 0 ? FieldUpdate.Set(_recipientManager.GetTargetRecipientCountFromIncludeLists()) : null;
                    var excludedRecipients = num != 0 ? FieldUpdate.Set(_recipientManager.GetTargetRecipientCountFromExcludeLists()) : null;
                    var globallyExcluded   = num != 0 ? FieldUpdate.Set(_recipientManager.GetTargetRecipientCountFromGlobalOptOutList()) : null;

                    _ecmDataProvider.SetMessageStatisticData(
                        messageItem.CampaignId.ToGuid(),
                        dateMessageSent,
                        dateMessageFinished,
                        recipientsCount,
                        includedRecipients,
                        excludedRecipients,
                        globallyExcluded, null, null
                        );
                    return;
                }
                catch (Exception)
                {
                    job.Status = $"Setting Statistics in the EXM database (retry {i} of {RetryCount})";
                    Thread.Sleep(sleepTime);
                    sleepTime += 1000;
                }
            }
        }
        private void AdjustEmailStats(MessageItem messageItem, SendingProcessData sendingProcessData,
                                      DateTime dateMessageSent, DateTime dateMessageFinished)
        {
            var deployAnalytics = new DeployAnalytics();

            deployAnalytics.Process(new DispatchNewsletterArgs(messageItem, sendingProcessData));

            messageItem.Source.StartTime = dateMessageSent;
            messageItem.Source.EndTime   = dateMessageFinished;

            var innerItem = messageItem.InnerItem;

            using (new EditContext(innerItem))
            {
                innerItem.RuntimeSettings.ReadOnlyStatistics = true;
                innerItem[FieldIDs.Updated] = DateUtil.ToIsoDate(dateMessageSent);
            }

            var itemUtil     = new ItemUtilExt();
            var campaignItem = itemUtil.GetItem(messageItem.CampaignId);

            using (new EditContext(campaignItem))
            {
                campaignItem["StartDate"] = DateUtil.ToIsoDate(dateMessageSent);
                campaignItem[CampaignclassificationItem.FieldIDs.Channel] =
                    EcmFactory.GetDefaultFactory().Io.EcmSettings.CampaignClassificationChannel;
                campaignItem["EndDate"] = DateUtil.ToIsoDate(dateMessageFinished);
            }

            EcmFactory.GetDefaultFactory()
            .Gateways.EcmDataGateway.SetMessageStatisticData(messageItem.CampaignId.ToGuid(), dateMessageSent,
                                                             dateMessageFinished, FieldUpdate.Set(messageItem.SubscribersIds.Value.Count),
                                                             FieldUpdate.Set(messageItem.SubscribersIncludeCount.Value),
                                                             FieldUpdate.Set(messageItem.SubscribersExcludeCount.Value),
                                                             FieldUpdate.Set(messageItem.SubscribersGlobalOptOutCount.Value));
        }
        private static void SetStatisticsInEXMDatabase(Job job, MessageItem messageItem, DateTime dateMessageSent, DateTime dateMessageFinished)
        {
            var sleepTime = 1000;

            for (var i = 0; i < RETRY_COUNT; i++)
            {
                try
                {
                    EcmFactory.GetDefaultFactory().Gateways.EcmDataGateway.SetMessageStatisticData(messageItem.CampaignId.ToGuid(), dateMessageSent, dateMessageFinished, FieldUpdate.Set(messageItem.SubscribersIds.Value.Count), FieldUpdate.Set(messageItem.SubscribersIncludeCount.Value), FieldUpdate.Set(messageItem.SubscribersExcludeCount.Value), FieldUpdate.Set(messageItem.SubscribersGlobalOptOutCount.Value));
                    return;
                }
                catch (Exception)
                {
                    job.Status = $"Setting Statistics in the EXM database (retry {i} of {RETRY_COUNT})";
                    Thread.Sleep(sleepTime);
                    sleepTime += 1000;
                }
            }
        }