示例#1
0
        /// <summary>
        /// Deletes the pending removal project files.
        /// </summary>
        /// <param name="projectId">The project identifier.</param>
        /// <returns>Is success.</returns>
        private bool DeletePendingRemovalProjectFiles(int projectId)
        {
            CompanyBL companyBL  = new CompanyBL(DataContext);
            string    baseFolder = companyBL.GetExportFileLocation(Common.Constants.GlobalConstants.RelatedTables.ExportFiles.Project, projectId);

            if (baseFolder.Trim().Length > 0)
            {
                FileHandler.DeleteFile(string.Concat(baseFolder, ".zip"));
                return(true);
            }

            return(false);
        }
示例#2
0
        /// <summary>
        /// Deletes the old exported zip files.
        /// </summary>
        public void DeleteOldExportedZipFiles()
        {
            CompanyBL companyBL = new CompanyBL(DataContext);
            var       queuedExportFileRequests = companyBL.GetGeneratedOldExportFiles();
            string    baseFolder = string.Empty;

            foreach (ExportFile exportFileRequest in queuedExportFileRequests)
            {
                baseFolder = companyBL.GetExportFileLocation(exportFileRequest.RelatedTable, exportFileRequest.RelatedId);

                if (baseFolder.Trim().Length > 0)
                {
                    FileHandler.DeleteFile(string.Concat(baseFolder, ".zip"));
                    exportFileRequest.IsActive            = false;
                    exportFileRequest.LastUpdatedByUserId = 0;
                    exportFileRequest.LastUpdatedDate     = Utils.Now;
                }
            }

            DataContext.SaveChanges();//Should not be placed with in the foreach
        }
示例#3
0
        /// <summary>
        /// Creates the project export file.
        /// </summary>
        /// <param name="projectId">The project identifier.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="fileSize">Size of the file.</param>
        /// <returns>Is success.</returns>
        private bool CreateProjectExportFile(int projectId, int userId, out long fileSize)
        {
            ProjectBL projectBL  = new ProjectBL(DataContext);
            CompanyBL companyBL  = new CompanyBL(DataContext);
            string    baseFolder = companyBL.GetExportFileLocation(Common.Constants.GlobalConstants.RelatedTables.ExportFiles.Project, projectId);

            if (baseFolder.Trim().Length > 0)
            {
                FileHandler.CreateFolder(baseFolder);
                Project project = projectBL.GetProject(projectId);
                try
                {
                    // Intialize Cancellation Token.
                    CancellationTokenSource cts = new CancellationTokenSource();
                    ParallelOptions         po  = new ParallelOptions();
                    po.CancellationToken = cts.Token;

                    // Create new task (thread) for create project attachments.
                    var projectAttachmentTask = Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            var documentMediaIds = projectBL.GetProjectDocumentMediaIds(projectId);

                            // Check for the cancel request. (after starting the task).
                            cts.Token.ThrowIfCancellationRequested();

                            var projectAttachmentPath = Path.Combine(baseFolder, "Project Details Attachments");
                            FileHandler.CreateFolder(projectAttachmentPath);

                            // Generate attachments.
                            // single threaded.
                            // Due to the fact that this is an heavy IO bound operation which is recomended to do async.
                            List <Task> asyncTaskList = new List <Task>();
                            foreach (int documentMediaId in documentMediaIds)
                            {
                                // Check for task cancelation request.
                                cts.Token.ThrowIfCancellationRequested();
                                asyncTaskList.Add(SaveDocumentMedia(string.Empty, documentMediaId, projectAttachmentPath));
                            }

                            // Wait for all async taks.
                            Task.WaitAll(asyncTaskList.ToArray());
                        }
                        catch (Exception ex)
                        {
                            // Request cancel for all other pending tasks. And throw the exception.
                            cts.Cancel();
                            throw ex;
                        }
                    }, cts.Token);

                    // Create new task (thread) for create Budget Summary Report.
                    var budgetSummaryReportTask = Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            GenerateProjectBudgetSummaryReport(project, userId, baseFolder);
                        }
                        catch (Exception ex)
                        {
                            // Request cancel for all other pending tasks. And throw the exception.
                            cts.Cancel();
                            throw ex;
                        }
                    }, cts.Token);

                    // Create new task (thread) for create item brief attachments.
                    var ItemBriefDocumentMediaTask = Task.Factory.StartNew(() =>
                    {
                        var groupedDocumentMedias = projectBL.GetItemTypeDocumentMediaByProject(projectId);

                        // Check for the cancel request. (after starting the task).
                        cts.Token.ThrowIfCancellationRequested();

                        // Loop all item types for the company.
                        Parallel.ForEach <ItemTypeDocumentMedia>(groupedDocumentMedias, po, dm =>
                        {
                            try
                            {
                                string itemTypePath   = Path.Combine(baseFolder, dm.ItemTypeName);
                                string attachmentPath = Path.Combine(itemTypePath, "Attachments");
                                FileHandler.CreateFolder(attachmentPath);

                                // Handle report generation to separate task.
                                Task itemBriefReportTask = Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        GenerateItemBriefExportFile(projectId, baseFolder, dm);
                                        GenerateItemisedPurchaseReport(project, dm.ItemTypeName, dm.ItemTypeId, userId, itemTypePath);
                                        GenerateActiveTaskReport(project, dm.ItemTypeName, dm.ItemTypeId, userId, itemTypePath);
                                    }
                                    catch (Exception ex)
                                    {
                                        // Request cancel for all other pending tasks. And throw the exception.
                                        cts.Cancel();
                                        throw ex;
                                    }
                                }, cts.Token);

                                // Generate attachments.
                                // single threaded.
                                // Due to the fact that this is an heavy IO bound operation which is recomended to do async.
                                List <Task> asyncTaskList = new List <Task>();
                                foreach (DocumentMediaInfo documentMediaInfo in dm.DocumentMedias)
                                {
                                    // Check for task cancelation request.
                                    cts.Token.ThrowIfCancellationRequested();
                                    string filePrefix = string.Format("{0} {1} - ", documentMediaInfo.EntityId, Utils.Ellipsize(documentMediaInfo.EntityName, 50));
                                    asyncTaskList.Add(SaveDocumentMedia(filePrefix, documentMediaInfo.DocumentMediaId, attachmentPath));
                                }

                                // Wait for all async taks.
                                Task.WaitAll(asyncTaskList.ToArray());

                                // Wait for report task.
                                itemBriefReportTask.Wait();
                            }
                            catch (Exception ex)
                            {
                                // Request cancel for all other pending tasks. And throw the exception.
                                cts.Cancel();
                                throw ex;
                            }
                        });
                    }, cts.Token);

                    Task[] tasks = new[] { projectAttachmentTask, budgetSummaryReportTask, ItemBriefDocumentMediaTask };
                    try
                    {
                        Task.WaitAll(tasks, cts.Token);
                        string zipPath = string.Concat(baseFolder, ".zip");
                        FileHandler.CreateZipFile(baseFolder, zipPath);
                        fileSize = FileHandler.GetFileSize(zipPath);
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        // One or more operations has been canceled. Wait for other running tasks to complete. (Any status => success or failed).
                        Task.Factory.ContinueWhenAll(tasks, _ =>
                        {
                            // Get exceptions from failed tasks.
                            Exception[] exceptions = tasks.Where(t => t.IsFaulted).Select(t => t.Exception).ToArray();
                            foreach (Exception e in exceptions)
                            {
                                // log failures.
                                AgentErrorLog.HandleException(e);
                            }
                        }).Wait();
                    }
                }
                catch (AggregateException ae)
                {
                    foreach (Exception e in ae.Flatten().InnerExceptions)
                    {
                        // log failures.
                        AgentErrorLog.HandleException(e);
                    }
                }
                finally
                {
                    FileHandler.DeleteFolder(baseFolder);
                }
            }

            fileSize = 0;
            return(false);
        }
示例#4
0
        /// <summary>
        /// Creates the company export file.
        /// </summary>
        /// <param name="companyId">The company identifier.</param>
        /// <param name="userId">The user identifier.</param>
        /// <param name="fileSize">Size of the file.</param>
        /// <returns>Is success.</returns>
        private bool CreateCompanyExportFile(int companyId, int userId, out long fileSize)
        {
            CompanyBL companyBL  = new CompanyBL(DataContext);
            string    baseFolder = companyBL.GetExportFileLocation(Common.Constants.GlobalConstants.RelatedTables.ExportFiles.Company, companyId);

            if (baseFolder.Trim().Length > 0)
            {
                try
                {
                    // Intialize Cancellation Token.
                    CancellationTokenSource cts = new CancellationTokenSource();
                    ParallelOptions         po  = new ParallelOptions();
                    po.CancellationToken = cts.Token;

                    string bookingPath = Path.Combine(baseFolder, "Bookings");
                    FileHandler.CreateFolder(bookingPath);

                    // Generate booking reports.(new Task/Thread.)
                    Task bookingReportTask = Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            GenerateCompanyInventoryBookingReports(companyId, userId, bookingPath);
                        }
                        catch (Exception ex)
                        {
                            // Request cancel for all other pending tasks. And throw the exception.
                            cts.Cancel();
                            throw ex;
                        }
                    }, cts.Token);

                    // Generate item type reports and item brief attachements.(new Task/Thread.)
                    Task itemTypeTask = Task.Factory.StartNew(() =>
                    {
                        var groupedDocumentMedias = companyBL.GetItemTypeDocumentMediaByCompany(companyId);

                        // Check for the cancel request. (after starting the task).
                        cts.Token.ThrowIfCancellationRequested();

                        Parallel.ForEach <ItemTypeDocumentMedia>(groupedDocumentMedias, po, dm =>
                        {
                            try
                            {
                                // Create Item List with all Specs report
                                Task itemReportTask = Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        string fileName          = string.Concat(dm.ItemTypeName, " - Item List with all Specs");
                                        string fileNameExtension = GlobalConstants.FileExtensions.ExcelFile;
                                        string encoding;
                                        string mimeType;
                                        string attachmentPathCI = Path.Combine(baseFolder, dm.ItemTypeName);
                                        byte[] reportBytes      = UserWebReportHandler.GenerateInventoryExport(companyId, dm.ItemTypeId, ReportTypes.Excel, out fileNameExtension, out encoding, out mimeType);
                                        FileHandler.SaveFileToDisk(reportBytes, string.Format("{0}.{1}", fileName, fileNameExtension), attachmentPathCI).Wait();
                                    }
                                    catch (Exception ex)
                                    {
                                        // Request cancel for all other pending tasks. And throw the exception.
                                        cts.Cancel();
                                        //throw ex;
                                    }
                                }, cts.Token);

                                string attachmentPath = Path.Combine(baseFolder, dm.ItemTypeName, "Attachments");
                                FileHandler.CreateFolder(attachmentPath);

                                // Generate attachments.
                                // single threaded.
                                // Due to the fact that this is an heavy IO bound operation which is recomended to do async.
                                List <Task> asyncTaskList = new List <Task>();
                                foreach (DocumentMediaInfo documentMediaInfo in dm.DocumentMedias)
                                {
                                    // Check for the cancel request. (after starting the task).
                                    cts.Token.ThrowIfCancellationRequested();

                                    string filePrefix = string.Format("{0} {1} - ", documentMediaInfo.EntityId, Utils.Ellipsize(documentMediaInfo.EntityName, 50));
                                    asyncTaskList.Add(SaveDocumentMedia(filePrefix, documentMediaInfo.DocumentMediaId, attachmentPath));
                                }

                                // Wait for all async taks.
                                Task.WaitAll(asyncTaskList.ToArray());

                                // Wait for report task.
                                itemReportTask.Wait();
                            }
                            catch (Exception ex)
                            {
                                // Request cancel for all other pending tasks. And throw the exception.
                                cts.Cancel();
                                throw ex;
                            }
                        });
                    }, cts.Token);

                    Task[] tasks = new[] { bookingReportTask, itemTypeTask };
                    try
                    {
                        Task.WaitAll(tasks, cts.Token);
                        string zipPath = string.Concat(baseFolder, ".zip");
                        FileHandler.CreateZipFile(baseFolder, zipPath);
                        fileSize = FileHandler.GetFileSize(zipPath);
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        // One or more operations has been canceled. Wait for other running tasks to complete. (Any status => success or failed).
                        Task.Factory.ContinueWhenAll(tasks, _ =>
                        {
                            // Get exceptions from failed tasks.
                            Exception[] exceptions = tasks.Where(t => t.IsFaulted).Select(t => t.Exception).ToArray();
                            foreach (Exception e in exceptions)
                            {
                                // log failures.
                                AgentErrorLog.HandleException(e);
                            }
                        }).Wait();
                    }
                }
                catch (AggregateException ae)
                {
                    foreach (Exception e in ae.Flatten().InnerExceptions)
                    {
                        // log failures.
                        AgentErrorLog.HandleException(e);
                    }
                }
                finally
                {
                    FileHandler.DeleteFolder(baseFolder);
                }
            }

            fileSize = 0;
            return(false);
        }