Exemplo n.º 1
0
        private IError PopulateReportContext(ReportTask task, SmtpServer smtpServer, out List<ReportContext> exportedReports, out List<string> missingReports)
        {
            exportedReports = new List<ReportContext>();
            missingReports = new List<string>();

            List<ReportContext> excelReports = new List<ReportContext>();

            string folder = string.Empty;
            if (task.OutputFolder.EndsWith(@"\"))
            {
                folder = task.OutputFolder.Remove(task.OutputFolder.Length - 1);
            }
            else
            {
                folder = task.OutputFolder;
            }
            IError error = new QvError();

            #region Export report
            foreach (var item in task.Reports.Values)
            {
                this.Logger.Message("Running report [" + item.Name + "]");
                this.Logger.Message("Setting connection [" + item.Connection.Name + "]");

                string fileName;

                error = this.ExportReport(item, folder, out fileName);

                if (error.HasError == false)
                {
                    if (item.ReportType != Common.ReportType.Excel)
                        exportedReports.Add(new ReportContext()
                        {
                            Name = item.Name,
                            Description = item.Description,
                            OutputFullName = fileName,
                            ReportType = item.ReportType,
                            HtmlFormat = this.GetHtml(item, fileName)
                        });
                    else
                        excelReports.Add(new ReportContext()
                        {
                            Name = item.Name,
                            Description = item.Description,
                            OutputFullName = fileName,
                            ReportType = item.ReportType,
                            HtmlFormat = this.GetHtml(item, fileName)
                        });
                    this.Logger.Message("Export report [" + item.Name + "] complete");
                }
                else
                {
                    missingReports.Add(fileName);
                    this.Logger.Error(string.Format("The report [{0}] cannot be exported.", fileName));
                }
            }

            if (task.IsMergeInSingleExcel)
            {
                this.Logger.Message("Begin Merge in single excel");
                ReportContext mergedReport;
                string mergedFileName = folder + @"\" + task.Name + ".xls";

                error = this.MergeExcelReports(excelReports, mergedFileName, task.Name, out mergedReport);

                if (error.HasError == false)
                {
                    exportedReports.Add(mergedReport);
                    this.Logger.Message("Succed to merge in single excel");
                }
                else
                {
                    this.Logger.Message("Failed to merge, retain multiple files \n" + error.ErrorMessage);
                    MailHelper.ExceptionNotify("Failed to merge files for task " + task.Name, error.ErrorMessage.ToString(), smtpServer);
                }
            }
            else
            {
                exportedReports.AddRange(excelReports);
            }
            #endregion

            return error;
        }
Exemplo n.º 2
0
        public IError RunTask(ReportTask task, SmtpServer smtpServer)
        {
            IError taskError = new QvError();
            try
            {

                this.Logger.Message("Begin to Run task [" + task.Name + "]------------------------------------------------");

                this.Logger.Message("Preview the connection. ");

                this.ConnectionPreview(task.Reports.Values.First());

                this.Logger.Message(string.Format("Task [{0}] have reports [{1}]", task.Name, string.Join(",", task.Reports.Keys.ToArray())));

                List<ReportContext> reports;
                List<string> missingExportReports;

                IError error = this.PopulateReportContext(task, smtpServer, out reports, out missingExportReports);

                if (error.HasError)
                {
                    taskError.HasError = true;
                    taskError.ErrorMessage.AppendLine(error.ErrorMessage.ToString());
                }
                else
                {
                    #region Send Mail

                    if (smtpServer.Validate() && reports.Count > 0)
                    {
                        SmtpClientAdaptor smtpClient = new SmtpClientAdaptor(smtpServer);

                        //Get the recipients
                        List<string> recipients = task.Recipients.Values.Select(x => x.Email).ToList();
                        if (task.Group != null)
                        {
                            foreach (var r in task.Group.RecipientList.Values)
                            {
                                if (!recipients.Contains(r.Email))
                                    recipients.Add(r.Email);
                            }
                        }

                        if (recipients.Count > 0)
                        {

                            if (task.IsSendMailInSingleMail)
                            {
                                smtpClient.SendEmail(task.MessageDefinition, reports, recipients);
                            }
                            else
                            {
                                foreach (var item in reports)
                                {
                                    List<ReportContext> list = new List<ReportContext>();
                                    list.Add(item);
                                    smtpClient.SendEmail(task.MessageDefinition, list, recipients);
                                }
                            }
                        }
                        else
                        {
                            this.Logger.Error("No recipients set up");
                        }
                    }
                    else
                    {
                        this.Logger.Error("SMTP SERVER Validate failed or there is no report exported, can not send email.");
                        this.Logger.Info("Exported reports count " + reports.Count);
                    }

                    #endregion

                    #region Upload to Ftp Server

                    if (task.FtpServer != null)
                    {
                        string remoteFile = string.Empty;
                        try
                        {
                            FtpClient client = new FtpClient(task.FtpServer.Host, task.FtpServer.Username, task.FtpServer.Password, task.FtpServer.Port);

                            foreach (var item in reports)
                            {
                                if (!string.IsNullOrWhiteSpace(task.FtpServer.Folder))
                                    remoteFile = task.FtpServer.Folder + (task.FtpServer.Folder.EndsWith("/") ? string.Empty : "/") + item.OutputFullName.Split("\\".ToArray())[item.OutputFullName.Split("\\".ToArray()).Length - 1];
                                else
                                    remoteFile = item.OutputFullName.Split("\\".ToArray())[item.OutputFullName.Split("\\".ToArray()).Length - 1];
                                client.Upload(remoteFile, item.OutputFullName);
                            }
                        }
                        catch (Exception ex)
                        {
                            this.Logger.Error("Ftp upload failed!" + ex.Message + ex.StackTrace);

                            MailHelper.ExceptionNotify("Ftp upload failed", ex.Message + ex.StackTrace + "\n" + "Remote file:" + remoteFile, smtpServer);
                        }
                    }

                    #endregion
                    if (missingExportReports.Count > 0)
                    {
                        string subject = "Some reports can not be exported";
                        string message = string.Join("\n\r", missingExportReports);
                        MailHelper.ExceptionNotify(subject, message, smtpServer);
                        taskError.HasError = true;
                        taskError.ErrorMessage.AppendLine(message);
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.Error("Failed to run the task [" + task.Name + "] Error:" + ex.Message + "\n"+ ex.StackTrace);

                try
                {
                    if (!string.IsNullOrEmpty(smtpServer.ExceptionTo))
                    {
                        string subject = "QlikView Report Export&MailSend system Exception";
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Task name: " + task.Name);
                        sb.AppendLine(string.Empty);
                        sb.AppendLine("Exception message: " + ex.Message);
                        sb.AppendLine("Stack Trace---------------");
                        sb.AppendLine(ex.StackTrace);

                        MailHelper.ExceptionNotify(subject, sb.ToString(), smtpServer);
                        taskError.HasError = true;
                        taskError.ErrorMessage.AppendLine(sb.ToString());
                    }
                }
                catch (Exception ex1)
                {
                    this.Logger.Error("Send exception mail failed. " + ex.Message + "\n" + ex1.StackTrace);
                }
            }

            this.Logger.Message("Complete running the task----------------------------------------------------------");

            return taskError;
        }
Exemplo n.º 3
0
        private void DeleteAllExportedFiles(ReportTask task)
        {
            string folder = string.Empty;
            if (task.OutputFolder.EndsWith(@"\"))
            {
                folder = task.OutputFolder.Remove(task.OutputFolder.Length - 1);
            }
            else
            {
                folder = task.OutputFolder;
            }

            if (File.Exists(folder + @"\DeleteCmd.bat"))
            {
                System.Diagnostics.Process.Start(folder + @"\DeleteCmd.bat");
            }
        }