public void OnCallBack(Timer timer) { if (executing) { if (SmsGeneratorTimeOut.IsNull(0) > 0) { timer.Change(SmsGeneratorTimeOut, Timeout.Infinite); } return; } executing = true; using (UnitOfWork unitOfWork = new UnitOfWork(new SimpleDataLayer(SenderHelper.GetConnection()))) { if (SmsGeneratorTimeOut.IsNull(0) == 0) { SmsGeneratorTimeOut = Convert.ToInt32(ConfigurationStatic.GetParameterValue(@"DoSoSmsGeneratorTimeOut", unitOfWork)); } string exception = string.Empty; try { var schedules = unitOfWork.Query <DoSoSmsSchedule>().Where(x => x.NextExecutionDate < DateTime.Now && x.IsActive && x.ExpiredOn == null); foreach (var schedule in schedules) { exception += GenerateSmsFromSchedule(schedule, unitOfWork); } } catch (Exception ex) { SenderHelper.CreateLogFileWithException(ex.ToString()); } if (!string.IsNullOrWhiteSpace(exception)) { SenderHelper.CreateLogFileWithException(exception); } if (SmsGeneratorTimeOut.IsNull(0) > 0) { timer.Change(SmsGeneratorTimeOut, Timeout.Infinite); } executing = false; } }
public string GenerateSmsFromSchedule(DoSoSmsSchedule schedule, UnitOfWork unitOfWork) { var objects = SenderHelper.GetMyObjects(schedule.ObjectsCriteria, unitOfWork, schedule.BusinessObjectFullName, false, 10000); string exception = string.Empty; foreach (var item in objects) { try { var classInfo = unitOfWork.GetClassInfo(item); var properties = unitOfWork.GetProperties(classInfo); var smsTo = new ExpressionEvaluator(properties, schedule.SmsTo).Evaluate(item).With(x => x.ToString()); if (string.IsNullOrEmpty(smsTo)) { continue; } var smsText = new ExpressionEvaluator(properties, schedule.SmsText).Evaluate(item).With(x => x.ToString()); if (!schedule.AllowUnicodeText) { smsText = SenderHelper.ChangeGeorgianText(smsText); } var key = classInfo.With(x => x.KeyProperty).With(x => x.GetValue(item)).With(x => x.ToString()); var objectTypeName = item.With(x => x.GetType()).With(x => x.FullName); var sameSmsFromDb = unitOfWork.Query <DoSoSms>().Where(x => x.SmsTo == smsTo && x.ExpiredOn == null && x.ObjectKey == key && x.ObjectTypeName == objectTypeName && x.DoSoSmsSchedule == schedule); var oldNotSentSms = sameSmsFromDb.Where(x => !x.IsSent && !x.IsCanceled && x.SendingDate < DateTime.Now); foreach (var oldSms in oldNotSentSms) { oldSms.IsCanceled = true; oldSms.StatusComment = "შეიქმნა ახალი SMS"; } var alredySentSms = sameSmsFromDb.FirstOrDefault(x => x.IsSent && !x.IsCanceled && x.SentDate.AddDays(schedule.SkipExecutionDate) > DateTime.Now); if (alredySentSms != null) { continue; } var sms = new DoSoSms(unitOfWork) { SmsTo = smsTo.With(x => x.Trim()).With(x => x.Replace(" ", "")).Replace("-", "").Replace("_", ""), SmsText = smsText, ObjectKey = key, ObjectTypeName = objectTypeName, DoSoSmsSchedule = schedule, SendingDate = DateTime.Now }; if (!Regex.IsMatch(sms.SmsTo, @"\d")) { sms.IsCanceled = true; sms.StatusComment = "ტელეფონის ნომერი არასწორ ფორმატშია"; } schedule.GetNextExecutionDate(); unitOfWork.CommitChanges(); } catch (Exception ex) { exception += ex + Environment.NewLine; continue; } } return(exception); }
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); }