Пример #1
0
 public static void Patch(this ExportPushNotification target, ExportProgressInfo source)
 {
     target.Description    = source.Description;
     target.Errors         = source.Errors;
     target.ProcessedCount = source.ProcessedCount;
     target.TotalCount     = source.TotalCount;
 }
 public static void Patch(this ExportPushNotification target, ExportProgressInfo source)
 {
     target.Description          = source.Description;
     target.ProcessedCount       = source.ProcessedCount;
     target.TotalCount           = source.TotalCount;
     target.ContactsFileUrl      = source.ContactsFileUrl;
     target.OrganizationsFileUrl = source.OrganizationsFileUrl;
 }
        public void Export(Stream stream, ExportDataRequest request, Action <ExportProgressInfo> progressCallback, ICancellationToken token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            token.ThrowIfCancellationRequested();

            var exportedTypeDefinition = _exportTypesResolver.ResolveExportedTypeDefinition(request.ExportTypeName);
            var pagedDataSource        = (exportedTypeDefinition.DataSourceFactory ?? throw new ArgumentNullException(nameof(ExportedTypeDefinition.DataSourceFactory))).Create(request.DataQuery);

            var completedMessage = $"Export completed";
            var totalCount       = pagedDataSource.GetTotalCount();
            var exportedCount    = 0;
            var exportProgress   = new ExportProgressInfo()
            {
                ProcessedCount = 0,
                TotalCount     = totalCount,
                Description    = "Export has started",
            };

            progressCallback(exportProgress);

            try
            {
                exportProgress.Description = "Creating provider…";
                progressCallback(exportProgress);

                using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)
                {
                    AutoFlush = true
                })
                    using (var exportProvider = _exportProviderFactory.CreateProvider(request))
                    {
                        var needTabularData = exportProvider.IsTabular;

                        if (needTabularData && !exportedTypeDefinition.IsTabularExportSupported)
                        {
                            throw new NotSupportedException($"Provider \"{exportProvider.TypeName}\" does not support tabular export.");
                        }

                        exportProgress.Description = "Fetching…";
                        progressCallback(exportProgress);

                        while (pagedDataSource.Fetch())
                        {
                            token.ThrowIfCancellationRequested();

                            var objectBatch = pagedDataSource.Items;

                            foreach (var obj in objectBatch)
                            {
                                try
                                {
                                    var preparedObject = obj.Clone() as IExportable;

                                    request.DataQuery.FilterProperties(preparedObject);

                                    if (needTabularData)
                                    {
                                        preparedObject = (preparedObject as ITabularConvertible)?.ToTabular() ??
                                                         throw new NotSupportedException($"Object should be {nameof(ITabularConvertible)} to be exported using tabular provider.");
                                    }

                                    exportProvider.WriteRecord(writer, preparedObject);
                                }
                                catch (Exception e)
                                {
                                    exportProgress.Errors.Add(e.Message);
                                    progressCallback(exportProgress);
                                }
                                exportedCount++;
                            }

                            exportProgress.ProcessedCount = exportedCount;

                            if (exportedCount != totalCount)
                            {
                                exportProgress.Description = $"{exportedCount} out of {totalCount} have been exported.";
                                progressCallback(exportProgress);
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                exportProgress.Errors.Add(e.Message);
            }
            finally
            {
                if (exportProgress.Errors.Count > 0)
                {
                    completedMessage = $"Export completed with errors";
                }

                exportProgress.Description = $"{completedMessage}: {exportedCount} out of {totalCount} have been exported.";
                progressCallback(exportProgress);
            }
        }
 private void ProgressCallback(ExportProgressInfo x, ExportPushNotification pushNotification, PerformContext context)
 {
     pushNotification.Patch(x);
     pushNotification.JobId = context.BackgroundJob.Id;
     _pushNotificationManager.Send(pushNotification);
 }
        public async Task ExportAsync(ExportDataRequest request, Action <ExportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var exportProgress = new ExportProgressInfo {
                ProcessedCount = 0, Description = "Export has started"
            };

            var dataSource = _customerExportPagedDataSourceFactory.Create(ModuleConstants.Settings.PageSize, request);

            exportProgress.TotalCount = await dataSource.GetTotalCountAsync();

            progressCallback(exportProgress);

            const string exportDescription = "{0} out of {1} have been exported.";

            exportProgress.Description = "Fetching...";
            progressCallback(exportProgress);

            var dynamicProperties = await _dynamicPropertySearchService.SearchDynamicPropertiesAsync(new DynamicPropertySearchCriteria()
            {
                ObjectTypes = new List <string> {
                    typeof(Contact).FullName, typeof(Organization).FullName
                },
                Skip = 0,
                Take = int.MaxValue
            });

            var contactsDynamicProperties =
                dynamicProperties.Results.Where(x => x.ObjectType == typeof(Contact).FullName).ToArray();

            var organizationsDynamicProperties =
                dynamicProperties.Results.Where(x => x.ObjectType == typeof(Organization).FullName).ToArray();

            var contactsFilePath    = GetExportFilePath("Contacts");
            var contactExportWriter = _exportWriterFactory.Create <ExportableContact>(contactsFilePath, new ExportConfiguration(), contactsDynamicProperties);

            var organizationFilePath     = GetExportFilePath("Organizations");
            var organizationExportWriter = _exportWriterFactory.Create <ExportableOrganization>(organizationFilePath, new ExportConfiguration(), organizationsDynamicProperties);

            try
            {
                while (await dataSource.FetchAsync())
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var contacts = dataSource.Items.OfType <ExportableContact>().ToArray();

                    if (!contacts.IsNullOrEmpty())
                    {
                        contactExportWriter.WriteRecords(contacts);
                    }

                    var organizations = dataSource.Items.OfType <ExportableOrganization>().ToArray();

                    if (!organizations.IsNullOrEmpty())
                    {
                        organizationExportWriter.WriteRecords(organizations);
                    }

                    exportProgress.ProcessedCount += dataSource.Items.Length;
                    exportProgress.Description     = string.Format(exportDescription, exportProgress.ProcessedCount,
                                                                   exportProgress.TotalCount);
                    progressCallback(exportProgress);
                }

                exportProgress.Description = "Export completed";
            }
            finally
            {
                contactExportWriter.Dispose();
                organizationExportWriter.Dispose();
            }

            try
            {
                var contactsFileInfo = await _blobStorageProvider.GetBlobInfoAsync(contactsFilePath);

                var organizationsFileInfo = await _blobStorageProvider.GetBlobInfoAsync(organizationFilePath);

                if (contactsFileInfo.Size > 0)
                {
                    exportProgress.ContactsFileUrl = _blobUrlResolver.GetAbsoluteUrl(contactsFilePath);
                }
                else
                {
                    await _blobStorageProvider.RemoveAsync(new string[] { contactsFilePath });
                }

                if (organizationsFileInfo.Size > 0)
                {
                    exportProgress.OrganizationsFileUrl = _blobUrlResolver.GetAbsoluteUrl(organizationFilePath);
                }
                else
                {
                    await _blobStorageProvider.RemoveAsync(new string[] { organizationFilePath });
                }
            }
            finally
            {
                progressCallback(exportProgress);
            }
        }