private static void LogException(string site, int batchId, Email email, Exception e) { log.Error("Failed to insert the sent info item for: " + email.to, e); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SentInfo sentTo = dataContext.SentInfos.FirstOrDefault(sent => sent.batchId == batchId && sent.subscriptionEmail == email.to && sent.site == site); if (sentTo == null) { sentTo = new SentInfo { batchId = batchId, lastSendDate = DateTime.Now, site = site, status = "failed", subscriptionEmail = email.to }; dataContext.SentInfos.InsertOnSubmit(sentTo); } else { sentTo.status = "failed"; } dataContext.SubmitChanges(); } }
/// <summary> /// Unsubscribes the specified email from the specified site's notifications. /// </summary> /// <param name="email">The email.</param> /// <param name="site">The site.</param> /// <returns></returns> public static Boolean Unsubscribe(string email, string site) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Subscription unSubscription = dataContext.Subscriptions.FirstOrDefault(sub => sub.email == email && sub.site == site); if (unSubscription != null) { unSubscription.optedIn = false; dataContext.SubmitChanges(); } else { return(false); } } } catch (Exception e) { log.Error("Not able to unsubscribe " + email, e); return(false); } return(true); }
private string GetTaxonomyName(string site) { if (String.IsNullOrEmpty(site)) { throw new ArgumentNullException("site"); } using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { return(dataContext.SiteConfigurations.FirstOrDefault(config => config.site == site).taxonomyName); } }
/// <summary> /// Gets the last batch run by site. /// </summary> /// <param name="site">The site.</param> /// <returns>The latest batch</returns> public Batch GetLastBatchRun(string site) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { return(dataContext.Batches.Where(batch => batch.site == site).OrderByDescending(batch => batch.batchId).FirstOrDefault()); } } catch (Exception e) { log.Error("Unable to find the last batch run for site: " + site, e); throw e; } }
/// <summary> /// Gets the batches for site. /// </summary> /// <param name="site">The site.</param> /// <returns>List of Batch objects for this site</returns> public static List <Batch> GetBatchesForSite(string site) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { return(dataContext.Batches.Where(batch => batch.site == site).OrderByDescending(batch => batch.startDate).ToList()); } } catch (Exception e) { log.Error("Unable to retrieve a list of batches for this site: " + site, e); return(null); } }
/// <summary> /// Gets all sites with available data. /// </summary> /// <returns>List of site names</returns> public static List <string> GetAllSitesWithAvailableData() { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { return(dataContext.Batches.Select(batch => batch.site).Distinct().ToList()); } } catch (Exception e) { log.Error("Unable to retrieve a list of available sites", e); return(null); } }
/// <summary> /// Gets the previous documents list by site and batch id. /// </summary> /// <param name="site">The site.</param> /// <param name="batch">The batch id.</param> /// <returns>A list of documents that were in a specific batch</returns> public BatchedDocumentResults GetDocumentsList(string site, int batchId) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = null; if (batchId <= 0) { batch = GetLastBatchRun(site); } else { batch = dataContext.Batches.FirstOrDefault(b => b.batchId == batchId && b.site == site); } List <UpdatedDoc> documentList = (from docs in dataContext.UpdatedDocuments where docs.Batch1.site == site && docs.batch == batchId select new UpdatedDoc { batch = (int)docs.batch, dateUpdated = (DateTime)docs.dateUpdated, documentName = docs.documentName, documentType = docs.documentType, fileSize = docs.fileSize, url = docs.url }).ToList(); return(new BatchedDocumentResults { Status = "Success", BatchId = batchId, StartDate = (null == batch)? DateTime.MinValue : batch.startDate, EndDate = (null == batch) ? DateTime.MinValue : batch.endDate, DocumentList = documentList }); } } catch (Exception e) { log.Error("Not able to get documents list", e); return(new BatchedDocumentResults { Status = "Error " + e.Message, BatchId = batchId, }); } }
/// <summary> /// Gets all subscribers. /// </summary> /// <param name="site">The site.</param> /// <returns>List of Subscription Objects</returns> public static List <Subscription> GetCurrentSubscribers(string site) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { List <Subscription> subscriptions = dataContext.Subscriptions.Where(subscription => subscription.site == site && subscription.optedIn).ToList(); DumpSubscriptions(subscriptions); return(subscriptions); } } catch (Exception e) { log.Error("Not able to get a list of current subscribers for site: " + site, e); return(null); } }
/// <summary> /// Resends the email. /// </summary> /// <param name="site">The site.</param> /// <param name="email">The email.</param> /// <param name="batchId">The batch id.</param> /// <returns>true if successful / false if not</returns> public Boolean ResendEmail(string site, string email, int batchId) { try { string emailTemplateURL = null; string emailSubject = null; using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SiteConfiguration siteConfiguration = dataContext.SiteConfigurations.FirstOrDefault(siteName => siteName.site == site); emailTemplateURL = siteConfiguration.emailTemplateURL; emailSubject = siteConfiguration.siteName + " " + (ConfigurationManager.AppSettings["Email.Subject"] as String); } string messageBody = GetEmailBody(emailTemplateURL + "?batchId=" + batchId); EmailSender.Instance.SendEmail(email, FromAddress, messageBody, emailSubject, false, true); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SentInfo sentInfo = dataContext.SentInfos.FirstOrDefault(info => info.site == site && info.subscriptionEmail == email && info.batchId == batchId); sentInfo.lastSendDate = DateTime.Now; sentInfo.status = "resent"; dataContext.SubmitChanges(); } return(true); } catch (Exception e) { log.Error("Unable to resend email from batch " + batchId + " for site " + site + " to email " + email, e); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SentInfo sentInfo = dataContext.SentInfos.FirstOrDefault(info => info.site == site && info.subscriptionEmail == email && info.batchId == batchId); sentInfo.status = "resend failed"; dataContext.SubmitChanges(); } return(false); } }
/// <summary> /// Subscribes the specified email from the specified site's notifications. /// </summary> /// <param name="email">The email.</param> /// <param name="site">The site.</param> /// <param name="firstName">The first name.</param> /// <param name="lastName">The last name.</param> /// <returns> /// true if subscribed / false if errors occured /// </returns> public static Boolean Subscribe(string email, string site, string firstName, string lastName) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Subscription newSubscription = dataContext.Subscriptions.FirstOrDefault(sub => sub.email == email && sub.site == site); //Has opted in before, may be opted out or not. if (newSubscription != null) { if (firstName != null) { newSubscription.firstName = firstName; } if (lastName != null) { newSubscription.lastName = lastName; } newSubscription.optedIn = true; } //Is a new Subscription else { newSubscription = new Subscription(); newSubscription.email = email; newSubscription.site = site; newSubscription.firstName = firstName; newSubscription.lastName = lastName; newSubscription.optedIn = true; dataContext.Subscriptions.InsertOnSubmit(newSubscription); } dataContext.SubmitChanges(); } } catch (Exception e) { log.Error("Not able to create subscription for email: " + email, e); return(false); } return(true); }
///// <summary> ///// Gets the previous documents list by site. ///// </summary> ///// <param name="site">The site.</param> ///// <returns>A list of documents that were in the last run batch</returns> //public List<UpdatedDoc> GetDocumentsList(string site) //{ // try // { // int latestBatch = GetLastBatchRun(site).batchId; // return GetDocumentsList(site, latestBatch); // } // catch (Exception e) // { // throw e; // } //} /// <summary> /// Creates the new batch. /// </summary> /// <param name="site">The site.</param> /// <returns>The new batch.</returns> private Batch CreateNewBatch(string site) { try { using (NotificationTablesDataContext notificationDataContext = new NotificationTablesDataContext()) { Batch batch = new Batch(); batch.startDate = DateTime.Now; batch.site = site; notificationDataContext.Batches.InsertOnSubmit(batch); notificationDataContext.SubmitChanges(); return(batch); } } catch (Exception e) { log.Error("Unable to create a new batch", e); throw e; } }
/// <summary> /// Gets the sent info by site and batch id. /// </summary> /// <param name="site">The site.</param> /// <param name="batchId">The batch id.</param> /// <returns>List of SentInfo objects</returns> public List <SentInfo> GetSentInfoBySiteAndBatchId(string site, int batchId) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { DataLoadOptions dataContextOptions = new DataLoadOptions(); dataContextOptions.LoadWith <SentInfo>(info => info.Subscription); dataContext.LoadOptions = dataContextOptions; List <SentInfo> sentInfo = dataContext.SentInfos.Where(info => info.site == site && info.batchId == batchId).ToList(); return(sentInfo); } } catch (Exception e) { log.Error("Unable to retrieve list of SentInfo objects for site " + site + " and batchId " + batchId, e); return(null); } }
private Batch CreateBatchForItems(List <UpdatedDoc> itemsForBatch, DocumentProcessOptions options) { //TODO: Transaction! using (NotificationTablesDataContext notificationDataContext = new NotificationTablesDataContext()) { Batch batch = new Batch(); batch.startDate = options.StartDate; batch.endDate = options.EndDate; batch.site = options.Site; notificationDataContext.Batches.InsertOnSubmit(batch); notificationDataContext.SubmitChanges(); // Add the updated docs for this batch foreach (UpdatedDoc docToAddToBatch in itemsForBatch) { //TODO: why not use UpdatedDocument here instead of Updated Doc notificationDataContext.UpdatedDocuments.InsertOnSubmit ( new UpdatedDocument { batch = batch.batchId, dateUpdated = docToAddToBatch.dateUpdated, documentType = docToAddToBatch.documentType, documentName = docToAddToBatch.documentName, fileSize = docToAddToBatch.fileSize, url = docToAddToBatch.url } ); } ; batch.finishDate = DateTime.Now; notificationDataContext.SubmitChanges(); return(batch); } }
private static void SaveSentMailInformation(string site, int batchId, Email email) { try { using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SentInfo sentTo = new SentInfo { batchId = batchId, lastSendDate = DateTime.Now, site = site, status = "sent", subscriptionEmail = email.to }; dataContext.SentInfos.InsertOnSubmit(sentTo); dataContext.SubmitChanges(); } } catch (Exception e) { LogException(site, batchId, email, e); } }
/// <summary> /// Sends the email to recipients. /// </summary> /// <param name="site">The site.</param> /// <param name="documents">The updated documents.</param> public void SendEmailToRecipients(string site, int batchId) { try { List <Subscription> subscribers = SubscriptionService.GetCurrentSubscribers(site); if (null == subscribers || subscribers.Count == 0) { log.Info("No Subscribers - email will not be sent"); return; } string emailTemplateURL = null; string emailSubject = null; using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SiteConfiguration siteConfiguration = dataContext.SiteConfigurations.FirstOrDefault(siteName => siteName.site == site); emailTemplateURL = siteConfiguration.emailTemplateURL; emailSubject = siteConfiguration.siteName + " " + (ConfigurationManager.AppSettings["Email.Subject"] as String); } string messageBody = GetEmailBody(emailTemplateURL + "?batchId=" + batchId); SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(maxThreads); foreach (Subscription subscription in subscribers) { Email email = new Email { to = subscription.email, from = FromAddress, body = messageBody, subject = emailSubject }; PostExecuteWorkItemCallback afterEmailSend = delegate { SaveSentMailInformation(site, batchId, email); }; WorkItemInfo workItem = new WorkItemInfo(); workItem.PostExecuteWorkItemCallback = afterEmailSend; workItemsGroup.QueueWorkItem(workItem, SendMail, email); } workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.finishDate = DateTime.Now; batch.status = "Successful"; dataContext.SubmitChanges(); } } catch (Exception e) { log.Error("Unable to Send Email", e); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.status = "Unsuccessful"; dataContext.SubmitChanges(); } throw e; } }