예제 #1
0
 static void SetParameteValues(DoSoReportSchedule schedule, object item, UnitOfWork unitOfWork)
 {
     foreach (var parameter in schedule.QueryParametersCollection.Where(x => x.ExpiredOn == null))
     {
         var parameterFromReport = schedule.ReportDefinition.QueryParametersCollection.Where(x => x.ExpiredOn == null && x.ParameterName == parameter.ParameterName);
         var parameterValue      = new ExpressionEvaluator(unitOfWork.GetProperties(unitOfWork.GetClassInfo(item)), parameter.ParameterValueExression).Evaluate(item);
         parameterFromReport.FirstOrDefault().ParameterValue = parameterValue?.ToString();
     }
 }
예제 #2
0
        public static DoSoEmail GenerateEmail(UnitOfWork unitOfWork, object item, string messageSubject, string to, string cc, string messageBody, DoSoReportSchedule schedule = null)
        {
            var classInfo  = unitOfWork.GetClassInfo(item);
            var properties = unitOfWork.GetProperties(classInfo);
            var mailTo     = new ExpressionEvaluator(properties, to).Evaluate(item)?.ToString();


            if (string.IsNullOrEmpty(mailTo))
            {
                return(null);
            }

            var subject = new ExpressionEvaluator(properties, messageSubject).Evaluate(item)?.ToString();
            var body    = new ExpressionEvaluator(properties, messageBody).Evaluate(item)?.ToString();

            var key = classInfo.KeyProperty.GetValue(item).ToString();

            var sameMailFromDb = unitOfWork.Query <DoSoEmail>().Where(x =>
                                                                      x.ExpiredOn == null &&
                                                                      x.EmailTo == mailTo &&
                                                                      x.ObjectKey == key &&
                                                                      x.ObjectTypeName == item.GetType().FullName).ToList();

            var oldNotSentMail = sameMailFromDb.Where(x => !x.IsSent && !x.IsCanceled && x.SendingDate < DateTime.Now && x.DoSoReportSchedule == schedule);

            foreach (var oldMail in oldNotSentMail)
            {
                oldMail.IsCanceled    = true;
                oldMail.StatusComment = "Created New Email";
            }

            //var alredySentMails = sameMailFromDb.FirstOrDefault(x =>
            //                                        x.IsSent &&
            //                                        !x.IsCanceled &&
            //                                        x.SentDate.AddDays(schedule.SkipExecutionDate) > DateTime.Now);


            var email = new DoSoEmail(unitOfWork)
            {
                EmailTo        = mailTo,
                EmailSubject   = subject,
                EmailCC        = cc,
                EmailBody      = body,
                SendingDate    = DateTime.Now,
                ObjectKey      = key,
                ObjectTypeName = item.GetType().FullName,
                //SourceFilePath = path + fullName,
                DoSoReportSchedule = schedule
            };

            var folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GeneratedFiles");

            if (schedule?.ReportDefinition != null)
            {
                //var path = System.IO.Path.GetTempPath();

                if (!Directory.Exists(folderName))
                {
                    Directory.CreateDirectory(folderName);
                }

                var name     = DateTime.Now.ToString("MMMddHHmmssfff");
                var fullName = name + ".xlsx";

                SetParameteValues(schedule, item, unitOfWork);

                //var providerType = (unitOfWork.DataLayer as SimpleDataLayer)?.ConnectionProvider;

                var exported = false;
                //if (providerType is MSSqlConnectionProvider)
                //exported = FastExportingMethod.ExportData2Excel(schedule.ReportDefinition, /*unitOfWork.Connection as SqlConnection*/unitOfWork, folderName, name);
                //else
                //    exported = FastExportingMethod.ExportData2ExcelFromPostgre(schedule.ReportDefinition, unitOfWork.Connection as SqlConnection, folderName, name);

                if (exported)
                {
                    email.SourceFilePath += Path.Combine(folderName, fullName) + ";";
                }
                else
                {
                    email.EmailBody += string.Format("{0}{0}{1}", Environment.NewLine, schedule.AlternativeText);
                }
            }


            if (!string.IsNullOrEmpty(schedule?.AttachedFilesExpression))
            {
                try
                {
                    var data = TryGetFileData(unitOfWork, item as XPBaseObject, schedule.AttachedFilesExpression.Replace("'", ""), 0);
                    if (data != null)
                    {
                        if (!Directory.Exists(folderName))
                        {
                            Directory.CreateDirectory(folderName);
                        }

                        foreach (var fileData in data)
                        {
                            var extention   = Path.GetExtension(fileData.FileName);
                            var name        = DateTime.Now.ToString("ddHHmmssfff");
                            var newFileName = $"{fileData.FileName.Replace(extention, "")}_{name}{extention}";
                            var fullPath    = Path.Combine(folderName, newFileName);
                            using (var stream = new FileStream(fullPath, FileMode.Create))
                            {
                                fileData.SaveToStream(stream);
                                email.SourceFilePath += fullPath + ";";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CreateLogFileWithException(ex.ToString());
                }
            }


            return(email);
        }
예제 #3
0
        public string GenerateEmailFromSchedule(DoSoReportSchedule schedule, UnitOfWork unitOfWork)
        {
            string exception = string.Empty;

            var objects = SenderHelper.GetMyObjects(schedule.ObjectsCriteria, unitOfWork, schedule.BusinessObjectFullName, true, 500);

            foreach (var item in objects)
            {
                try
                {
                    var classInfo  = unitOfWork.GetClassInfo(item);
                    var properties = unitOfWork.GetProperties(classInfo);
                    var mailTo     = new ExpressionEvaluator(properties, schedule.MessageTo).Evaluate(item).With(x => x.ToString());

                    if (string.IsNullOrEmpty(mailTo))
                    {
                        continue;
                    }

                    var subject = new ExpressionEvaluator(properties, schedule.MessageSubject).Evaluate(item).ToString();
                    var body    = new ExpressionEvaluator(properties, schedule.MessageBody).Evaluate(item).ToString();

                    var key = classInfo.KeyProperty.GetValue(item).ToString();

                    var sameMailFromDb = unitOfWork.Query <DoSoEmail>().Where(x =>
                                                                              x.ExpiredOn == null &&
                                                                              x.EmailTo == mailTo &&
                                                                              x.ObjectKey == key &&
                                                                              x.ObjectTypeName == item.GetType().FullName&&
                                                                              x.ReportSchedule == schedule);
                    var oldNotSentMail = sameMailFromDb.Where(x => !x.IsSent && !x.IsCanceled && x.SendingDate < DateTime.Now);
                    foreach (var oldMail in oldNotSentMail)
                    {
                        oldMail.IsCanceled    = true;
                        oldMail.StatusComment = "Created New Email";
                    }

                    //var alredySentMails = sameMailFromDb.FirstOrDefault(x =>
                    //                                        x.IsSent &&
                    //                                        !x.IsCanceled &&
                    //                                        x.SentDate.AddDays(schedule.SkipExecutionDate) > DateTime.Now);

                    var email = new DoSoEmail(unitOfWork)
                    {
                        EmailTo        = mailTo,
                        EmailSubject   = subject,
                        EmailBody      = body,
                        SendingDate    = DateTime.Now,
                        ObjectKey      = key,
                        ObjectTypeName = item.GetType().FullName,
                        //SourceFilePath = path + fullName,
                        ReportSchedule = schedule
                    };

                    if (schedule.ReportDefinition != null)
                    {
                        var path     = System.IO.Path.GetTempPath();
                        var name     = DateTime.Now.ToString("MMMddHHmmssfff");
                        var fullName = name + ".xlsx";

                        SetParameteValues(schedule, item, unitOfWork);
                        var exported = FastExportingMethod.ExportData2Excel(schedule.ReportDefinition, unitOfWork.Connection as SqlConnection, path, name);
                        if (exported)
                        {
                            email.SourceFilePath = path + fullName;
                        }
                        else
                        {
                            email.EmailBody += string.Format("{0}{0}{1}", Environment.NewLine, schedule.AlternativeText);
                        }
                    }

                    schedule.GetNextExecutionDate();
                    unitOfWork.CommitChanges();
                }
                catch (Exception ex)
                {
                    exception += ex + Environment.NewLine;
                    continue;
                }
            }
            return(exception);
        }
예제 #4
0
        //private void Viewwer_ConnectionError(object sender, DevExpress.DataAccess.ConnectionErrorEventArgs e)
        //{
        //    GeneratorHelper.CreateLogFileWithException(Environment.UserInteractive + Environment.NewLine + e.Exception);
        //}

        public bool SendMail(string mailTo, string EmailCC, string path, string subject, string body, ReportData report, object obj, ReportExportFileFormatEnum?fileType, DoSoReportSchedule schedule = null)
        {
            //if (report == null)
            //    return false;
            using (var mail = new MailMessage())
            {
                //var dashboard = schedule?.DashboardDefinition;

                var otherFilePath = string.Empty;
                try
                {
                    if (report != null && obj != null)
                    {
                        otherFilePath = AddAlternateViewWithLinkedResource(mail, report, obj, fileType);
                    }

                    if (!string.IsNullOrEmpty(path))
                    {
                        var paths = path.Split(';');
                        foreach (var s in paths)
                        {
                            if (s.Length < 10)
                            {
                                continue;
                            }
                            var attachment = new Attachment(s);
                            mail.Attachments.Add(attachment);
                        }
                    }

                    mail.From = new MailAddress(MailFrom);

                    var tos = mailTo.Split(';');
                    foreach (var item in tos.Where(x => x.Length > 2))
                    {
                        mail.To.Add(new MailAddress(item.Trim()));
                    }

                    if (!string.IsNullOrEmpty(EmailCC))
                    {
                        var ccs = EmailCC.Split(';');
                        foreach (var item in ccs.Where(x => x.Length > 2))
                        {
                            mail.CC.Add(new MailAddress(item.Trim()));
                        }
                    }

                    mail.Subject = subject;
                    mail.Body   += body;

                    //if (dashboard != null)
                    //    try
                    //    {
                    //        otherFilePath = AddDashboardToMail(mail, dashboard, schedule);
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        throw new InvalidOperationException("Cannot Generate Dashboard" + Environment.NewLine + ex);
                    //    }


                    using (var client = new SmtpClient()
                    {
                        Host = SmtpServer,
                        Port = SmtpPort,
                        EnableSsl = SmtpEnableSsl,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = UseDefaultCredentials,
                        Credentials = new NetworkCredential(SmtpUserName, SmtpPassword),
                        Timeout = 50000
                    })
                    {
                        client.Send(mail);
                    }

                    DisposeResources(mail);
                    DeleteSentFiles(otherFilePath);

                    return(true);
                }
                catch (Exception ex)
                {
                    //if (!string.IsNullOrEmpty(otherFilePath))
                    //     File.Delete(otherFilePath);
                    DisposeResources(mail);
                    GeneratorHelper.CreateLogFileWithException(ex.ToString());
                    return(false);
                }
            }
        }
 public SaveCommandHandler(XRDesignPanel panel, DoSoReportSchedule schedule, IObjectSpace os)
 {
     this.panel    = panel;
     this.schedule = schedule;
     this.os       = os;
 }