/// <summary> /// Parses the email addresses and creates in-memory work items. /// </summary> /// <remarks> /// During parsing, duplicates and invalid addresses are discarded. /// The work items has not been saved. /// </remarks> /// <param name="emailAddresses">The email addresses as a separated string to parse. Separators are ";, \n"</param> /// <returns>The email addresses as JobWorkItem objects</returns> public static JobWorkItems ParseEmailAddressesToWorkItems(string emailAddresses, JobWorkStatus defaultStatus) { JobWorkItems items = new JobWorkItems(); string[] emailAddressArray = emailAddresses.Split(new char[] { ';', ',', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string emailAddress in emailAddressArray) { // Clean address string emailAddressCleaned = NewsLetterUtil.CleanEmailAddress(emailAddress); // Validate email address if (EmailSyntaxValidator.Validate(emailAddressCleaned) == true) { // Check if already added. JobWorkItem workItem = items.FirstOrDefault(j => j.EmailAddress.Equals(emailAddressCleaned)); if (workItem == null) { // Handle duplicates - try to load it first workItem = new JobWorkItem(emailAddressCleaned, defaultStatus); // Add to collection items.Add(workItem); } } } return(items); }
internal static void FillFromDataTable(DataTable table, JobWorkItems workItems) { foreach (DataRow row in table.Rows) { JobWorkItem workItem = new JobWorkItem(row); workItems.Items.Add(workItem); } }
/// <summary> /// Imports email addresses as work items for this job /// </summary> /// <param name="emailArray">The email addresses to import.</param> /// <param name="invalidEmailAddresses">A list of all available email addresses that could not be parsed as a valid address</param> /// <param name="duplicateAddresses">A list of duplicate email addresses added</param> /// <returns>The number of email addresses imported as new work items. Duplicates are not part of this number.</returns> public int ImportEmailAddresses(string[] emailArray, out List <string> invalidEmailAddresses, out List <string> duplicateAddresses) { JobWorkItems importedItems = new JobWorkItems(); invalidEmailAddresses = new List <string>(); duplicateAddresses = new List <string>(); int numberOfNewItems = 0; foreach (string emailAddress in emailArray) { // TODO: This can be optimized by checking the // existence of these email addresses in batches // Clean address (this is done on save, so we need to make sure it's correct) string emailAddressCleaned = NewsLetterUtil.CleanEmailAddress(emailAddress); // Validate email address if (EmailSyntaxValidator.Validate(emailAddressCleaned) == false) { // Invalid email address, skip it. invalidEmailAddresses.Add(emailAddressCleaned); } else { // Check if already imported. This is the quickest duplicate check JobWorkItem workItem = importedItems.Items.FirstOrDefault(j => j.EmailAddress.Equals(emailAddressCleaned)); if (workItem == null) { // Handle duplicates - try to load it first workItem = JobWorkItem.Load(Id, emailAddressCleaned); if (workItem == null) { // Create it, and save it. It is automatically // added to the WorkItems collection workItem = this.CreateWorkItem(emailAddressCleaned); workItem.Save(); numberOfNewItems++; } else { // Duplicate duplicateAddresses.Add(emailAddressCleaned); } // Add to imported collection, for quick // in memory duplicate check importedItems.Items.Add(workItem); } } } // Counters are no longer valid ResetStatusCounters(); return(numberOfNewItems); }
/// <summary> /// Searches the specified job for work items that matches /// a specified string. Will search all emails with a LIKE clause. /// </summary> /// <param name="jobId">The job id.</param> /// <param name="searchFor">The email to search for.</param> /// <returns>A collection of Work Items. The collection count can be 0</returns> public static JobWorkItems Search(int jobId, string searchFor) { JobWorkItems items = new JobWorkItems(); WorkItemData dataUtil = GetWorker(); DataTable workItemsTable = dataUtil.WorkItemSearch(jobId, searchFor); FillFromDataTable(workItemsTable, items); return(items); }
public static JobWorkItems ListAll(int jobId, JobWorkStatus status) { JobWorkItems items = new JobWorkItems(); WorkItemData dataUtil = GetWorker(); DataTable workItemsTable = dataUtil.WorkItemGetAllForJob(jobId, status); FillFromDataTable(workItemsTable, items); return(items); }
/// <summary> /// Gets work items for processing as a separate collection. This /// collection is not wired back to this Job object. /// </summary> /// <remarks> /// All work items returned will be set to Sending status. /// </remarks> /// <returns>A WorkItems collections with work items ready for processing.</returns> public JobWorkItems GetWorkItemsForProcessing(int batchSize) { WorkItemData dataUtil = GetWorker <WorkItemData>(); DataTable workTable = dataUtil.WorkItemGetBatchForProcessing(_id, JobWorkStatus.NotStarted, JobWorkStatus.Sending, batchSize); JobWorkItems workItems = new JobWorkItems(); JobWorkItems.FillFromDataTable(workTable, workItems); return(workItems); }
/// <summary> /// Adds work items based on email addresses in a recipient list. /// </summary> /// <remarks> /// All new items and items that exists in both list will have their /// status set to NotStarted. /// </remarks> /// <param name="recipientListId">The recipient list id.</param> /// <returns>The number of new work items</returns> public int AddWorkItemsFromRecipientList(int recipientListId) { WorkItemData dataUtil = GetWorker <WorkItemData>(); int count = dataUtil.WorkItemInsertFromRecipientList(_id, recipientListId, JobWorkStatus.NotStarted); // Remove any workitems from memory _workItems = null; // Counters are no longer valid ResetStatusCounters(); return(count); }
/// <summary> /// Deletes this job and all worker items related to the job /// </summary> public void Delete() { if (_id <= 0) { throw new ArgumentException("Cannot delete job without id."); } JobData dataUtil = GetWorker <JobData>(); dataUtil.JobDelete(_id); // Remove any workitems from memory _workItems = null; }
/// <summary> /// Loads the work items for processing into the /// WorkItems property. Will clear the existing /// collection, and replace it with the new items /// ready for processing. If you want WorkItems as /// a separate collection, use the /// GetWorkItemsForProcessing method. /// </summary> /// <param name="batchSize">Number of work items to load</param> public void LoadWorkItemsForProcessing(int batchSize) { JobWorkItems workItems = GetWorkItemsForProcessing(batchSize); if (_workItems != null) { _workItems.Items.Clear(); _workItems = null; } _workItems = workItems; // Counters are no longer valid - we just changed them ResetStatusCounters(); }
/// <summary> /// A list of work items that needs to be handled /// </summary> /// <remarks> /// When accessed for the first time, the list will be /// filled with all the work items for this job. /// This can be time consuming if there are many items /// in the database. /// </remarks> public JobWorkItems GetWorkItems() { if (_workItems == null) { // Get worker items for this job if (_id <= 0) { throw new ApplicationException( "Cannot retrieve work items for a job that has no id. Save the job first to get the id."); } // Get items _workItems = JobWorkItems.ListAll(_id); } return(_workItems); }
/// <summary> /// Deletes all work items. /// </summary> public void DeleteAllWorkItems() { if (_id <= 0) { throw new ArgumentException("Cannot remove work items for a job without id. Please save the job first."); } WorkItemData dataUtil = GetWorker <WorkItemData>(); dataUtil.WorkItemDeleteAllForJob(_id); // Remove any workitems from memory _workItems = null; // Counters are no longer valid ResetStatusCounters(); }
/// <summary> /// Removes work items from this job based on the contents of a recipient list. /// </summary> /// <remarks> /// Deletes all work items with email addresses that also exists in a recipient list /// </remarks> /// <param name="recipientListId">The id of the recipient list to use as filter.</param> public int FilterOnRecipients(int recipientListId) { if (_id <= 0) { throw new ArgumentException("Cannot filter work items for a job without id. Please save the job first."); } WorkItemData dataUtil = GetWorker <WorkItemData>(); int count = dataUtil.WorkItemFilterAgainstRecipientList(_id, recipientListId); // Remove any workitems from memory _workItems = null; // Counters are no longer valid ResetStatusCounters(); return(count); }