public void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; RockContext rockContext = new RockContext(); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); var communications = recurringCommunicationService.Queryable("Schedule").ToList(); int count = 0; foreach (var communication in communications) { var lastExpectedRun = communication.Schedule .GetScheduledStartTimes(RockDateTime.Now.AddDays(-1), RockDateTime.Now) .LastOrDefault(); if (lastExpectedRun != null && lastExpectedRun > DateTime.MinValue) { if (communication.LastRunDateTime == null || communication.LastRunDateTime <= lastExpectedRun) { communication.LastRunDateTime = RockDateTime.Now; rockContext.SaveChanges(); EnqueRecurringCommuniation(communication.Id); count++; } } } context.Result = string.Format("Sent {0} communication{1}", count, count == 1 ? "" : "s"); }
private void BindGrid() { RockContext rockContext = new RockContext(); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); gRC.DataSource = recurringCommunicationService.Queryable().ToList(); gRC.DataBind(); }
protected void btnDelete_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e) { var rcId = ( int )e.RowKeyValue; RockContext rockContext = new RockContext(); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); var recurringCommunication = recurringCommunicationService.Get(rcId); if (recurringCommunication != null) { recurringCommunicationService.Delete(recurringCommunication); rockContext.SaveChanges(); } BindGrid(); }
private RecurringCommunication GetRecurringCommunication(RecurringCommunicationService recurringCommunicationService) { var rucurringCommunicationId = PageParameter(PageParameterKey.RecurringCommunicationId).AsInteger(); var recurringCommunication = recurringCommunicationService.Get(rucurringCommunicationId); if (recurringCommunication == null) { recurringCommunication = new RecurringCommunication { CommunicationType = CommunicationType.Email, EmailBody = "{{ 'Global' | Attribute:'EmailHeader' }}\n<br>\n<br>\n{{ 'Global' | Attribute:'EmailFooter' }}", }; } return(recurringCommunication); }
public void Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; RockContext rockContext = new RockContext(); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); var communications = recurringCommunicationService.Queryable("Schedule").ToList(); int count = 0; var errors = new List <RecurringCommunication>(); foreach (var communication in communications) { var lastExpectedRun = communication.Schedule .GetScheduledStartTimes(RockDateTime.Now.AddDays(-1), RockDateTime.Now) .LastOrDefault(); if (lastExpectedRun != null && lastExpectedRun > DateTime.MinValue) { if (communication.LastRunDateTime == null || communication.LastRunDateTime <= lastExpectedRun) { communication.LastRunDateTime = RockDateTime.Now; rockContext.SaveChanges(); try { EnqueRecurringCommunication(communication.Id); } catch (Exception e) { ExceptionLogService.LogException(new Exception($"Error Sending Recurring Communication: ID {communication.Id}", e)); errors.Add(communication); } count++; } } } var errorText = ""; if (errors.Any()) { errorText = "There were errors with the following Recurring Communications: "; errorText += string.Join(", ", errors.Select(c => $"{c.Id}:{c.Name}")); } context.Result = $"Sent {count} communication{( count == 1 ? "" : "s" )} {errorText}"; }
protected void btnSave_Click(object sender, EventArgs e) { var rockContext = new RockContext(); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); var recurringCommunication = GetRecurringCommunication(recurringCommunicationService); if (recurringCommunication.Schedule == null) { recurringCommunication.Schedule = new Schedule(); ScheduleService scheduleService = new ScheduleService(rockContext); scheduleService.Add(recurringCommunication.Schedule); } recurringCommunication.Name = tbName.Text; recurringCommunication.DataViewId = dvpDataview.SelectedValue.AsInteger(); recurringCommunication.Schedule.iCalendarContent = sbScheduleBuilder.iCalendarContent; recurringCommunication.CommunicationType = rblCommunicationType.SelectedValueAsEnum <CommunicationType>(); recurringCommunication.FromName = tbFromName.Text; recurringCommunication.FromEmail = tbFromEmail.Text; recurringCommunication.Subject = tbSubject.Text; recurringCommunication.EmailBody = ceEmailBody.Text; recurringCommunication.PhoneNumberValueId = dvpPhoneNumber.SelectedDefinedValueId > 0 ? dvpPhoneNumber.SelectedDefinedValueId : null; recurringCommunication.SMSBody = tbSMSBody.Text; recurringCommunication.PushTitle = tbPushNotificationTitle.Text; recurringCommunication.PushMessage = tbPushNotificationBody.Text; recurringCommunication.PushSound = cbPlaySound.Checked ? "default" : string.Empty; recurringCommunication.TransformationEntityTypeId = ddlTransformTypes.SelectedValue.AsIntegerOrNull(); if (recurringCommunication.Id == 0) { recurringCommunicationService.Add(recurringCommunication); } recurringCommunication.ScheduleDescription = GetScheduleDescription(); rockContext.SaveChanges(); NavigateToParentPage(); }
private void EnqueRecurringCommuniation(int id) { RockContext rockContext = new RockContext(); CommunicationService communicationService = new CommunicationService(rockContext); RecurringCommunicationService recurringCommunicationService = new RecurringCommunicationService(rockContext); var recurringCommunication = recurringCommunicationService.Get(id); if (recurringCommunication == null) { return; } var communication = new Communication(); communication.SenderPersonAlias = recurringCommunication.CreatedByPersonAlias; communication.Name = recurringCommunication.Name; communication.CommunicationType = recurringCommunication.CommunicationType; communication.FromName = recurringCommunication.FromName; communication.FromEmail = recurringCommunication.FromEmail; communication.Subject = recurringCommunication.Subject; communication.Message = recurringCommunication.EmailBody; communication.SMSFromDefinedValueId = recurringCommunication.PhoneNumberValueId; communication.SMSMessage = recurringCommunication.SMSBody; communication.PushTitle = recurringCommunication.PushTitle; communication.PushSound = recurringCommunication.PushSound; communication.PushMessage = recurringCommunication.PushMessage; communication.Status = CommunicationStatus.Approved; DataTransformComponent transform = null; if (recurringCommunication.TransformationEntityTypeId.HasValue) { transform = DataTransformContainer.GetComponent(recurringCommunication.TransformationEntityType.Name); communication.AdditionalMergeFields = new List <string>() { "AppliesTo" }; } communicationService.Add(communication); var emailMediumEntityType = EntityTypeCache.Get(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid()); var smsMediumEntityType = EntityTypeCache.Get(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid()); var pushNotificationMediumEntityType = EntityTypeCache.Get(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_PUSH_NOTIFICATION.AsGuid()); List <string> errorsOut; var dataview = (IQueryable <Person>)recurringCommunication.DataView.GetQuery(null, rockContext, null, out errorsOut); if (transform != null) { var recipients = new List <CommunicationRecipient>(); var personService = new PersonService(rockContext); var paramExpression = personService.ParameterExpression; foreach (Person dvPerson in dataview) { var whereExp = Rock.Reporting.FilterExpressionExtractor.Extract <Rock.Model.Person>(personService.Queryable().Where(p => p.Id == dvPerson.Id), paramExpression, "p"); var transformExp = transform.GetExpression(personService, paramExpression, whereExp); MethodInfo getMethod = personService.GetType().GetMethod("Get", new Type[] { typeof(System.Linq.Expressions.ParameterExpression), typeof(System.Linq.Expressions.Expression) }); if (getMethod != null) { var getResult = getMethod.Invoke(personService, new object[] { paramExpression, transformExp }); var qry = getResult as IQueryable <Person>; foreach (var p in qry.ToList()) { var fieldValues = new Dictionary <string, object>(); fieldValues.Add("AppliesTo", dvPerson); recipients.Add(new CommunicationRecipient() { PersonAlias = p.PrimaryAlias, MediumEntityTypeId = p.CommunicationPreference == CommunicationType.SMS ? smsMediumEntityType.Id : emailMediumEntityType.Id, AdditionalMergeValues = fieldValues }); } } communication.Recipients = recipients; } } else { communication.Recipients = dataview .ToList() .Select(p => new CommunicationRecipient { PersonAlias = p.PrimaryAlias, MediumEntityTypeId = p.CommunicationPreference == CommunicationType.SMS ? smsMediumEntityType.Id : emailMediumEntityType.Id }) .ToList(); } Dictionary <int, CommunicationType?> communicationListGroupMemberCommunicationTypeLookup = new Dictionary <int, CommunicationType?>(); foreach (var recipient in communication.Recipients) { if (communication.CommunicationType == CommunicationType.Email) { recipient.MediumEntityTypeId = emailMediumEntityType.Id; } else if (communication.CommunicationType == CommunicationType.SMS) { recipient.MediumEntityTypeId = smsMediumEntityType.Id; } else if (communication.CommunicationType == CommunicationType.PushNotification) { recipient.MediumEntityTypeId = pushNotificationMediumEntityType.Id; } else if (communication.CommunicationType == CommunicationType.RecipientPreference) { //Do nothing we already defaulted to the recipient's preference } else { throw new Exception("Unexpected CommunicationType: " + communication.CommunicationType.ConvertToString()); } } rockContext.SaveChanges(); var transaction = new Rock.Transactions.SendCommunicationTransaction(); transaction.CommunicationId = communication.Id; transaction.PersonAlias = recurringCommunication.CreatedByPersonAlias; Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction); }