public bool CanSendReminder(int reminder_id, int account_id) { using (SnCore.Data.Hibernate.Session.OpenConnection()) { ISession session = SnCore.Data.Hibernate.Session.Current; ManagedReminder mr = new ManagedReminder(session, reminder_id); return(mr.CanSend(account_id)); } }
public void RunSystemReminders(ISession session, ManagedSecurityContext sec) { // get reminders IList reminders = session.CreateCriteria(typeof(Reminder)) .Add(Expression.Eq("Enabled", true)) .List(); foreach (Reminder reminder in reminders) { if (IsStopping) { break; } reminder.LastRun = DateTime.UtcNow; reminder.LastRunError = string.Empty; session.Save(reminder); ManagedReminder mr = new ManagedReminder(session, reminder); try { // get the type of the object seeked Type objecttype = Assembly.GetAssembly(typeof(DataObject)) .GetType(reminder.DataObject.Name, true); // todo: currently this works with date-based fields only // anything older than this time DateTime timeboundary = DateTime.UtcNow.AddHours(-reminder.DeltaHours); // find all records matching the property IList objects = session.CreateCriteria(objecttype) .Add(Expression.Le(reminder.DataObjectField, timeboundary)) .List(); // currently only support account identities // the object is either an Account object or has an AccountId property string accountidproperty = (reminder.DataObject.Name == "Account") ? "Id" : "AccountId"; PropertyInfo accountidpropertyinfo = objecttype.GetProperty(accountidproperty); if (accountidpropertyinfo == null) { throw new Exception(string.Format("Object {0} does not have a property {1}.", reminder.DataObject.Name, accountidproperty)); } foreach (object o in objects) { if (IsStopping) { break; } int accountid = (int)accountidpropertyinfo.GetValue(o, null); ReminderEvent reminderevent = (ReminderEvent)session.CreateCriteria(typeof(ReminderEvent)) .Add(Expression.Eq("Reminder.Id", reminder.Id)) .Add(Expression.Eq("Account.Id", accountid)) .UniqueResult(); Account acct = session.Load <Account>(accountid); ManagedAccount ma = new ManagedAccount(session, acct); try { if (reminderevent == null) { reminderevent = new ReminderEvent(); reminderevent.Account = acct; reminderevent.Reminder = reminder; reminderevent.Created = reminderevent.Modified = DateTime.UtcNow; } else { if (reminderevent.Modified >= timeboundary) { // this field was already noticed and event was fired in a prior run continue; } if (!reminder.Recurrent) { // this reminder has already been sent but is not recurrent continue; } reminderevent.Modified = DateTime.UtcNow; } if (!mr.CanSend(acct)) { continue; } ManagedSiteConnector.TrySendAccountEmailMessageUriAsAdmin( session, ma, string.Format("{0}?id={1}", reminder.Url, ma.Id)); session.Save(reminderevent); } catch (ThreadAbortException) { throw; } catch (Exception ex) { EventLogManager.WriteEntry(string.Format("Error sending a reminder at {0} to account id {1}: {2}", reminder.Url, accountid, ex.Message), EventLogEntryType.Warning); } } } catch (ThreadAbortException) { throw; } catch (Exception ex) { reminder.LastRunError = ex.Message; session.Save(reminder); } Thread.Sleep(1000 * InterruptInterval); } }
public void RunSystemReminders(ISession session, ManagedSecurityContext sec) { // get reminders IList reminders = session.CreateCriteria(typeof(Reminder)) .Add(Expression.Eq("Enabled", true)) .List(); foreach (Reminder reminder in reminders) { if (IsStopping) break; reminder.LastRun = DateTime.UtcNow; reminder.LastRunError = string.Empty; session.Save(reminder); ManagedReminder mr = new ManagedReminder(session, reminder); try { // get the type of the object seeked Type objecttype = Assembly.GetAssembly(typeof(DataObject)) .GetType(reminder.DataObject.Name, true); // todo: currently this works with date-based fields only // anything older than this time DateTime timeboundary = DateTime.UtcNow.AddHours(-reminder.DeltaHours); // find all records matching the property IList objects = session.CreateCriteria(objecttype) .Add(Expression.Le(reminder.DataObjectField, timeboundary)) .List(); // currently only support account identities // the object is either an Account object or has an AccountId property string accountidproperty = (reminder.DataObject.Name == "Account") ? "Id" : "AccountId"; PropertyInfo accountidpropertyinfo = objecttype.GetProperty(accountidproperty); if (accountidpropertyinfo == null) { throw new Exception(string.Format("Object {0} does not have a property {1}.", reminder.DataObject.Name, accountidproperty)); } foreach (object o in objects) { if (IsStopping) break; int accountid = (int)accountidpropertyinfo.GetValue(o, null); ReminderEvent reminderevent = (ReminderEvent)session.CreateCriteria(typeof(ReminderEvent)) .Add(Expression.Eq("Reminder.Id", reminder.Id)) .Add(Expression.Eq("Account.Id", accountid)) .UniqueResult(); Account acct = session.Load<Account>(accountid); ManagedAccount ma = new ManagedAccount(session, acct); try { if (reminderevent == null) { reminderevent = new ReminderEvent(); reminderevent.Account = acct; reminderevent.Reminder = reminder; reminderevent.Created = reminderevent.Modified = DateTime.UtcNow; } else { if (reminderevent.Modified >= timeboundary) { // this field was already noticed and event was fired in a prior run continue; } if (!reminder.Recurrent) { // this reminder has already been sent but is not recurrent continue; } reminderevent.Modified = DateTime.UtcNow; } if (!mr.CanSend(acct)) continue; ManagedSiteConnector.TrySendAccountEmailMessageUriAsAdmin( session, ma, string.Format("{0}?id={1}", reminder.Url, ma.Id)); session.Save(reminderevent); } catch (ThreadAbortException) { throw; } catch (Exception ex) { EventLogManager.WriteEntry(string.Format("Error sending a reminder at {0} to account id {1}: {2}", reminder.Url, accountid, ex.Message), EventLogEntryType.Warning); } } } catch (ThreadAbortException) { throw; } catch (Exception ex) { reminder.LastRunError = ex.Message; session.Save(reminder); } Thread.Sleep(1000 * InterruptInterval); } }
public bool CanSendReminder(int reminder_id, int account_id) { using (SnCore.Data.Hibernate.Session.OpenConnection()) { ISession session = SnCore.Data.Hibernate.Session.Current; ManagedReminder mr = new ManagedReminder(session, reminder_id); return mr.CanSend(account_id); } }