예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        /// <summary>
        /// Creates a new work item with a given email address,
        /// connected to this job, but does not add it to the WorkItems collection.
        /// The work item is not saved, it has to be explicitly saved.
        /// </summary>
        /// <remarks>
        /// There is no check for a duplicate email address for this
        /// job when creating the work item.
        /// </remarks>
        /// <returns>
        /// A new work item object with an email address connected to this job
        /// </returns>
        public JobWorkItem CreateWorkItem(string email)
        {
            // Get worker items for this job
            if (_id <= 0)
            {
                throw new ApplicationException("Cannot create work items for a job that has no id. Save the job first to get the id.");
            }

            JobWorkItem workItem = new JobWorkItem(_id, email, JobWorkStatus.NotStarted, null);

            return(workItem);
        }